import { UserGetPayload } from 'nexus-plugin-prisma/client' import { MANAGER_NOTIFICATION_TYPES } from '../../utils/notification' import { prismaClient as prisma } from '../../prismaClient' const PLATFORMS = ['APP', 'EMAIL', 'SMS'] type UserWithRelations = UserGetPayload<{ include: { notificationPreferences: true } }> prisma.user .findMany({ where: { role: 'MANAGER' }, include: { notificationPreferences: true } }) .then(async (users: UserWithRelations[]) => { for await (const user of users) { const newNotificationPreferencesForUser = [] for await (const platform of PLATFORMS) { for await (const notificationType of MANAGER_NOTIFICATION_TYPES) { const userHasNotificationPreference = user.notificationPreferences.some( (preference) => preference.type === notificationType && preference.platform === platform ) if (!userHasNotificationPreference) { // Based on the current user notifciation settings let isEnable = false if (platform === 'APP' && user.hasAppNotificationEnable) { isEnable = true } else if (platform === 'EMAIL' && user.hasEmailNotificationEnable) { isEnable = true } else if (platform === 'SMS' && user.hasSMSNotificationEnable) { isEnable = true } newNotificationPreferencesForUser.push({ platform, type: notificationType, isEnable, }) } } } if (newNotificationPreferencesForUser.length) { console.log('Adding notification preferences to user') await prisma.user.update({ where: { id: user.id }, data: { notificationPreferences: { create: newNotificationPreferencesForUser, }, }, }) console.log('Added') } } console.log('Done') })