46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
|
|
import { AggregateRoot, Name, type UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import { SupportError, type SupportToolInput, SupportToolInputSchema } from "./support-values";
|
||
|
|
|
||
|
|
type Props = Readonly<{ name: Name; active: boolean; builtin: boolean }>;
|
||
|
|
export class SupportTool extends AggregateRoot<Props> {
|
||
|
|
private constructor(props: Props, id?: UniqueID) {
|
||
|
|
super(Object.freeze({ ...props }), id);
|
||
|
|
}
|
||
|
|
static create(input: SupportToolInput, builtin = false): Result<SupportTool, SupportError> {
|
||
|
|
const name = Name.create(input.name, 100);
|
||
|
|
if (!(SupportToolInputSchema.safeParse(input).success && name.isSuccess)) {
|
||
|
|
return Result.fail(new SupportError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
return Result.ok(new SupportTool({ name: name.data, active: input.active, builtin }));
|
||
|
|
}
|
||
|
|
static rehydrate(props: Props, id: UniqueID) {
|
||
|
|
return new SupportTool(props, id);
|
||
|
|
}
|
||
|
|
get name() {
|
||
|
|
return this.props.name;
|
||
|
|
}
|
||
|
|
get active() {
|
||
|
|
return this.props.active;
|
||
|
|
}
|
||
|
|
get builtin() {
|
||
|
|
return this.props.builtin;
|
||
|
|
}
|
||
|
|
update(input: SupportToolInput): Result<SupportTool, SupportError> {
|
||
|
|
const name = Name.create(input.name, 100);
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
return Result.ok(
|
||
|
|
new SupportTool({ ...this.props, name: name.data, active: input.active }, this.id)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|