Refactor API types and sync logic
Rename and refactor the types passed over the API to be based on an entity that's either living or a tombstone. A living entity has a deleted property that's either undefined or false, while a tombstone has a deleted property set to true. All entities have a numeric id and an updatedAt timestamp. To sync entities, an array of replacements are passed around. Living entities are replaced with tombstones when they're deleted. And tombstones are replaced with living entities when restored.
This commit is contained in:
parent
251e83f640
commit
fe06d0d6bd
36 changed files with 1242 additions and 834 deletions
|
@ -1,39 +1,28 @@
|
|||
import { Account } from "~/shared/types/account";
|
||||
import { readAccounts, writeAccounts } from "~/server/database";
|
||||
import { DateTime } from "luxon";
|
||||
import { apiAccountPatchSchema } from "~/shared/types/api";
|
||||
import { z } from "zod/v4-mini";
|
||||
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const session = await requireServerSession(event);
|
||||
const body: Pick<Account, "interestedIds" | "timezone"> = await readBody(event);
|
||||
if (
|
||||
body.interestedIds !== undefined
|
||||
&& (
|
||||
!(body.interestedIds instanceof Array)
|
||||
|| !body.interestedIds.every(id => typeof id === "string")
|
||||
)
|
||||
) {
|
||||
const { success, error, data: patch } = apiAccountPatchSchema.safeParse(await readBody(event));
|
||||
if (!success) {
|
||||
throw createError({
|
||||
status: 400,
|
||||
message: "Invalid interestedIds",
|
||||
statusText: "Bad Request",
|
||||
message: z.prettifyError(error),
|
||||
});
|
||||
}
|
||||
|
||||
if (body.timezone !== undefined) {
|
||||
if (typeof body.timezone !== "string") {
|
||||
if (patch.timezone?.length) {
|
||||
const zonedTime = DateTime.local({ locale: "en-US" }).setZone(patch.timezone);
|
||||
if (!zonedTime.isValid) {
|
||||
throw createError({
|
||||
status: 400,
|
||||
message: "Invalid timezone",
|
||||
message: "Invalid timezone: " + zonedTime.invalidExplanation,
|
||||
});
|
||||
}
|
||||
if (body.timezone.length) {
|
||||
const zonedTime = DateTime.local({ locale: "en-US" }).setZone(body.timezone);
|
||||
if (!zonedTime.isValid) {
|
||||
throw createError({
|
||||
status: 400,
|
||||
message: "Invalid timezone: " + zonedTime.invalidExplanation,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const accounts = await readAccounts();
|
||||
|
@ -42,16 +31,23 @@ export default defineEventHandler(async (event) => {
|
|||
throw Error("Account does not exist");
|
||||
}
|
||||
|
||||
if (body.interestedIds !== undefined) {
|
||||
if (body.interestedIds.length) {
|
||||
sessionAccount.interestedIds = body.interestedIds;
|
||||
if (patch.interestedEventIds !== undefined) {
|
||||
if (patch.interestedEventIds.length) {
|
||||
sessionAccount.interestedEventIds = patch.interestedEventIds;
|
||||
} else {
|
||||
delete sessionAccount.interestedIds;
|
||||
delete sessionAccount.interestedEventIds;
|
||||
}
|
||||
}
|
||||
if (body.timezone !== undefined) {
|
||||
if (body.timezone)
|
||||
sessionAccount.timezone = body.timezone;
|
||||
if (patch.interestedEventSlotIds !== undefined) {
|
||||
if (patch.interestedEventSlotIds.length) {
|
||||
sessionAccount.interestedEventSlotIds = patch.interestedEventSlotIds;
|
||||
} else {
|
||||
delete sessionAccount.interestedEventSlotIds;
|
||||
}
|
||||
}
|
||||
if (patch.timezone !== undefined) {
|
||||
if (patch.timezone)
|
||||
sessionAccount.timezone = patch.timezone;
|
||||
else
|
||||
delete sessionAccount.timezone;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue