Skip to content

Commit

Permalink
fixed proto properties conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
rfontanarosa committed Sep 27, 2024
1 parent ef7ccdb commit b87667e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion functions/src/import-geojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function toLoiPb(
});
}

function toLoiPbProperties(properties: GeoJsonProperties): {
export function toLoiPbProperties(properties: GeoJsonProperties): {
[k: string]: Pb.LocationOfInterest.Property;
} {
return Object.fromEntries(
Expand Down
23 changes: 16 additions & 7 deletions functions/src/on-create-loi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ import {QueryDocumentSnapshot} from 'firebase-functions/v1/firestore';
import {getDatastore} from './common/context';
import {Datastore} from './common/datastore';
import {broadcastSurveyUpdate} from './common/broadcast-survey-update';
import {GroundProtos} from '@ground/proto';
import {registry} from '@ground/lib';
import {geojsonToWKT} from '@terraformer/wkt';
import {toLoiPbProperties} from './import-geojson';

import Pb = GroundProtos.ground.v1beta1;
const l = registry.getFieldIds(Pb.LocationOfInterest);

type Properties = {[key: string]: string | number};

type PropertyGenerator = {
name: string;
Expand All @@ -33,17 +41,17 @@ export async function onCreateLoiHandler(
) {
const surveyId = context.params.surveyId;
const loiId = context.params.loiId;
const loi = snapshot.data();
const data = snapshot.data();

if (!loiId || !loi) return;
if (!loiId || !data) return;

const db = getDatastore();

let properties = loi.properties || {};
let properties = {} as Properties;

const propertyGenerators = await db.fetchPropertyGenerators();

const wkt = geojsonToWKT(Datastore.fromFirestoreMap(loi.geometry));
const wkt = geojsonToWKT(Datastore.fromFirestoreMap(data[l.geometry]));

for (const propertyGeneratorDoc of propertyGenerators.docs) {
properties = await updateProperties(
Expand All @@ -57,7 +65,10 @@ export async function onCreateLoiHandler(
.forEach(key => (properties[key] = JSON.stringify(properties[key])));
}

await db.updateLoiProperties(surveyId, loiId, properties);
await db.updateLoiProperties(surveyId, loiId, {
...data[l.properties],
...toLoiPbProperties(properties),
});

await broadcastSurveyUpdate(context.params.surveyId);
}
Expand All @@ -79,8 +90,6 @@ async function updateProperties(
};
}

type Properties = {[key: string]: string | number};

async function fetchProperties(url: string, wkt: string): Promise<Properties> {
const response = await fetch(url, {
method: 'POST',
Expand Down

0 comments on commit b87667e

Please sign in to comment.