import z from 'zod'; import { DateFilter, NumberFilter, UserSchema } from '../../models'; import { USER_ROLE, SORT_ORDER, USER_SORT_BY, USER_STATUS } from '../../constants'; export namespace FindUsersByCriteriaCommand { export const RequestSchema = z.preprocess( (input) => { if (input && typeof input === 'object' && !Array.isArray(input)) { const obj = { ...input } as Record; const onlyNormalize = ['createdAt', 'updatedAt', 'tokenBonusBalance']; const relevantBracketed = Object.keys(obj).some((key) => { const match = key.match(/^(\w+)\[(\w+)\]$/); return match && onlyNormalize.includes(match[1]); }); if (!relevantBracketed) { return obj; } const result = { ...obj }; for (const key of Object.keys(obj)) { const match = key.match(/^(\w+)\[(\w+)\]$/); if (match && onlyNormalize.includes(match[1])) { const [, outer, inner] = match; result[outer] = result[outer] || {}; result[outer][inner] = obj[key]; delete result[key]; } } return result; } return input; }, z.object({ uuid: z.string().uuid().optional(), email: z.string().email().optional(), status: z.nativeEnum(USER_STATUS).optional(), role: z.nativeEnum(USER_ROLE).optional(), createdAt: DateFilter.optional(), updatedAt: DateFilter.optional(), deletedAt: DateFilter.optional(), tokenBonusBalance: NumberFilter.optional(), sortBy: z.nativeEnum(USER_SORT_BY).optional(), sortOrder: z.nativeEnum(SORT_ORDER).default(SORT_ORDER.DESC).optional(), limit: z.coerce.number().min(1), offset: z.coerce.number().min(0).default(0), }), ); export type Request = z.infer; export const ResponseSchema = z.object({ data: z.array( UserSchema.omit({ verifyTokenHash: true, password: true, passwordHash: true, restoreTokenHash: true, }), ), page: z.number(), totalPages: z.number(), }); export type Response = z.infer; }