import { logger } from './logger' import { USER_ROLE } from 'nexus-plugin-prisma/client' export const roleIsForManagingPlatform = (role) => ['MANAGER', 'MARKETER'].some((r) => r === role) export const isManager = (role) => role === 'MANAGER' const userCanReceiveNotificationForNotificationType = (user, notificationType) => { if (!user.managingCompanyRole?.permissions) { logger.error( 'User passed in userCanReceiveNotificationForNotificationType has managingCompanyRole but managingCompanyRole doesn\'t have permissions' ) return true } let permissionsNeededForNotificationType = [] switch (notificationType) { case 'NEW_RESIDENT': permissionsNeededForNotificationType = ['USER_READ'] break case 'NEW_CHAT_PRIVATE_MENTIONED': case 'NEW_CHAT_MESSAGE': permissionsNeededForNotificationType = ['CHAT_READ'] break case 'NEW_POST': case 'NEW_PROXY': case 'NEW_PROXY_COMPLETED': permissionsNeededForNotificationType = ['NOTIFICATION_READ'] break case 'NEW_EVENT': permissionsNeededForNotificationType = ['CALENDAR_READ'] break case 'NEW_RULE': permissionsNeededForNotificationType = ['RULE_READ'] break case 'NEW_CONTACT': permissionsNeededForNotificationType = ['CONTACT_READ'] break case 'NEW_FILE': permissionsNeededForNotificationType = ['FILE_READ'] break case 'NEW_SERVICE_REQUEST': case 'UPDATE_SERVICE_REQUEST': permissionsNeededForNotificationType = ['WORK_ORDER_READ'] break case 'NEW_TASK': case 'SERVICE_REQUEST_ASSIGNED': case 'TASK_ASSIGNEE': permissionsNeededForNotificationType = ['TASK_READ'] break case 'PACKAGE_UPDATE': case 'PACKAGE_AT_RECEPTION': case 'PACKAGE_DELIVERED': permissionsNeededForNotificationType = ['MAIL_READ'] break case 'NEW_AMENITY': case 'NEW_RESERVATION': case 'UPDATE_RESERVATION': permissionsNeededForNotificationType = ['AMENITY_READ'] break case 'NEW_SALE': case 'UPDATE_SALE': case 'NEW_AD': case 'UPDATE_AD': permissionsNeededForNotificationType = ['SALE_READ'] break default: logger.error(`Missing a case for notification: ${notificationType}`) return true } // The user needs at least one of them to receive the notification return permissionsNeededForNotificationType.some((permissionNeeded) => user.managingCompanyRole.permissions.some((permission) => permission === permissionNeeded) ) } const hasAccessToProject = ( projectId: string, user: { role: USER_ROLE managingCompanyRole: { projects: { id: string }[] } } ) => user.role === 'PROVIDER' || user.role === 'RESIDENT' || (user.managingCompanyRole ? user.managingCompanyRole.projects?.some((p) => p.id === projectId) : true) export default { userCanReceiveNotificationForNotificationType, hasAccessToProject, }