Uecko_ERP/apps/server/archive/contexts/auth/domain/value-objects/auth-user-roles.ts

36 lines
833 B
TypeScript
Raw Normal View History

2025-05-02 21:43:51 +00:00
import { ValueObject } from "@/core/common/domain";
2025-05-09 10:45:32 +00:00
import { Result } from "@repo/rdx-utils";
2025-01-29 19:02:59 +00:00
import { z } from "zod";
const RoleSchema = z.enum(["Admin", "User", "Manager", "Editor"]);
2025-02-21 10:06:27 +00:00
interface UserRolesProps {
value: string[];
}
export class UserRoles extends ValueObject<UserRolesProps> {
2025-01-29 19:02:59 +00:00
static create(roles: string[]): Result<UserRoles, Error> {
const result = UserRoles.validate(roles);
return result.success
2025-02-21 10:06:27 +00:00
? Result.ok(new UserRoles({ value: result.data }))
2025-01-29 19:02:59 +00:00
: Result.fail(new Error("Invalid user roles"));
}
private static validate(roles: string[]) {
return z.array(RoleSchema).safeParse(roles);
}
hasRole(role: string): boolean {
2025-02-21 10:06:27 +00:00
return this.props.value.includes(role);
}
getValue() {
return this.props.value;
2025-01-29 19:02:59 +00:00
}
2025-05-04 20:06:57 +00:00
toPrimitive() {
return this.props.value;
}
2025-01-29 19:02:59 +00:00
}