feat(stores): Verify store data
This commit is contained in:
@ -61,12 +61,28 @@ export const SyncPayload = z.object({
|
|||||||
|
|
||||||
export type SyncPayload = z.infer<typeof SyncPayload>;
|
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) {
|
export async function sync(payload: SyncPayload, user: User) {
|
||||||
const changes: Store[] = [];
|
const changes: Store[] = [];
|
||||||
// Apply changes from client
|
// Apply changes from client
|
||||||
for(const change of payload.changes) {
|
for(const change of payload.changes) {
|
||||||
const store = await Store.findOne({ where: { id: change.id }, relations: { user: true } });
|
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) {
|
if(!store) {
|
||||||
// Store doesn't exist, create it
|
// Store doesn't exist, create it
|
||||||
const newStore = new Store();
|
const newStore = new Store();
|
||||||
|
|||||||
Reference in New Issue
Block a user