import {z} from 'zod' export type GetApiActivityLogsArgs = {toDate?: string | undefined, fromDate?: string | undefined, userEmail?: string | undefined, userId?: string | undefined, action?: ('CREATE' | 'UPDATE' | 'DELETE' | 'LOGIN' | 'LOGOUT' | 'VIEW' | 'PAYMENT_SUCCESS' | 'PAYMENT_FAILED' | 'PAYMENT_CANCELED' | 'PAYMENT_PROCESSING' | 'EMAIL_SENT' | 'DOCUMENT_VIEW' | 'DOCUMENT_DOWNLOAD' | 'OTHER') | undefined, module?: string | undefined, search?: string | undefined, size: number, page: number}; export type GetApiActivityLogsOptions = {baseUrl?: string | undefined, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const activityLogResponseDto = z.object({id: z.number(), title: z.string(), description: z.string().optional(), action: z.enum(["CREATE", "UPDATE", "DELETE", "LOGIN", "LOGOUT", "VIEW", "PAYMENT_SUCCESS", "PAYMENT_FAILED", "PAYMENT_CANCELED", "PAYMENT_PROCESSING", "EMAIL_SENT", "DOCUMENT_VIEW", "DOCUMENT_DOWNLOAD", "OTHER"]), module: z.string(), userId: z.string().optional(), userEmail: z.string().optional(), userName: z.string().optional(), ipAddress: z.string().optional(), browser: z.string().optional(), location: z.string().optional(), entityId: z.number().optional(), entityType: z.string().optional(), paymentId: z.number().optional(), source: z.enum(["APPLICATION", "WEBSITE"]).optional(), metadata: z.object({}).optional(), createdAt: z.string()}); export const findAllActivityLogsResponseDto = z.object({totalItems: z.number(), items: z.array(activityLogResponseDto), page: z.number(), size: z.number()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get activity logs with filters * * GET /activity-logs */ export const getApiActivityLogs = async (args: GetApiActivityLogsArgs, options?: GetApiActivityLogsOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.userEmail != null) query.append("userEmail", String(args.userEmail)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.action != null) query.append("action", String(args.action)) if (args.module != null) query.append("module", String(args.module)) if (args.search != null) query.append("search", String(args.search)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/activity-logs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(findAllActivityLogsResponseDto, await res.json()) };