import {z} from 'zod' export type NotificationType = 'welcome' | 'quote_ready' | 'quote_rejected' | 'payment_confirmed' | 'shipment_update' | 'delivery_confirmed' | 'project_status_changed' | 'project_submitted_by_admin' | 'po_approved' | 'po_revoked' | 'po_uploaded_by_admin' | 'quote_approved_by_admin' | 'waitlist_confirmation' | 'waitlist_invite_sent' | 'new_comment' | 'new_project_submitted' | 'payment_received' | 'order_confirmed' | 'po_submitted' | 'new_user_registered' | 'new_waitlist_signup' | 'new_project_comment' | 'order_request_submitted' | 'order_request_edited' | 'agent_escalation' | 'rfq_drafts_ready' | 'supplier_reply_parsed' | 'supplier_quote_received' | 'agent_escalation_rfq' | 'rfq_clarification_drafted' | 'rfq_response_sla' | 'quote_revision_overdue' | 'quote_reactivation_requested'; export type NotificationPreferenceUpdate = {notification_type: NotificationType, email_enabled: boolean, in_app_enabled: boolean}; export type NotificationPreferencesUpdateRequest = {preferences: Array}; export type UpdateApiApiV1NotificationsPreferencesArgs = {body: NotificationPreferencesUpdateRequest}; export type UpdateApiApiV1NotificationsPreferencesOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationType = z.enum(["welcome", "quote_ready", "quote_rejected", "payment_confirmed", "shipment_update", "delivery_confirmed", "project_status_changed", "project_submitted_by_admin", "po_approved", "po_revoked", "po_uploaded_by_admin", "quote_approved_by_admin", "waitlist_confirmation", "waitlist_invite_sent", "new_comment", "new_project_submitted", "payment_received", "order_confirmed", "po_submitted", "new_user_registered", "new_waitlist_signup", "new_project_comment", "order_request_submitted", "order_request_edited", "agent_escalation", "rfq_drafts_ready", "supplier_reply_parsed", "supplier_quote_received", "agent_escalation_rfq", "rfq_clarification_drafted", "rfq_response_sla", "quote_revision_overdue", "quote_reactivation_requested"]); export const notificationPreferenceResponse = z.object({notification_type: notificationType, email_enabled: z.boolean(), in_app_enabled: z.boolean()}); export const updateApiApiV1NotificationsPreferencesResponse = z.array(notificationPreferenceResponse); 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 }; /** * Update Preferences * * PUT /api/v1/notifications/preferences */ export const updateApiApiV1NotificationsPreferences = async (args: UpdateApiApiV1NotificationsPreferencesArgs, options: UpdateApiApiV1NotificationsPreferencesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/notifications/preferences${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiApiV1NotificationsPreferencesResponse, await res.json()) };