-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1152 from jetstreamapp/feat/record-sync
Feat/record-sync
- Loading branch information
Showing
123 changed files
with
3,498 additions
and
944 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { ensureBoolean, REGEX } from '@jetstream/shared/utils'; | ||
import { SyncRecordOperationSchema } from '@jetstream/types'; | ||
import { parseISO } from 'date-fns'; | ||
import { clamp } from 'lodash'; | ||
import { z } from 'zod'; | ||
import * as userSyncDbService from '../db/data-sync.db'; | ||
import { emitRecordSyncEventsToOtherClients, SyncEvent } from '../services/data-sync-broadcast.service'; | ||
import { sendJson } from '../utils/response.handlers'; | ||
import { createRoute } from '../utils/route.utils'; | ||
|
||
export const routeDefinition = { | ||
pull: { | ||
controllerFn: () => pull, | ||
validators: { | ||
query: z.object({ | ||
updatedAt: z | ||
.string() | ||
.regex(REGEX.ISO_DATE) | ||
.nullish() | ||
.transform((val) => (val ? parseISO(val) : null)), | ||
limit: z.coerce | ||
.number() | ||
.int() | ||
.optional() | ||
.default(userSyncDbService.MAX_PULL) | ||
.transform((val) => clamp(val, userSyncDbService.MIN_PULL, userSyncDbService.MAX_PULL)), | ||
/** | ||
* Used for pagination, if there are more records, this is the last key of the previous page | ||
*/ | ||
lastKey: z.string().nullish(), | ||
}), | ||
hasSourceOrg: false, | ||
}, | ||
}, | ||
push: { | ||
controllerFn: () => push, | ||
validators: { | ||
query: z.object({ | ||
clientId: z.string().uuid(), | ||
updatedAt: z | ||
.string() | ||
.regex(REGEX.ISO_DATE) | ||
.nullish() | ||
.transform((val) => (val ? parseISO(val) : null)), | ||
includeAllIfUpdatedAtNull: z | ||
.union([z.enum(['true', 'false']), z.boolean()]) | ||
.optional() | ||
.default(false) | ||
.transform(ensureBoolean), | ||
}), | ||
body: SyncRecordOperationSchema.array().max(userSyncDbService.MAX_SYNC), | ||
hasSourceOrg: false, | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* Pull changes from server | ||
*/ | ||
const pull = createRoute(routeDefinition.pull.validators, async ({ user, query }, req, res) => { | ||
const { lastKey, updatedAt, limit } = query; | ||
const response = await userSyncDbService.findByUpdatedAt({ | ||
userId: user.id, | ||
lastKey, | ||
updatedAt, | ||
limit, | ||
}); | ||
sendJson(res, response); | ||
}); | ||
|
||
/** | ||
* Push changes to server and emit to any other clients the user has active | ||
*/ | ||
const push = createRoute(routeDefinition.push.validators, async ({ user, body: records, query }, req, res) => { | ||
const response = await userSyncDbService.syncRecordChanges({ | ||
updatedAt: query.updatedAt, | ||
userId: user.id, | ||
records, | ||
includeAllIfUpdatedAtNull: query.includeAllIfUpdatedAtNull, | ||
}); | ||
|
||
const syncEvent: SyncEvent = { | ||
clientId: query.clientId, | ||
data: { keys: response.records.map(({ key }) => key) }, | ||
userId: user.id, | ||
}; | ||
|
||
emitRecordSyncEventsToOtherClients(req.session.id, syncEvent); | ||
|
||
sendJson(res, response); | ||
}); |
Oops, something went wrong.