Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes to task proto serialization #1923

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/src/proto-to-firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {DocumentData, DocumentFieldValue} from '@google-cloud/firestore';
* The map is keyed by message field numbers represented as strings, while field values are
* converted to corresponding Firestore data types.
*/
export function toDocumentData(message: object): DocumentData {
export function toDocumentData(message: object): DocumentData | DocumentData[] {
if (Array.isArray(message)) {
return message.map(messageEl => toDocumentData(messageEl));
}
if (Object.keys(message).length === 0) {
return {};
}
Expand Down Expand Up @@ -110,4 +113,4 @@ function toObjectValue(fieldType: string, value: any) {
} else {
return toDocumentData(value);
}
}
}
13 changes: 6 additions & 7 deletions lib/src/testing/firestore/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Firestore } from "@google-cloud/firestore";
import {Firestore} from '@google-cloud/firestore';

const MockFirebase = require('mock-cloud-firestore');

Expand All @@ -30,9 +30,8 @@ export function createMockFirestore(): Firestore {
* in tests to work around lack of support in MockFirebase lib.
*/
export function TestGeoPoint(_latitude: number, _longitude: number) {
return {
_latitude,
_longitude,
};
}

return {
_latitude,
_longitude,
};
}
10 changes: 5 additions & 5 deletions lib/src/testing/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

export * from "./firestore";
export * from "./event-context";
export * from "./document-snapshot";
export * from "./query";
export * from "./setup";
export * from './firestore';
export * from './event-context';
export * from './document-snapshot';
export * from './query';
export * from './setup';
4 changes: 2 additions & 2 deletions lib/src/testing/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import {SpecReporter, StacktraceOption} from 'jasmine-spec-reporter';
// Show references to TypeScript code in stacktraces.
require('source-map-support').install();

jasmine.getEnv().clearReporters()
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(
new SpecReporter({
spec: {
displayPending: true,
displayStacktrace: StacktraceOption.RAW,
},
})
)
);
21 changes: 11 additions & 10 deletions web/src/app/converters/proto-model-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,23 @@ export function aclToDocument(acl: Map<string, Role>): DocumentData | Error {
/**
* Returns the proto representation of a Job model object.
*/
export function jobToDocument(job: Job): DocumentData {
export function jobToDocument(
job: Job,
taskOverride: List<Task> | null = null
): DocumentData {
return toDocumentData(
new Pb.Job({
id: job.id,
index: job.index,
name: job.name,
style: new Pb.Style({color: job.color}),
tasks: job.tasks?.toList().map(toTaskMessage).toArray(),
tasks: (taskOverride ?? job.tasks?.toList() ?? List())
.map((task: Task) => toTaskMessage(task))
.toArray(),
})
);
}

export function tasksToDocument(tasks: List<Task>): DocumentData {
return toDocumentData(tasks.toList().map(toTaskMessage).toArray());
}

/**
* Returns a Protobuf message representing a partial Task model.
*/
Expand Down Expand Up @@ -192,7 +193,7 @@ function toTaskTypeMessage(
case TaskType.CAPTURE_LOCATION:
return {
captureLocation: new Pb.Task.CaptureLocation({
minAccuracyMeters: null,
minAccuracyMeters: 0,
}),
};
default:
Expand Down Expand Up @@ -222,7 +223,7 @@ function toTaskConditionMessage(
}

/**
* Returns a Protobuf messager epresenting a Task model.
* Returns a Protobuf message representing a Task model.
*/
function toTaskMessage(task: Task): Pb.ITask {
return new Pb.Task({
Expand All @@ -232,8 +233,8 @@ function toTaskMessage(task: Task): Pb.ITask {
prompt: task.label,
required: task.required,
level: task.addLoiTask
? Pb.Task.DataCollectionLevel.LOI_DATA
: Pb.Task.DataCollectionLevel.LOI_METADATA,
? Pb.Task.DataCollectionLevel.LOI_METADATA
: Pb.Task.DataCollectionLevel.LOI_DATA,
conditions: toTaskConditionMessage(task.condition),
});
}
2 changes: 1 addition & 1 deletion web/src/app/pages/create-survey/create-survey.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class CreateSurveyComponent implements OnInit {
await this.taskService.addOrUpdateTasks(
survey.id,
// Assume there is at least one job.
survey.jobs.first()!.id,
survey.jobs.first(),
tasks!
);
}
Expand Down
35 changes: 21 additions & 14 deletions web/src/app/services/data-store/data-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
jobToDocument,
newSurveyToDocument,
partialSurveyToDocument,
tasksToDocument,
} from 'app/converters/proto-model-converter';
import {Job} from 'app/models/job.model';
import {LocationOfInterest} from 'app/models/loi.model';
Expand Down Expand Up @@ -242,11 +241,15 @@ export class DataStoreService {
]);
}

updateJob(surveyId: string, job: Job): Promise<void> {
updateJob(
surveyId: string,
job: Job,
tasks: List<Task> | null = null
): Promise<void> {
return this.db
.collection(`${SURVEYS_COLLECTION_NAME}/${surveyId}/jobs`)
.doc(job.id)
.set(jobToDocument(job));
.set(jobToDocument(job, tasks));
}

async deleteSurvey(survey: Survey) {
Expand Down Expand Up @@ -530,18 +533,22 @@ export class DataStoreService {

addOrUpdateTasks(
surveyId: string,
jobId: string,
job: Job,
tasks: List<Task>
): Promise<void> {
return this.db
.collection(SURVEYS_COLLECTION_NAME)
.doc(surveyId)
.update({
[`jobs.${jobId}.tasks`]: {
...FirebaseDataConverter.tasksToJS(this.convertTasksListToMap(tasks)),
...tasksToDocument(tasks),
},
});
): Promise<[void, void]> {
return Promise.all([
this.db
.collection(SURVEYS_COLLECTION_NAME)
.doc(surveyId)
.update({
[`jobs.${job.id}.tasks`]: {
...FirebaseDataConverter.tasksToJS(
this.convertTasksListToMap(tasks)
),
},
}),
this.updateJob(surveyId, job, tasks),
]);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions web/src/app/services/task/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {Injectable} from '@angular/core';
import {List, Map} from 'immutable';
import {Observable, switchMap} from 'rxjs';

import {DataCollectionStrategy} from 'app/models/job.model';
import {DataCollectionStrategy, Job} from 'app/models/job.model';
import {MultipleChoice} from 'app/models/task/multiple-choice.model';
import {Task, TaskType} from 'app/models/task/task.model';
import {DataStoreService} from 'app/services/data-store/data-store.service';
Expand Down Expand Up @@ -101,10 +101,10 @@ export class TaskService {

addOrUpdateTasks(
surveyId: string,
jobId: string,
job: Job,
tasks: List<Task>
): Promise<void> {
return this.dataStoreService.addOrUpdateTasks(surveyId, jobId, tasks);
): Promise<[void, void]> {
return this.dataStoreService.addOrUpdateTasks(surveyId, job, tasks);
}

/**
Expand Down
Loading