File size: 1,904 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { z } from "zod";
import { USER_ROLES, UserRoleNames } from "app-types/roles";

import { ActionState } from "lib/action-utils";
import { BasicUserWithLastLogin } from "app-types/user";
import { passwordSchema } from "lib/validations/password";

export const UpdateUserRoleSchema = z.object({
  userId: z.string().uuid("Invalid user ID"),
  role: z
    .enum(Object.values(USER_ROLES) as [UserRoleNames, ...UserRoleNames[]])
    .optional(),
});

export const UpdateUserPasswordError = {
  PASSWORD_MISMATCH: "Passwords do not match",
  CURRENT_PASSWORD_REQUIRED: "Current password is required",
} as const;

export type UpdateUserPasswordError =
  (typeof UpdateUserPasswordError)[keyof typeof UpdateUserPasswordError];

export const UpdateUserDetailsSchema = z.object({
  userId: z.uuid("Invalid user ID"),
  name: z.string().min(1, "Name is required").max(100, "Name is too long"),
  email: z.email("Invalid email address").optional(),
  image: z.string().optional(),
});

export const DeleteUserSchema = z.object({
  userId: z.uuid("Invalid user ID"),
});

export const UpdateUserPasswordSchema = z
  .object({
    userId: z.string().uuid("Invalid user ID"),
    newPassword: passwordSchema,
    confirmPassword: passwordSchema,
    currentPassword: z.string().optional(),
  })
  .superRefine((data, ctx) => {
    if (data.newPassword !== data.confirmPassword) {
      ctx.addIssue({
        code: "custom",
        message: UpdateUserPasswordError.PASSWORD_MISMATCH,
      });
    }
  });

export type UpdateUserRoleActionState = ActionState & {
  user?: BasicUserWithLastLogin | null;
};

export type DeleteUserActionState = ActionState & {
  redirect?: string;
};

export type UpdateUserActionState = ActionState & {
  user?: BasicUserWithLastLogin | null;
  currentUserUpdated?: boolean;
};

export type UpdateUserPasswordActionState = ActionState & {
  error?: UpdateUserPasswordError;
};