From c59df0bf6a66e05ba104d03d8cbe9a60197767c3 Mon Sep 17 00:00:00 2001 From: Jannik Date: Fri, 26 Sep 2025 20:08:40 +0200 Subject: [PATCH] feat: more work on stores --- README.md | 1 + src/stores.ts | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 148b3e8..12d5046 100644 --- a/README.md +++ b/README.md @@ -65,5 +65,6 @@ You can run this yourself to host your own instance, or contribute to the offici - `OIDC_JWKS_URL` (the JWKS/Certificate URL of your OIDC server) - `REVIEWS_ENABLED` (optional, set to `true` to enable POI reviews by users, requires OIDC) - `HAZARDS_ENABLED` (optional, set to `true` to enable hazard reporting by users, requires OIDC) + - `STORES_ENALED` (optional, set to `true` to enable user stores, requires OIDC) When configuring your OIDC server, make sure to enable Public Client and PCKE support. diff --git a/src/stores.ts b/src/stores.ts index d0f4154..15fa59b 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -49,7 +49,9 @@ export const SyncPayload = z.object({ id: z.uuid(), operation: z.enum(["create", "update", "delete"]), data: z.string(), - modified_at: z.string().refine(val => !isNaN(Date.parse(val)), { message: "Invalid date" }) + modified_at: z.string().refine(val => !isNaN(Date.parse(val)), { message: "Invalid date" }), + type: z.enum(["location", "vehicle", "route"]), + name: z.string().min(1).max(100) })), stores: z.array(z.object({ id: z.uuid(), @@ -59,12 +61,25 @@ export const SyncPayload = z.object({ export type SyncPayload = z.infer; +// TODO: verify data export async function sync(payload: SyncPayload, user: User) { const changes: Store[] = []; // Apply changes from client for(const change of payload.changes) { const store = await Store.findOneBy({ id: change.id }); - if(!store) continue; + if(!store) { + // Store doesn't exist, create it + const newStore = new Store(); + newStore.id = change.id; + newStore.user = user; + newStore.data = change.data; + newStore.modified_at = new Date(change.modified_at); + newStore.created_at = new Date(); + newStore.type = change.type; + newStore.name = change.name; + await newStore.save(); + continue; + } if(store.user.id !== user.id) { // Not the owner of this store changes.push(store); // Send back the store to the client to overwrite their changes