feat(stores): Verify store data
Some checks failed
TrafficCue Server CI / check (push) Failing after 51s
TrafficCue Server CD / build (push) Successful in 2m2s

This commit is contained in:
2025-09-27 13:42:50 +02:00
parent a0d08e5041
commit 9940a86ac0

View File

@ -61,12 +61,28 @@ export const SyncPayload = z.object({
export type SyncPayload = z.infer<typeof SyncPayload>;
// TODO: verify data
export function verifyStoreData(type: StoreType, data: string) {
const schema = storeTypes[type];
if(!schema) return false;
try {
const parsedData = JSON.parse(data);
schema.parse(parsedData);
return true;
} catch {
return false;
}
}
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.findOne({ where: { id: change.id }, relations: { user: true } });
if(!verifyStoreData(change.type, change.data)) {
// Invalid data
if(store) changes.push(store); // Send back the store to the client to overwrite their changes
continue;
}
if(!store) {
// Store doesn't exist, create it
const newStore = new Store();