import { schema } from 'nexus' import * as Auth from '../../utils/auth' import GeneralUtils from '../../utils/general' import { logger } from '../../utils/logger' import { send as sendNotification } from '../../utils/notification' import { AdOrderByInput, AdWhereInput } from 'nexus-plugin-prisma/client' export const Ad = schema.objectType({ name: 'Ad', definition(t) { t.model.id() t.model.createdAt() t.model.updatedAt() t.model.status() t.model.description() t.model.title() t.model.price() t.model.email() t.model.isSold() t.model.owner() t.model.phone() t.model.project() t.model.images() }, }) export const AdMutation = schema.extendType({ type: 'Mutation', definition: (t) => { t.crud.createOneAd({ alias: 'createAd' }) t.crud.updateOneAd({ alias: 'updateAd' }) t.crud.deleteOneAd({ alias: 'deleteAd' }) t.field('createAd', { type: 'Ad', args: { data: schema.arg({ type: 'AdCreateInput' }), }, resolve: async (parent, { data }, ctx: NexusContext) => { if (!data.project) { throw new Error('Missing project') } const currentUserId = Auth.getUserId(ctx) const currentUser = await ctx.prisma.user.findOne({ where: { id: currentUserId } }) // Ad created by resident need to be check first, maybe the property manager wants to approve them if (currentUser.role === 'RESIDENT') { const project = await ctx.prisma.project.findOne({ where: { id: data.project.connect.id }, }) if (project.newAdNeedApproval) { data.status = 'PENDING_APPROVAL' } } const createdAd = await ctx.prisma.ad.create({ data: { ...data }, select: { id: true, title: true, description: true, price: true, owner: { select: { id: true, firstName: true, lastName: true, property: { select: { address: { select: { apartmentNumber: true, }, }, }, }, properties: { select: { address: { select: { apartmentNumber: true, }, }, }, }, }, }, project: { select: { id: true, name: true, managingCompany: { select: { id: true, shortName: true, longName: true, users: true, }, }, }, }, }, }) // SEND NOTIFICATIONS try { if (currentUser.role === 'RESIDENT') { // Notifications to resident. IN COMMENT UNTIL WE RELEASE APP V2. // Make sure to includes users when fetching juste above // Notification.send({ // noEmail: true, // noSMS: true, // noAppNotification: true, // users: createdAd.project.users.filter(u => u.id !== currentUser.id), // title: `New ad posted by someone in your building`, // titleFr: `Nouvelle annonce publiƩe par quelqu'un de votre immeuble`, // subtitle: createdAd.title, // message: createdAd.description, // project: createdAd.project, // type: 'NEW_AD', // data: createdAd // }) // Notifications to managers await sendNotification({ noAppNotification: true, users: createdAd.project.managingCompany.users, title: `New ad posted by ${await GeneralUtils.getUserNameAndUnits( createdAd.owner, createdAd.project )}`, subtitle: createdAd.title, message: createdAd.description, projectId: createdAd.project.id, type: 'NEW_AD', data: createdAd, }) } } catch (e) { logger.error(e) } return ctx.prisma.ad.findOne({ where: { id: createdAd.id } }) }, }) }, }) export const AdQueries = schema.extendType({ type: 'Query', definition(t) { t.crud.ad() t.crud.ads({ filtering: true, ordering: true, pagination: true }) t.field('ads', { type: 'Ad', list: true, args: { where: schema.arg({ type: 'AdWhereInput' }), orderBy: schema.arg({ type: 'AdOrderByInput', list: true }), first: schema.arg({ type: 'Int' }), skip: schema.arg({ type: 'Int' }), }, resolve: async ( parent, { where, orderBy, first, skip, }: { where?: AdWhereInput; orderBy?: AdOrderByInput[]; first?: number; skip?: number }, ctx: NexusContext ) => { return ctx.prisma.ad.findMany({ where: where, ...(orderBy?.length > 0 && { orderBy: orderBy[0] }), take: first, skip: skip, }) }, }) }, })