70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import {
|
||
|
|
ApplicationConfigurationError,
|
||
|
|
type ApplicationConfigurationInput,
|
||
|
|
ApplicationConfigurationInputSchema,
|
||
|
|
} from "../application-configuration-values";
|
||
|
|
export class ApplicationConfigurationValues {
|
||
|
|
private constructor(
|
||
|
|
public readonly profileId: UniqueID,
|
||
|
|
public readonly resourceId: UniqueID,
|
||
|
|
public readonly delivery: ApplicationConfigurationInput["delivery"],
|
||
|
|
public readonly version: Maybe<string>,
|
||
|
|
public readonly state: ApplicationConfigurationInput["state"],
|
||
|
|
public readonly notes: Maybe<string>
|
||
|
|
) {
|
||
|
|
Object.freeze(this);
|
||
|
|
}
|
||
|
|
static create(
|
||
|
|
input: ApplicationConfigurationInput
|
||
|
|
): Result<ApplicationConfigurationValues, ApplicationConfigurationError> {
|
||
|
|
const parsed = ApplicationConfigurationInputSchema.safeParse(input);
|
||
|
|
if (!parsed.success) {
|
||
|
|
return Result.fail(new ApplicationConfigurationError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
const data = parsed.data;
|
||
|
|
const profile = UniqueID.create(data.profileId);
|
||
|
|
const resource = UniqueID.create(data.resourceId);
|
||
|
|
if (!(profile.isSuccess && resource.isSuccess) || (data.delivery === "web" && data.version)) {
|
||
|
|
return Result.fail(new ApplicationConfigurationError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
return Result.ok(
|
||
|
|
new ApplicationConfigurationValues(
|
||
|
|
profile.data,
|
||
|
|
resource.data,
|
||
|
|
data.delivery,
|
||
|
|
data.version ? Maybe.some(data.version) : Maybe.none(),
|
||
|
|
data.state,
|
||
|
|
data.notes ? Maybe.some(data.notes) : Maybe.none()
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
get closed() {
|
||
|
|
return this.state === "removed" || this.state === "never_configured";
|
||
|
|
}
|
||
|
|
sameTarget(other: ApplicationConfigurationValues) {
|
||
|
|
return this.profileId.equals(other.profileId) && this.resourceId.equals(other.resourceId);
|
||
|
|
}
|
||
|
|
equals(other: ApplicationConfigurationValues) {
|
||
|
|
return (
|
||
|
|
this.sameTarget(other) &&
|
||
|
|
this.delivery === other.delivery &&
|
||
|
|
this.state === other.state &&
|
||
|
|
this.version.getOrUndefined() === other.version.getOrUndefined() &&
|
||
|
|
this.notes.getOrUndefined() === other.notes.getOrUndefined()
|
||
|
|
);
|
||
|
|
}
|
||
|
|
toPrimitive(): ApplicationConfigurationInput {
|
||
|
|
return {
|
||
|
|
profileId: this.profileId.toPrimitive(),
|
||
|
|
resourceId: this.resourceId.toPrimitive(),
|
||
|
|
delivery: this.delivery,
|
||
|
|
version: this.version.getOrUndefined() ?? null,
|
||
|
|
state: this.state,
|
||
|
|
notes: this.notes.getOrUndefined() ?? null,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|