123 lines
4.1 KiB
TypeScript
123 lines
4.1 KiB
TypeScript
import { DataTypes } from "sequelize";
|
|
|
|
import { MailSnapshotSchema } from "../../domain/mail-configuration-values";
|
|
import { MailboxInputSchema, MailboxUseInputSchema } from "../../domain/mailbox-values";
|
|
import { RemoteConnectionInputSchema } from "../../domain/remote-connection-values";
|
|
|
|
import type { SnapshotDefinition } from "./scalar-snapshot-reader";
|
|
|
|
const reference = (model: string, allowNull = true) => ({
|
|
type: DataTypes.UUID,
|
|
allowNull,
|
|
references: { model, key: "id" },
|
|
onDelete: "RESTRICT",
|
|
});
|
|
|
|
const note = { type: DataTypes.TEXT, allowNull: true };
|
|
const state = { type: DataTypes.STRING(20), allowNull: false };
|
|
|
|
export const mailboxSnapshot = {
|
|
schema: MailboxInputSchema,
|
|
columns: {
|
|
address: { type: DataTypes.STRING(254), allowNull: false },
|
|
kind: { type: DataTypes.STRING(20), allowNull: false },
|
|
context: { type: DataTypes.STRING(255), allowNull: true },
|
|
accountId: reference("access_items"),
|
|
active: { type: DataTypes.BOOLEAN, allowNull: false },
|
|
notes: note,
|
|
},
|
|
booleans: ["active"],
|
|
} satisfies SnapshotDefinition<ReturnType<typeof MailboxInputSchema.parse>>;
|
|
|
|
export const mailboxUseSnapshot = {
|
|
schema: MailboxUseInputSchema,
|
|
columns: {
|
|
mailboxId: reference("mailboxes", false),
|
|
personId: reference("company_contacts", false),
|
|
startDate: { type: DataTypes.STRING(10), allowNull: false },
|
|
endDate: { type: DataTypes.STRING(10), allowNull: true },
|
|
},
|
|
} satisfies SnapshotDefinition<ReturnType<typeof MailboxUseInputSchema.parse>>;
|
|
|
|
export const mailSnapshot = {
|
|
schema: MailSnapshotSchema,
|
|
columns: {
|
|
snapshotKind: { type: DataTypes.STRING(10), allowNull: false },
|
|
personId: reference("company_contacts"),
|
|
accountId: reference("access_items"),
|
|
assetId: reference("assets"),
|
|
profileId: reference("work_profiles"),
|
|
mailboxId: reference("mailboxes"),
|
|
address: { type: DataTypes.STRING(254), allowNull: false },
|
|
method: { type: DataTypes.STRING(16), allowNull: false },
|
|
state,
|
|
notes: note,
|
|
},
|
|
fromColumns(row: Record<string, unknown>) {
|
|
const { snapshotKind, personId, accountId, assetId, profileId, mailboxId, ...common } = row;
|
|
|
|
if (snapshotKind === "legacy") {
|
|
return { ...common, personId, accountId, assetId };
|
|
}
|
|
if (snapshotKind !== "profile") {
|
|
throw new Error("Tipo de instantánea de correo inconsistente.");
|
|
}
|
|
|
|
return { ...common, profileId, mailboxId };
|
|
},
|
|
toColumns(source: unknown) {
|
|
const input = MailSnapshotSchema.parse(source);
|
|
|
|
return {
|
|
personId: null,
|
|
accountId: null,
|
|
assetId: null,
|
|
profileId: null,
|
|
mailboxId: null,
|
|
snapshotKind: "profileId" in input ? "profile" : "legacy",
|
|
...input,
|
|
};
|
|
},
|
|
} satisfies SnapshotDefinition<ReturnType<typeof MailSnapshotSchema.parse>>;
|
|
|
|
export const remoteSnapshot = {
|
|
schema: RemoteConnectionInputSchema,
|
|
columns: {
|
|
personId: reference("company_contacts", false),
|
|
sourceKind: { type: DataTypes.STRING(20), allowNull: false },
|
|
sourceAssetId: reference("assets"),
|
|
targetKind: { type: DataTypes.STRING(16), allowNull: false },
|
|
targetAssetId: reference("assets"),
|
|
targetResourceId: reference("resources"),
|
|
siteId: reference("company_sites"),
|
|
method: { type: DataTypes.STRING(80), allowNull: false },
|
|
requiresVpn: { type: DataTypes.BOOLEAN, allowNull: false },
|
|
state,
|
|
notes: note,
|
|
},
|
|
booleans: ["requiresVpn"],
|
|
fromColumns(row: Record<string, unknown>) {
|
|
const { targetAssetId, targetResourceId, ...values } = row;
|
|
|
|
return {
|
|
...values,
|
|
targetId:
|
|
row.targetKind === "asset"
|
|
? targetAssetId
|
|
: row.targetKind === "resource"
|
|
? targetResourceId
|
|
: null,
|
|
};
|
|
},
|
|
toColumns(source: unknown) {
|
|
const input = RemoteConnectionInputSchema.parse(source);
|
|
const { targetId, ...values } = input;
|
|
|
|
return {
|
|
...values,
|
|
targetAssetId: input.targetKind === "asset" ? targetId : null,
|
|
targetResourceId: input.targetKind === "resource" ? targetId : null,
|
|
};
|
|
},
|
|
} satisfies SnapshotDefinition<ReturnType<typeof RemoteConnectionInputSchema.parse>>;
|