2026-10-04 19:04:14 +00:00
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
|
|
|
|
|
import { AssetCatalogError } from "../domain/asset-definition";
|
|
|
|
|
import { Workstation } from "../domain/workstation.aggregate";
|
|
|
|
|
import type { WorkstationInput } from "../domain/workstation-values";
|
|
|
|
|
|
|
|
|
|
import type { IAssetRepository } from "./asset.repository";
|
|
|
|
|
import { mapWorkstationInput } from "./workstation-input.mapper";
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
export class WorkstationUseCases {
|
|
|
|
|
constructor(private readonly repository: IAssetRepository) {}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-13 07:30:28 +00:00
|
|
|
remove(companyId: string, id: string) {
|
|
|
|
|
const company = UniqueID.create(companyId);
|
|
|
|
|
const identity = UniqueID.create(id);
|
|
|
|
|
|
|
|
|
|
if (!(company.isSuccess && identity.isSuccess)) {
|
|
|
|
|
return Promise.resolve(Result.fail(new AssetCatalogError("INVALID_REQUEST")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.repository.removeWorkstation(company.data, identity.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decommission(companyId: string, id: string, actorId: string) {
|
|
|
|
|
const company = UniqueID.create(companyId);
|
|
|
|
|
const identity = UniqueID.create(id);
|
|
|
|
|
const actor = UniqueID.create(actorId);
|
|
|
|
|
|
|
|
|
|
if (!(company.isSuccess && identity.isSuccess && actor.isSuccess)) {
|
|
|
|
|
return Promise.resolve(Result.fail(new AssetCatalogError("INVALID_REQUEST")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.repository.saveWorkstation(company.data, (context) => {
|
|
|
|
|
const workstation = context.workstations.find((item) => item.id.equals(identity.data));
|
|
|
|
|
|
|
|
|
|
return workstation
|
|
|
|
|
? workstation.decommission(actor.data, new Date().toISOString())
|
|
|
|
|
: Result.fail(new AssetCatalogError("WORKSTATION_NOT_FOUND"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
save(companyId: string, id: string | undefined, input: WorkstationInput, actorId: string) {
|
|
|
|
|
const company = UniqueID.create(companyId);
|
|
|
|
|
const actor = UniqueID.create(actorId);
|
|
|
|
|
const identity = id ? UniqueID.create(id) : undefined;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
if (!(company.isSuccess && actor.isSuccess) || (identity && !identity.isSuccess)) {
|
|
|
|
|
return Promise.resolve(Result.fail(new AssetCatalogError("INVALID_REQUEST")));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
return this.repository.saveWorkstation(company.data, (context) => {
|
|
|
|
|
const previous = identity?.isSuccess
|
|
|
|
|
? context.workstations.find((item) => item.id.equals(identity.data))
|
|
|
|
|
: undefined;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
if (id && !previous) {
|
|
|
|
|
return Result.fail(new AssetCatalogError("WORKSTATION_NOT_FOUND"));
|
|
|
|
|
}
|
|
|
|
|
const values = mapWorkstationInput(input, context, previous);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
if (!values.isSuccess) {
|
|
|
|
|
return Result.fail(values.error);
|
|
|
|
|
}
|
|
|
|
|
const now = new Date().toISOString();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-04 19:04:14 +00:00
|
|
|
return previous
|
|
|
|
|
? previous.update(values.data, actor.data, now)
|
|
|
|
|
: Workstation.create(company.data, values.data, actor.data, now);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|