Uecko_ERP/apps/server/archive/contexts/auth/domain/value-objects/auth-user-roles.ts
2025-05-09 12:45:32 +02:00

36 lines
833 B
TypeScript

import { ValueObject } from "@/core/common/domain";
import { Result } from "@repo/rdx-utils";
import { z } from "zod";
const RoleSchema = z.enum(["Admin", "User", "Manager", "Editor"]);
interface UserRolesProps {
value: string[];
}
export class UserRoles extends ValueObject<UserRolesProps> {
static create(roles: string[]): Result<UserRoles, Error> {
const result = UserRoles.validate(roles);
return result.success
? Result.ok(new UserRoles({ value: result.data }))
: Result.fail(new Error("Invalid user roles"));
}
private static validate(roles: string[]) {
return z.array(RoleSchema).safeParse(roles);
}
hasRole(role: string): boolean {
return this.props.value.includes(role);
}
getValue() {
return this.props.value;
}
toPrimitive() {
return this.props.value;
}
}