import { z } from 'zod'; import { ListSchema } from './common'; export const NotificationTypeSchema = z.enum([ 'reservation_reminder', 'watchlist_alert', 'rating_request', ]); export type NotificationType = z.infer; export const NotificationDataSchema = z.discriminatedUnion('type', [ z.object({ type: NotificationTypeSchema.extract(['reservation_reminder']), }), z.object({ type: NotificationTypeSchema.extract(['watchlist_alert']), productionSlug: z.string(), }), z.object({ type: NotificationTypeSchema.extract(['rating_request']), productionSlug: z.string(), }), ]); export type NotificationData = z.infer; export const NotificationPreferenceSchema = z.object({ type: NotificationTypeSchema, isEnabled: z.boolean(), }); export type NotificationPreference = z.infer< typeof NotificationPreferenceSchema >; export const NotificationPreferenceHalSchema = NotificationPreferenceSchema.extend({ _links: z.object({ self: z.object({ href: z.string(), }), }), }); export type NotificationPreferenceHalSchemaType = z.infer< typeof NotificationPreferenceHalSchema >; // Ignore unknown preference types, avoid error when new type is added on backend. const NotificationPreferenceHalOrNullSchema = NotificationPreferenceHalSchema.nullable().catch(null) as z.ZodType< NotificationPreferenceHalSchemaType | null, NotificationPreferenceHalSchemaType >; export const NotificationPreferencesHalListSchema = ListSchema.extend({ _embedded: z.object({ preferences: z .array(NotificationPreferenceHalOrNullSchema) .transform((preferences) => preferences.filter( (preference): preference is NotificationPreferenceHalSchemaType => preference !== null ) ), }), }); export type NotificationPreferencesHalListSchemaType = z.infer< typeof NotificationPreferencesHalListSchema >; export const NotificationPreferenceUpdateInputSchema = NotificationPreferenceSchema.pick({ isEnabled: true, }); export type NotificationPreferenceUpdateInputSchemaType = z.infer< typeof NotificationPreferenceUpdateInputSchema >;