55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
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";
|
|
import {
|
|
requiresAvailableSoftware,
|
|
softwareAvailable,
|
|
softwareLicenseAvailable,
|
|
softwareReferences,
|
|
} from "./software-installation-policy";
|
|
|
|
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 licenseChanged =
|
|
previous?.values.licenseResourceId.getOrUndefined()?.toPrimitive() !==
|
|
values.licenseResourceId.getOrUndefined()?.toPrimitive();
|
|
|
|
if (licenseChanged && !softwareLicenseAvailable(values, context)) {
|
|
return Result.fail(new SoftwareInstallationError("SOFTWARE_REFERENCE_UNAVAILABLE"));
|
|
}
|
|
|
|
if (requiresAvailableSoftware(values, previous) && !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;
|
|
}
|