for merger
This commit is contained in:
@@ -47,7 +47,10 @@ export interface CreateUserPayload {
|
||||
name: string;
|
||||
password: string;
|
||||
phone: string;
|
||||
role: string;
|
||||
/** Role code (e.g. "operator"). If both role and roleId are provided, roleId takes precedence. */
|
||||
role?: string;
|
||||
/** Role ID. Takes precedence over `role` code when both are present. */
|
||||
roleId?: number;
|
||||
socialMedia: SocialMedia;
|
||||
username: string;
|
||||
}
|
||||
@@ -61,11 +64,19 @@ export interface UpdateUserPayload {
|
||||
lightMode?: number;
|
||||
name?: string;
|
||||
phone?: string;
|
||||
/** Role code. If both role and roleId are provided, roleId takes precedence. */
|
||||
role?: string;
|
||||
/** Role ID. Takes precedence over `role` code when both are present. */
|
||||
roleId?: number;
|
||||
socialMedia?: SocialMedia;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
/** Payload for PUT /users/:id/role */
|
||||
export interface UpdateUserRolePayload {
|
||||
roleId: number;
|
||||
}
|
||||
|
||||
// ─── API Functions ────────────────────────────────────────────────────────────
|
||||
|
||||
export const usersApi = {
|
||||
@@ -103,4 +114,10 @@ export const usersApi = {
|
||||
deleteUser: async (id: number): Promise<void> => {
|
||||
await apiClient.delete(`/users/${id}`);
|
||||
},
|
||||
|
||||
/** PUT /users/:id/role — dedicated endpoint to change a user's role */
|
||||
updateUserRole: async (id: number, payload: UpdateUserRolePayload): Promise<User> => {
|
||||
const { data } = await apiClient.put<User>(`/users/${id}/role`, payload);
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -39,13 +39,13 @@ export interface ClientRole {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
scope: "own" | "branch" | "all";
|
||||
isSuper: boolean;
|
||||
}
|
||||
|
||||
export interface ClientPermission {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
}
|
||||
|
||||
export interface ClientBranch {
|
||||
|
||||
@@ -44,13 +44,13 @@ export interface OperatorRole {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
scope: "own" | "branch" | "all";
|
||||
isSuper: boolean;
|
||||
}
|
||||
|
||||
export interface OperatorPermission {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
}
|
||||
|
||||
export interface OperatorBranch {
|
||||
@@ -104,7 +104,10 @@ export interface CreateOperatorPayload {
|
||||
name: string;
|
||||
password: string;
|
||||
phone: string;
|
||||
role: string;
|
||||
/** Role code. If both role and roleId are provided, roleId takes precedence. */
|
||||
role?: string;
|
||||
/** Role ID. Takes precedence over `role` code when both are present. */
|
||||
roleId?: number;
|
||||
socialMedia: SocialMedia;
|
||||
username: string;
|
||||
}
|
||||
@@ -118,11 +121,19 @@ export interface UpdateOperatorPayload {
|
||||
lightMode?: number;
|
||||
name?: string;
|
||||
phone?: string;
|
||||
/** Role code. If both role and roleId are provided, roleId takes precedence. */
|
||||
role?: string;
|
||||
/** Role ID. Takes precedence over `role` code when both are present. */
|
||||
roleId?: number;
|
||||
socialMedia?: SocialMedia;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
/** Payload for PUT /users/:id/role */
|
||||
export interface UpdateOperatorRolePayload {
|
||||
roleId: number;
|
||||
}
|
||||
|
||||
// ─── API Functions ────────────────────────────────────────────────────────────
|
||||
|
||||
export const operatorsApi = {
|
||||
@@ -158,4 +169,10 @@ export const operatorsApi = {
|
||||
deleteOperator: async (id: number): Promise<void> => {
|
||||
await apiClient.delete(`/users/${id}`);
|
||||
},
|
||||
|
||||
/** PUT /users/:id/role — dedicated endpoint to change an operator's role */
|
||||
updateOperatorRole: async (id: number, payload: UpdateOperatorRolePayload): Promise<Operator> => {
|
||||
const { data } = await apiClient.put<Operator>(`/users/${id}/role`, payload);
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -2,13 +2,10 @@ import apiClient from "@/lib/api/client";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Simplified permission — just a code+name catalogue entry, no id/guardName. */
|
||||
export interface Permission {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface PermissionsListResponse {
|
||||
@@ -22,16 +19,10 @@ export interface PermissionsListParams {
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface PermissionPayload {
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
}
|
||||
|
||||
// ─── API functions ────────────────────────────────────────────────────────────
|
||||
|
||||
export const permissionsApi = {
|
||||
/** GET /permissions — backend filters by `permission_name` */
|
||||
/** GET /permissions — returns a catalogue of [{code, name}] */
|
||||
getAll: async (params: PermissionsListParams = {}, signal?: AbortSignal): Promise<PermissionsListResponse> => {
|
||||
const { search, page, limit } = params;
|
||||
const { data } = await apiClient.get<PermissionsListResponse>("/permissions", {
|
||||
@@ -40,27 +31,4 @@ export const permissionsApi = {
|
||||
});
|
||||
return data;
|
||||
},
|
||||
|
||||
/** GET /permissions/:id */
|
||||
getById: async (id: number): Promise<Permission> => {
|
||||
const { data } = await apiClient.get<Permission>(`/permissions/${id}`);
|
||||
return data;
|
||||
},
|
||||
|
||||
/** POST /permissions */
|
||||
create: async (payload: PermissionPayload): Promise<Permission> => {
|
||||
const { data } = await apiClient.post<Permission>("/permissions", payload);
|
||||
return data;
|
||||
},
|
||||
|
||||
/** PUT /permissions/:id */
|
||||
update: async (id: number, payload: PermissionPayload): Promise<Permission> => {
|
||||
const { data } = await apiClient.put<Permission>(`/permissions/${id}`, payload);
|
||||
return data;
|
||||
},
|
||||
|
||||
/** DELETE /permissions/:id */
|
||||
delete: async (id: number): Promise<void> => {
|
||||
await apiClient.delete(`/permissions/${id}`);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { useQuery, useMutation, useQueryClient, useInfiniteQuery } from "@tanstack/react-query";
|
||||
import { toast } from "sonner";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useQuery, useInfiniteQuery } from "@tanstack/react-query";
|
||||
import { useDebouncedValue, normalizeSearch } from "@/hooks/useDebouncedSearch";
|
||||
import { permissionsApi, type PermissionPayload, type PermissionsListParams } from "../api/permissionsApi";
|
||||
import { permissionsApi, type PermissionsListParams } from "../api/permissionsApi";
|
||||
|
||||
// ─── Query keys ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -10,7 +8,6 @@ export const permissionKeys = {
|
||||
all: ["permissions"] as const,
|
||||
lists: () => ["permissions", "list"] as const,
|
||||
list: (params: PermissionsListParams) => ["permissions", "list", params] as const,
|
||||
detail: (id: number) => ["permissions", "detail", id] as const,
|
||||
infinite: (search: string) => ["permissions", "infinite", search] as const,
|
||||
};
|
||||
|
||||
@@ -27,16 +24,6 @@ export function usePermissions(params: PermissionsListParams) {
|
||||
});
|
||||
}
|
||||
|
||||
// ─── usePermission (single) ───────────────────────────────────────────────────
|
||||
|
||||
export function usePermission(id: number) {
|
||||
return useQuery({
|
||||
queryKey: permissionKeys.detail(id),
|
||||
queryFn: () => permissionsApi.getById(id),
|
||||
enabled: !!id,
|
||||
});
|
||||
}
|
||||
|
||||
// ─── useInfinitePermissions ───────────────────────────────────────────────────
|
||||
|
||||
const PERMISSION_PAGE_SIZE = 25;
|
||||
@@ -63,49 +50,3 @@ export function useInfinitePermissions(search = "", enabled = true) {
|
||||
enabled,
|
||||
});
|
||||
}
|
||||
|
||||
// ─── useCreatePermission ──────────────────────────────────────────────────────
|
||||
|
||||
export function useCreatePermission() {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: PermissionPayload) => permissionsApi.create(payload),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: permissionKeys.all });
|
||||
toast.success(t("permissions.toast.created", "Rugsat üstünlikli döredildi!"));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// ─── useUpdatePermission ──────────────────────────────────────────────────────
|
||||
|
||||
export function useUpdatePermission(id: number) {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: PermissionPayload) => permissionsApi.update(id, payload),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: permissionKeys.all });
|
||||
queryClient.invalidateQueries({ queryKey: permissionKeys.detail(id) });
|
||||
toast.success(t("permissions.toast.updated", "Rugsat üstünlikli üýtgedildi!"));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// ─── useDeletePermission ──────────────────────────────────────────────────────
|
||||
|
||||
export function useDeletePermission() {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => permissionsApi.delete(id),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: permissionKeys.all });
|
||||
toast.success(t("permissions.toast.deleted", "Rugsat üstünlikli pozuldy!"));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,11 +2,18 @@ import apiClient from "@/lib/api/client";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Scope of a role: 'own' | 'branch' | 'all' */
|
||||
export type RoleScope = "own" | "branch" | "all";
|
||||
|
||||
export interface Role {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
scope: RoleScope;
|
||||
isSuper: boolean;
|
||||
isSystem: boolean;
|
||||
permissions: string[];
|
||||
usersCount: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
@@ -25,7 +32,14 @@ export interface RolesListParams {
|
||||
export interface RolePayload {
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
scope: RoleScope;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
export interface UpdateRolePayload {
|
||||
name: string;
|
||||
scope: RoleScope;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
// ─── API functions ────────────────────────────────────────────────────────────
|
||||
@@ -47,19 +61,26 @@ export const rolesApi = {
|
||||
return data;
|
||||
},
|
||||
|
||||
/** POST /roles */
|
||||
/** POST /roles — { code, name, scope, permissions: string[] } */
|
||||
create: async (payload: RolePayload): Promise<Role> => {
|
||||
const { data } = await apiClient.post<Role>("/roles", payload);
|
||||
return data;
|
||||
},
|
||||
|
||||
/** PUT /roles/:id */
|
||||
update: async (id: number, payload: RolePayload): Promise<Role> => {
|
||||
/**
|
||||
* PUT /roles/:id — { name, scope, permissions }
|
||||
* Note: `code` cannot be changed via update.
|
||||
* `isSystem: true` roles cannot have their scope changed (enforce in UI).
|
||||
*/
|
||||
update: async (id: number, payload: UpdateRolePayload): Promise<Role> => {
|
||||
const { data } = await apiClient.put<Role>(`/roles/${id}`, payload);
|
||||
return data;
|
||||
},
|
||||
|
||||
/** DELETE /roles/:id */
|
||||
/**
|
||||
* DELETE /roles/:id
|
||||
* Note: `isSystem: true` roles (e.g. superadmin, user) cannot be deleted (enforce in UI).
|
||||
*/
|
||||
delete: async (id: number): Promise<void> => {
|
||||
await apiClient.delete(`/roles/${id}`);
|
||||
},
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import { useMemo, useEffect, useCallback } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useForm, Controller } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { FormActions } from "@/components/formActions";
|
||||
import { BentoGrid, BentoCard } from "@/components/bento";
|
||||
import { useCreateRole, useUpdateRole } from "../hooks/useRoles";
|
||||
import { createRoleFormSchema, DEFAULT_FORM_VALUES, buildPayload } from "../schemas/role.schema";
|
||||
import { createRoleFormSchema, DEFAULT_FORM_VALUES, buildCreatePayload, buildUpdatePayload, SCOPE_OPTIONS } from "../schemas/role.schema";
|
||||
import type { RoleFormData } from "../schemas/role.schema";
|
||||
import type { Role } from "../api/rolesApi";
|
||||
import { FormInput } from "@/components/formInput";
|
||||
import { useInfinitePermissions } from "@/features/permissions/hooks/usePermissions";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -46,10 +51,15 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
|
||||
const schema = useMemo(() => createRoleFormSchema(t), [t, i18n.language]);
|
||||
|
||||
// ── All available permissions (for the multi-select) ───────────────────────
|
||||
const { data: permInfinite } = useInfinitePermissions("", true);
|
||||
const allPermissions = useMemo(() => permInfinite?.pages.flatMap((p) => p.permissions) ?? [], [permInfinite]);
|
||||
|
||||
const {
|
||||
watch,
|
||||
setValue,
|
||||
getValues,
|
||||
control,
|
||||
formState: { errors: rhfErrors },
|
||||
clearErrors,
|
||||
trigger,
|
||||
@@ -60,7 +70,8 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
...DEFAULT_FORM_VALUES,
|
||||
code: initialData.code,
|
||||
name: initialData.name,
|
||||
guardName: initialData.guardName,
|
||||
scope: initialData.scope,
|
||||
permissions: initialData.permissions ?? [],
|
||||
}
|
||||
: DEFAULT_FORM_VALUES,
|
||||
});
|
||||
@@ -82,6 +93,16 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [i18n.language]);
|
||||
|
||||
// ── Toggle a permission code in/out of the selected list ──
|
||||
const togglePermission = useCallback(
|
||||
(code: string) => {
|
||||
const current = getValues("permissions") ?? [];
|
||||
const next = current.includes(code) ? current.filter((c) => c !== code) : [...current, code];
|
||||
setValue("permissions", next);
|
||||
},
|
||||
[getValues, setValue],
|
||||
);
|
||||
|
||||
// ── Submit ─────────────────────────────────────────────────────────────────
|
||||
|
||||
const handleSubmit = async () => {
|
||||
@@ -89,14 +110,13 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
if (!isValid) return;
|
||||
|
||||
const values = getValues();
|
||||
const payload = buildPayload(values);
|
||||
|
||||
if (mode === "create") {
|
||||
createRole.mutate(payload, {
|
||||
createRole.mutate(buildCreatePayload(values), {
|
||||
onSuccess: () => navigate("/settings/users/roles"),
|
||||
});
|
||||
} else {
|
||||
updateRole.mutate(payload, {
|
||||
updateRole.mutate(buildUpdatePayload(values), {
|
||||
onSuccess: () => navigate("/settings/users/roles"),
|
||||
});
|
||||
}
|
||||
@@ -104,12 +124,19 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
|
||||
// ── Render ─────────────────────────────────────────────────────────────────
|
||||
|
||||
const scopeLabels: Record<string, string> = {
|
||||
own: t("roles.scope.own", "Öz ýazgylary"),
|
||||
branch: t("roles.scope.branch", "Şahamça"),
|
||||
all: t("roles.scope.all", "Hemmesi"),
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-5">
|
||||
<h1 className="text-xl font-semibold text-foreground">
|
||||
{mode === "create" ? t("roles.createTitle", "Rol dörediň") : t("roles.editTitle", "Rol üýtgetmek")}
|
||||
</h1>
|
||||
<BentoGrid cols={2}>
|
||||
{/* Code — only editable on create */}
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
@@ -118,23 +145,13 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
onChange={setField("code")}
|
||||
placeholder={t("roles.fields.code", "Kod")}
|
||||
error={errors.code}
|
||||
disabled={isPending}
|
||||
disabled={isPending || mode === "edit"}
|
||||
required
|
||||
/>
|
||||
</BentoCard>
|
||||
|
||||
{/* Name */}
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("roles.fields.guardName", "Guard name")}
|
||||
value={form.guardName}
|
||||
onChange={setField("guardName")}
|
||||
placeholder="operator"
|
||||
error={errors.guardName}
|
||||
disabled={isPending}
|
||||
required
|
||||
/>
|
||||
</BentoCard>
|
||||
<BentoCard span="full">
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("roles.fields.name", "Ady")}
|
||||
@@ -146,7 +163,71 @@ export function RoleForm({ mode, initialData }: RoleFormProps) {
|
||||
required
|
||||
/>
|
||||
</BentoCard>
|
||||
|
||||
{/* Scope */}
|
||||
<BentoCard span="full">
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label className="text-sm font-medium">
|
||||
{t("roles.fields.scope", "Scope")}
|
||||
<span className="text-destructive ml-1">*</span>
|
||||
</Label>
|
||||
<Controller
|
||||
control={control}
|
||||
name="scope"
|
||||
render={({ field }) => (
|
||||
<Select value={field.value} onValueChange={field.onChange} disabled={isPending}>
|
||||
<SelectTrigger className={cn(errors.scope && "border-destructive")}>
|
||||
<SelectValue placeholder={t("roles.fields.scope", "Scope")} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{SCOPE_OPTIONS.map((s) => (
|
||||
<SelectItem key={s} value={s}>
|
||||
{scopeLabels[s]}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)}
|
||||
/>
|
||||
{errors.scope && <p className="text-destructive text-xs">{errors.scope}</p>}
|
||||
</div>
|
||||
</BentoCard>
|
||||
|
||||
{/* Permissions multi-select */}
|
||||
<BentoCard span="full">
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label className="text-sm font-medium">{t("roles.fields.permissions", "Rugsatlar")}</Label>
|
||||
{allPermissions.length === 0 ? (
|
||||
<p className="text-muted-foreground text-sm">{t("common.loading", "Ýüklenýär...")}</p>
|
||||
) : (
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-2">
|
||||
{allPermissions.map((perm) => {
|
||||
const checked = (form.permissions ?? []).includes(perm.code);
|
||||
return (
|
||||
<label
|
||||
key={perm.code}
|
||||
className={cn(
|
||||
"flex items-center gap-2 rounded-lg border p-2.5 cursor-pointer transition-colors",
|
||||
checked ? "border-primary bg-primary/5 text-primary" : "border-border hover:border-primary/50",
|
||||
isPending && "opacity-60 pointer-events-none",
|
||||
)}
|
||||
>
|
||||
<Checkbox
|
||||
checked={checked}
|
||||
onCheckedChange={() => togglePermission(perm.code)}
|
||||
disabled={isPending}
|
||||
id={`perm-${perm.code}`}
|
||||
/>
|
||||
<span className="text-sm font-medium">{perm.code}</span>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</BentoCard>
|
||||
</BentoGrid>
|
||||
|
||||
<FormActions
|
||||
isPending={isPending}
|
||||
onSubmit={handleSubmit}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useQuery, useMutation, useQueryClient, useInfiniteQuery } from "@tansta
|
||||
import { toast } from "sonner";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useDebouncedValue, normalizeSearch } from "@/hooks/useDebouncedSearch";
|
||||
import { rolesApi, type RolePayload, type RolesListParams } from "../api/rolesApi";
|
||||
import { rolesApi, type RolePayload, type UpdateRolePayload, type RolesListParams } from "../api/rolesApi";
|
||||
|
||||
// ─── Query keys ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -46,6 +46,7 @@ export function useInfiniteRoles(search = "", enabled = true) {
|
||||
const count = lastPage?.count ?? 0;
|
||||
return loaded < count ? allPages.length + 1 : undefined;
|
||||
},
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
enabled,
|
||||
});
|
||||
}
|
||||
@@ -82,7 +83,7 @@ export function useUpdateRole(id: number) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: RolePayload) => rolesApi.update(id, payload),
|
||||
mutationFn: (payload: UpdateRolePayload) => rolesApi.update(id, payload),
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: roleKeys.all });
|
||||
queryClient.invalidateQueries({ queryKey: roleKeys.detail(data.id) });
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import { z } from "zod";
|
||||
import type { RolePayload } from "../api/rolesApi";
|
||||
import type { RolePayload, UpdateRolePayload, RoleScope } from "../api/rolesApi";
|
||||
|
||||
const SCOPE_OPTIONS: RoleScope[] = ["own", "branch", "all"];
|
||||
|
||||
export function createRoleFormSchema(t: (key: string, fallback?: string) => string) {
|
||||
return z.object({
|
||||
code: z.string().min(1, t("validation.required", "validation.required")),
|
||||
name: z.string().min(1, t("validation.required", "validation.required")),
|
||||
guardName: z.string().min(1, t("validation.required", "validation.required")),
|
||||
scope: z.enum(["own", "branch", "all"], {
|
||||
message: t("validation.required", "validation.required"),
|
||||
}),
|
||||
permissions: z.array(z.string()),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,13 +19,25 @@ export type RoleFormData = z.infer<ReturnType<typeof createRoleFormSchema>>;
|
||||
export const DEFAULT_FORM_VALUES: RoleFormData = {
|
||||
code: "",
|
||||
name: "",
|
||||
guardName: "operator",
|
||||
scope: "branch",
|
||||
permissions: [],
|
||||
};
|
||||
|
||||
export function buildPayload(data: RoleFormData): RolePayload {
|
||||
export { SCOPE_OPTIONS };
|
||||
|
||||
export function buildCreatePayload(data: RoleFormData): RolePayload {
|
||||
return {
|
||||
code: data.code,
|
||||
name: data.name,
|
||||
guardName: data.guardName,
|
||||
scope: data.scope,
|
||||
permissions: data.permissions,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildUpdatePayload(data: RoleFormData): UpdateRolePayload {
|
||||
return {
|
||||
name: data.name,
|
||||
scope: data.scope,
|
||||
permissions: data.permissions,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user