2026-10-04 19:04:14 +00:00
|
|
|
import type { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import type { Transaction } from "sequelize";
|
|
|
|
|
|
|
|
|
|
import type { Asset } from "../../domain/asset.aggregate";
|
|
|
|
|
|
2026-10-10 11:32:40 +00:00
|
|
|
import { WorkstationAssetModel } from "./workstation.model";
|
2026-10-04 19:04:14 +00:00
|
|
|
import { WorkstationPersistenceMapper } from "./workstation-persistence.mapper";
|
2026-10-10 11:32:40 +00:00
|
|
|
import { readWorkstations } from "./workstation-relational";
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
export async function loadWorkstations(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
transaction: Transaction,
|
|
|
|
|
assets: readonly Asset[],
|
|
|
|
|
sites: readonly { id: string }[],
|
|
|
|
|
people: readonly { id: string }[]
|
|
|
|
|
) {
|
2026-10-10 11:32:40 +00:00
|
|
|
const database = WorkstationAssetModel.sequelize;
|
|
|
|
|
|
|
|
|
|
if (!database) {
|
|
|
|
|
throw new Error("Persistencia de puestos sin inicializar.");
|
|
|
|
|
}
|
|
|
|
|
const { rows, revisions } = await readWorkstations(
|
|
|
|
|
database,
|
|
|
|
|
companyId.toPrimitive(),
|
|
|
|
|
transaction
|
|
|
|
|
);
|
2026-10-04 19:04:14 +00:00
|
|
|
const references = await WorkstationAssetModel.findAll({
|
|
|
|
|
where: { companyId: companyId.toPrimitive() },
|
|
|
|
|
transaction,
|
|
|
|
|
});
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
return rows.map((row) => {
|
|
|
|
|
const item = new WorkstationPersistenceMapper().toDomain(
|
|
|
|
|
row,
|
|
|
|
|
revisions.filter((revision) => revision.workstationId === row.id),
|
|
|
|
|
references.filter((reference) => reference.workstationId === row.id)
|
|
|
|
|
);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
for (const revision of item.history) {
|
|
|
|
|
const values = revision.values.toPrimitive();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
if (
|
|
|
|
|
(values.siteId && !sites.some((site) => site.id === values.siteId)) ||
|
|
|
|
|
(values.personId && !people.some((person) => person.id === values.personId)) ||
|
2026-10-14 17:42:59 +00:00
|
|
|
values.assetIds.some((id) => !assets.some((asset) => asset.id.toPrimitive() === id))
|
2026-10-04 19:04:14 +00:00
|
|
|
) {
|
|
|
|
|
throw new Error("Puesto de trabajo fuera de su compañía.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|