2026-10-05 07:53:29 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
|
2026-10-09 08:38:19 +00:00
|
|
|
import { WorkProfileValues } from "../domain/value-objects/work-profile-values.vo";
|
2026-10-05 07:53:29 +00:00
|
|
|
import type { WorkProfile } from "../domain/work-profile.aggregate";
|
2026-10-08 14:43:34 +00:00
|
|
|
import type { WorkProfileInput } from "../domain/work-profile-values";
|
|
|
|
|
import { WorkProfileError } from "../domain/work-profile-values";
|
2026-10-05 07:53:29 +00:00
|
|
|
|
|
|
|
|
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.available)
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const accountId = values.accountId.getOrUndefined();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
if (!accountId) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const account = context.items.find((item) => item.id.equals(accountId))?.values;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
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;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
if (
|
|
|
|
|
!system ||
|
|
|
|
|
system.kind !== "system" ||
|
|
|
|
|
!system.active ||
|
|
|
|
|
(account.environmentId &&
|
|
|
|
|
(!environment || environment.kind !== "environment" || !environment.active))
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
return context.items.some((item) => {
|
|
|
|
|
const link = item.values;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
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")
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
export function mapWorkProfileInput(
|
|
|
|
|
input: WorkProfileInput,
|
|
|
|
|
context: WorkProfileContext,
|
|
|
|
|
previous?: WorkProfile
|
|
|
|
|
) {
|
|
|
|
|
const mapped = WorkProfileValues.create(input);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
if (!mapped.isSuccess) {
|
|
|
|
|
return mapped;
|
|
|
|
|
}
|
|
|
|
|
const values = mapped.data;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
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();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
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;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
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"));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-05 07:53:29 +00:00
|
|
|
return mapped;
|
|
|
|
|
}
|