- Changed asset status label from "status" to "technicalStatus" in AssetForm component. - Added help text for technical status in AssetForm. - Updated test cases to reflect changes in asset status from "available" to "operational". - Enhanced workstation form to utilize new asset status checks and display appropriate messages. - Refactored asset context to replace "available" with "notRetired" across various application policies and repositories. - Introduced new utility functions for asset status management, including technical status determination and assignment validation. - Added integration tests for asset state verification and unlinking assets from workstations.
122 lines
3.6 KiB
TypeScript
122 lines
3.6 KiB
TypeScript
import { Result } from "@repo/rdx-utils";
|
|
|
|
import { WorkProfileValues } from "../domain/value-objects/work-profile-values.vo";
|
|
import type { WorkProfile } from "../domain/work-profile.aggregate";
|
|
import type { WorkProfileInput } from "../domain/work-profile-values";
|
|
import { WorkProfileError } from "../domain/work-profile-values";
|
|
|
|
import type { WorkProfileContext } from "./work-profile.repository";
|
|
|
|
export function profileReferencesAvailable(values: WorkProfileValues, context: WorkProfileContext) {
|
|
if (
|
|
!(
|
|
context.people.some((person) => person.id.equals(values.personId) && person.active) &&
|
|
context.assets.some(
|
|
(asset) =>
|
|
asset.id.equals(values.assetId) && asset.notRetired && asset.isProfileHost === true
|
|
)
|
|
)
|
|
) {
|
|
return false;
|
|
}
|
|
const accountId = values.accountId.getOrUndefined();
|
|
|
|
if (!accountId) {
|
|
return true;
|
|
}
|
|
const account = context.items.find((item) => item.id.equals(accountId))?.values;
|
|
|
|
if (
|
|
!account ||
|
|
account.kind !== "account" ||
|
|
!account.active ||
|
|
account.usage === "technical" ||
|
|
(account.expiresOn && account.expiresOn < context.today)
|
|
) {
|
|
return false;
|
|
}
|
|
const system = context.items.find((item) => item.id.toPrimitive() === account.systemId)?.values;
|
|
const environment = account.environmentId
|
|
? context.items.find((item) => item.id.toPrimitive() === account.environmentId)?.values
|
|
: undefined;
|
|
|
|
if (
|
|
!system ||
|
|
system.kind !== "system" ||
|
|
!system.active ||
|
|
(account.environmentId &&
|
|
(!environment || environment.kind !== "environment" || !environment.active))
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return context.items.some((item) => {
|
|
const link = item.values;
|
|
|
|
return (
|
|
link.kind === "account_person" &&
|
|
link.personId === values.personId.toPrimitive() &&
|
|
link.accountId === accountId.toPrimitive() &&
|
|
link.startDate <= context.today &&
|
|
(link.endDate === null || context.today < link.endDate) &&
|
|
(link.configuration === "pending" || link.configuration === "configured")
|
|
);
|
|
});
|
|
}
|
|
|
|
export function mapWorkProfileInput(
|
|
input: WorkProfileInput,
|
|
context: WorkProfileContext,
|
|
previous?: WorkProfile
|
|
) {
|
|
const mapped = WorkProfileValues.create(input);
|
|
|
|
if (!mapped.isSuccess) {
|
|
return mapped;
|
|
}
|
|
const values = mapped.data;
|
|
|
|
if (previous && !previous.values.sameTarget(values)) {
|
|
return Result.fail(new WorkProfileError("PROFILE_TARGET_IMMUTABLE"));
|
|
}
|
|
if (
|
|
!(
|
|
context.people.some((person) => person.id.equals(values.personId)) &&
|
|
context.assets.some((asset) => asset.id.equals(values.assetId))
|
|
)
|
|
) {
|
|
return Result.fail(new WorkProfileError("PROFILE_REFERENCE_NOT_FOUND"));
|
|
}
|
|
const accountId = values.accountId.getOrUndefined();
|
|
|
|
if (
|
|
accountId &&
|
|
!context.items.some((item) => item.id.equals(accountId) && item.values.kind === "account")
|
|
) {
|
|
return Result.fail(new WorkProfileError("PROFILE_REFERENCE_NOT_FOUND"));
|
|
}
|
|
const changingAccount =
|
|
previous?.values.toPrimitive().accountId !== values.toPrimitive().accountId;
|
|
|
|
if (
|
|
values.active &&
|
|
(!(previous && previous.values.active) || changingAccount) &&
|
|
!profileReferencesAvailable(values, context)
|
|
) {
|
|
return Result.fail(new WorkProfileError("PROFILE_REFERENCE_UNAVAILABLE"));
|
|
}
|
|
if (
|
|
values.active &&
|
|
context.profiles.some(
|
|
(item) =>
|
|
item.id.toPrimitive() !== previous?.id.toPrimitive() &&
|
|
item.values.active &&
|
|
item.values.sameTarget(values)
|
|
)
|
|
) {
|
|
return Result.fail(new WorkProfileError("PROFILE_ALREADY_ACTIVE"));
|
|
}
|
|
|
|
return mapped;
|
|
}
|