UNPKG

@paroicms/server

Version:
110 lines 3.97 kB
import { type } from "arktype"; import { AccountPreferencesAT, formatAccount } from "../../common/data-format.js"; const AuthorNodeRowAT = type({ nodeId: "number", title: "string|null", "+": "reject", }).pipe((r) => ({ nodeId: String(r.nodeId), title: r.title ?? undefined, })); const AccountRowAT = type({ id: "number", email: "string", name: "string|null", preferences: "string|null", passwordResetToken: "string|null", active: "0|1", "+": "reject", }).pipe((r) => ({ id: String(r.id), email: r.email, name: r.name ?? undefined, preferences: r.preferences ?? undefined, passwordResetToken: r.passwordResetToken ?? undefined, active: r.active === 1, })); export async function linkAuthorToAccount(siteContext, authorNodeId, accountId) { await siteContext.cn("PaAuthorAccount").insert({ authorNodeId, accountId, }); } export async function unlinkAuthorFromAccount(siteContext, authorNodeId, accountId) { await siteContext.cn("PaAuthorAccount").where({ authorNodeId, accountId }).delete(); } export async function getLinkedAuthors(siteContext, accountId) { const rows = await siteContext .cn("PaAuthorAccount as aa") .innerJoin("PaNode as n", "n.id", "aa.authorNodeId") .leftJoin("PaDocument as d", function () { this.on("d.nodeId", "=", "n.id").andOn("d.language", "=", siteContext.cn.raw("?", ["en"])); }) .select("n.id as nodeId", "d.title") .where("aa.accountId", accountId); return rows.map((row) => AuthorNodeRowAT.assert(row)); } export async function getLinkedAccounts(siteContext, authorNodeId) { const rows = await siteContext .cn("PaAuthorAccount as aa") .innerJoin("PaAccount as a", "a.id", "aa.accountId") .select("a.id", "a.email", "a.name", "a.preferences", "a.passwordResetToken", "a.active") .where("aa.authorNodeId", authorNodeId); return rows.map((row) => formatAccount(AccountRowAT.assert(row))); } export async function setCurrentAuthor(siteContext, accountId, authorNodeId) { const account = await siteContext .cn("PaAccount") .select("preferences") .where("id", accountId) .first(); if (!account) return; const PreferencesRowAT = type({ preferences: "string|null", "+": "reject", }).pipe((r) => ({ preferences: r.preferences ?? undefined, })); const validatedRow = PreferencesRowAT.assert(account); const currentPreferences = validatedRow.preferences ? AccountPreferencesAT.assert(JSON.parse(validatedRow.preferences)) : {}; const newPreferences = { ...currentPreferences, currentAuthorNodeId: authorNodeId, }; await siteContext .cn("PaAccount") .where("id", accountId) .update({ preferences: JSON.stringify(newPreferences) }); } export async function getCurrentAuthor(siteContext, accountId) { const linkedAuthors = await getLinkedAuthors(siteContext, accountId); if (linkedAuthors.length === 0) return; if (linkedAuthors.length === 1) return linkedAuthors[0]; const account = await siteContext .cn("PaAccount") .select("preferences") .where("id", accountId) .first(); if (!account) return; const PreferencesRowAT = type({ preferences: "string|null", "+": "reject", }).pipe((r) => ({ preferences: r.preferences ?? undefined, })); const validatedRow = PreferencesRowAT.assert(account); if (!validatedRow.preferences) return; const preferences = AccountPreferencesAT.assert(JSON.parse(validatedRow.preferences)); if (!preferences.currentAuthorNodeId) return; const currentAuthor = linkedAuthors.find((author) => author.nodeId === preferences.currentAuthorNodeId); return currentAuthor; } //# sourceMappingURL=author-account.queries.js.map