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";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
WorkstationAssetModel,
|
|
|
|
|
WorkstationModel,
|
|
|
|
|
WorkstationRevisionModel,
|
|
|
|
|
} from "./workstation.model";
|
|
|
|
|
import { WorkstationPersistenceMapper } from "./workstation-persistence.mapper";
|
|
|
|
|
export async function loadWorkstations(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
transaction: Transaction,
|
|
|
|
|
assets: readonly Asset[],
|
|
|
|
|
sites: readonly { id: string }[],
|
|
|
|
|
people: readonly { id: string }[]
|
|
|
|
|
) {
|
|
|
|
|
const rows = await WorkstationModel.findAll({
|
|
|
|
|
where: { companyId: companyId.toPrimitive() },
|
|
|
|
|
transaction,
|
|
|
|
|
});
|
|
|
|
|
const revisions = rows.length
|
|
|
|
|
? await WorkstationRevisionModel.findAll({
|
|
|
|
|
where: { workstationId: rows.map((item) => item.id) },
|
|
|
|
|
transaction,
|
|
|
|
|
})
|
|
|
|
|
: [];
|
|
|
|
|
const references = await WorkstationAssetModel.findAll({
|
|
|
|
|
where: { companyId: companyId.toPrimitive() },
|
|
|
|
|
transaction,
|
|
|
|
|
});
|
|
|
|
|
return rows.map((row) => {
|
|
|
|
|
const item = new WorkstationPersistenceMapper().toDomain(
|
|
|
|
|
row,
|
|
|
|
|
revisions.filter((revision) => revision.workstationId === row.id),
|
|
|
|
|
references.filter((reference) => reference.workstationId === row.id)
|
|
|
|
|
);
|
|
|
|
|
for (const revision of item.history) {
|
|
|
|
|
const values = revision.values.toPrimitive();
|
|
|
|
|
if (
|
|
|
|
|
(values.siteId && !sites.some((site) => site.id === values.siteId)) ||
|
|
|
|
|
(values.personId && !people.some((person) => person.id === values.personId)) ||
|
2026-10-05 07:53:29 +00:00
|
|
|
[...values.assetIds, ...values.sharedAssetIds].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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|