2026-10-12 18:31:21 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
|
|
|
|
|
import type { SoftwareInstallation } from "../domain/software-installation.aggregate";
|
|
|
|
|
import type { SoftwareInstallationInput } from "../domain/software-installation-values";
|
|
|
|
|
import { SoftwareInstallationError } from "../domain/software-installation-values";
|
|
|
|
|
import { SoftwareInstallationValues } from "../domain/value-objects/software-installation-values.vo";
|
|
|
|
|
|
|
|
|
|
import type { SoftwareContext } from "./software-installation.repository";
|
2026-10-13 07:30:28 +00:00
|
|
|
import {
|
|
|
|
|
softwareAvailable,
|
|
|
|
|
softwareLicenseAvailable,
|
|
|
|
|
softwareReferences,
|
|
|
|
|
} from "./software-installation-policy";
|
2026-10-12 18:31:21 +00:00
|
|
|
|
|
|
|
|
export function mapSoftwareInput(
|
|
|
|
|
input: SoftwareInstallationInput,
|
|
|
|
|
context: SoftwareContext,
|
|
|
|
|
previous?: SoftwareInstallation
|
|
|
|
|
) {
|
|
|
|
|
const parsed = SoftwareInstallationValues.create(input);
|
|
|
|
|
|
|
|
|
|
if (!parsed.isSuccess) {
|
|
|
|
|
return parsed;
|
|
|
|
|
}
|
|
|
|
|
const values = parsed.data;
|
|
|
|
|
|
|
|
|
|
if (previous && !previous.values.sameTarget(values)) {
|
|
|
|
|
return Result.fail(new SoftwareInstallationError("SOFTWARE_TARGET_IMMUTABLE"));
|
|
|
|
|
}
|
|
|
|
|
if (!softwareReferences(values, context)) {
|
|
|
|
|
return Result.fail(new SoftwareInstallationError("SOFTWARE_REFERENCE_NOT_FOUND"));
|
|
|
|
|
}
|
|
|
|
|
const reconfiguring =
|
|
|
|
|
!previous ||
|
|
|
|
|
previous.values.state !== values.state ||
|
|
|
|
|
previous.values.version.getOrUndefined() !== values.version.getOrUndefined();
|
2026-10-13 07:30:28 +00:00
|
|
|
const licenseChanged =
|
|
|
|
|
previous?.values.licenseResourceId.getOrUndefined()?.toPrimitive() !==
|
|
|
|
|
values.licenseResourceId.getOrUndefined()?.toPrimitive();
|
|
|
|
|
|
|
|
|
|
if (licenseChanged && !softwareLicenseAvailable(values, context)) {
|
|
|
|
|
return Result.fail(new SoftwareInstallationError("SOFTWARE_REFERENCE_UNAVAILABLE"));
|
|
|
|
|
}
|
2026-10-12 18:31:21 +00:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
["pending", "configured"].includes(values.state) &&
|
|
|
|
|
reconfiguring &&
|
|
|
|
|
!softwareAvailable(values, context)
|
|
|
|
|
) {
|
|
|
|
|
return Result.fail(new SoftwareInstallationError("SOFTWARE_REFERENCE_UNAVAILABLE"));
|
|
|
|
|
}
|
|
|
|
|
const others = context.configurations.filter(
|
|
|
|
|
(item) => !(item.id.equals(previous?.id) || item.values.closed)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!values.closed && others.some((item) => item.values.sameTarget(values))) {
|
|
|
|
|
return Result.fail(new SoftwareInstallationError("SOFTWARE_ALREADY_OPEN"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
}
|