2025-02-01 21:48:13 +00:00
|
|
|
import { Result, ValueObject } from "@common/domain";
|
2025-01-29 19:02:59 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
|
|
const RoleSchema = z.enum(["Admin", "User", "Manager", "Editor"]);
|
|
|
|
|
|
|
|
|
|
export class UserRoles extends ValueObject<string[]> {
|
|
|
|
|
static create(roles: string[]): Result<UserRoles, Error> {
|
|
|
|
|
const result = UserRoles.validate(roles);
|
|
|
|
|
|
|
|
|
|
return result.success
|
|
|
|
|
? Result.ok(new UserRoles(result.data))
|
|
|
|
|
: Result.fail(new Error("Invalid user roles"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static validate(roles: string[]) {
|
|
|
|
|
return z.array(RoleSchema).safeParse(roles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasRole(role: string): boolean {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.includes(role);
|
2025-01-29 19:02:59 +00:00
|
|
|
}
|
|
|
|
|
}
|