metis-desk/modules/assets/src/api/domain/support-tool.aggregate.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-10-08 14:43:34 +00:00
import type { UniqueID } from "@repo/rdx-ddd";
import { AggregateRoot, Name } from "@repo/rdx-ddd";
2026-10-07 08:16:39 +00:00
import { Result } from "@repo/rdx-utils";
2026-10-08 14:43:34 +00:00
import type { SupportToolInput } from "./support-values";
import { SupportError, SupportToolInputSchema } from "./support-values";
2026-10-07 08:16:39 +00:00
type Props = Readonly<{ name: Name; active: boolean; builtin: boolean }>;
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
export class SupportTool extends AggregateRoot<Props> {
private constructor(props: Props, id?: UniqueID) {
super(Object.freeze({ ...props }), id);
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
static create(input: SupportToolInput, builtin = false): Result<SupportTool, SupportError> {
const name = Name.create(input.name, 100);
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
if (!(SupportToolInputSchema.safeParse(input).success && name.isSuccess)) {
return Result.fail(new SupportError("INVALID_REQUEST"));
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
return Result.ok(new SupportTool({ name: name.data, active: input.active, builtin }));
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
static rehydrate(props: Props, id: UniqueID) {
return new SupportTool(props, id);
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
get name() {
return this.props.name;
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
get active() {
return this.props.active;
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
get builtin() {
return this.props.builtin;
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
update(input: SupportToolInput): Result<SupportTool, SupportError> {
const name = Name.create(input.name, 100);
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
if (!(SupportToolInputSchema.safeParse(input).success && name.isSuccess)) {
return Result.fail(new SupportError("INVALID_REQUEST"));
}
if (this.builtin && !this.name.equals(name.data)) {
return Result.fail(new SupportError("BUILTIN_PROTECTED"));
}
if (this.name.equals(name.data) && this.active === input.active) {
return Result.ok(this);
}
2026-10-10 10:01:58 +00:00
2026-10-07 08:16:39 +00:00
return Result.ok(
new SupportTool({ ...this.props, name: name.data, active: input.active }, this.id)
);
}
}