import moment from 'moment' import { schema } from 'nexus' import { NotificationOrderByInput, NotificationWhereInput } from 'nexus-plugin-prisma/client' export const Notification = schema.objectType({ name: 'Notification', definition(t) { t.model.id() t.model.createdAt() t.model.updatedAt() t.model.type() t.model.appType() t.model.title() t.model.message() t.model.hasBeenSeen() t.model.data() t.model.subtitle() t.model.hasBeenSeenAt() t.model.hasBeenSeenByApp() t.model.hasBeenSeenByAppAt() t.model.hasBeenSeenBySMS() t.model.hasBeenSeenBySMSAt() t.model.hasBeenSeenByEmail() t.model.hasBeenSeenByEmailAt() t.model.delivered() t.model.deliveredAt() t.model.deliveredByApp() t.model.deliveredByAppAt() t.model.deliveredByEmail() t.model.deliveredByEmailAt() t.model.deliveredBySMS() t.model.deliveredBySMSAt() t.model.hasBeenSeenByWeb() t.model.hasBeenSeenByWebAt() t.model.sent() t.model.sentAt() t.model.sentByApp() t.model.sentByAppAt() t.model.sentByEmail() t.model.sentByEmailAt() t.model.sentBySMS() t.model.sentBySMSAt() t.model.firstReminderSent() t.model.firstReminderSentAt() t.model.firstReminderSentByApp() t.model.firstReminderSentByAppAt() t.model.firstReminderSentByEmail() t.model.firstReminderSentByEmailAt() t.model.firstReminderSentBySMS() t.model.firstReminderSentBySMSAt() t.model.hasBeenOpenedByApp() t.model.hasBeenOpenedByAppAt() t.model.deliveryErrorMessageApp() t.model.deliveryErrorMessageEmail() t.model.deliveryErrorMessageSMS() t.model.errorMessage() t.model.forceSend() t.model.noApp() t.model.noEmail() t.model.noSMS() t.model.forceEmail() t.model.forceApp() t.model.forceSMS() t.model.sentByAppErrorMessage() t.model.sentByEmailErrorMessage() t.model.sentBySMSErrorMessage() t.model.sentErrorMessage() t.model.forceSendMessage() t.model.event() t.model.post() t.model.project() t.model.serviceProject() t.model.user() }, }) export const NotificationMutation = schema.extendType({ type: 'Mutation', definition: (t) => { t.crud.deleteOneNotification({ alias: 'deleteNotification' }) t.crud.updateOneNotification({ alias: 'updateNotification' }) t.crud.createOneNotification({ alias: 'createNotification' }) t.field('updateNotification', { type: 'Notification', args: { data: schema.arg({ type: 'NotificationUpdateInput' }), where: schema.arg({ type: 'NotificationWhereUniqueInput' }), }, resolve: async (parent, { where, data }, ctx: NexusContext) => { if (!where?.id) { throw new Error('Can\'t update notification without notification id') } // If we have seen the notification in an app we can update the good time here if (data.hasBeenSeenByApp) { data.hasBeenSeenByAppAt = moment().toDate() } if (data.hasBeenSeenByWeb) { data.hasBeenSeenByWebAt = moment().toDate() } if (data.hasBeenOpenedByApp) { data.hasBeenOpenedByAppAt = moment().toDate() } return ctx.prisma.notification.update({ where, data }) }, }) }, }) export const NotificationQuery = schema.extendType({ type: 'Query', definition(t) { t.crud.notification() t.crud.notifications({ filtering: true, ordering: true, pagination: true }) t.field('notifications', { type: 'Notification', list: true, args: { where: schema.arg({ type: 'NotificationWhereInput' }), orderBy: schema.arg({ type: 'NotificationOrderByInput', list: true }), first: schema.arg({ type: 'Int' }), last: schema.arg({ type: 'Int' }), skip: schema.arg({ type: 'Int' }), }, resolve: async ( parent, { where, orderBy, first, last, skip, }: { where?: NotificationWhereInput orderBy?: NotificationOrderByInput[] first?: number last?: number skip?: number }, ctx: NexusContext ) => { if (first && last) { throw new Error('Can\'t provide both first and last parameters at the same time') } return ctx.prisma.notification.findMany({ where: where, skip: skip, ...(orderBy?.length > 0 && { orderBy: orderBy[0] }), ...((first || last) && { take: first || -last, }), }) }, }) }, })