import { schema } from 'nexus' import * as Auth from '../../utils/auth' import * as MeetingParticipantController from '../../controllers/meetingParticipant' import * as UserController from '../../controllers/user' export const MeetingParticipant = schema.objectType({ name: 'MeetingParticipant', definition(t) { t.model.id() t.model.status() t.model.createdAt() t.model.updatedAt() t.model.proportionateShare() t.model.accessCode() t.model.isInTheMeeting() t.model.isRegistered() t.model.isRepresentedByName() t.model.copropertyDueAmount() t.model.firstName() t.model.email() t.model.joinUrl() t.model.proxyId() t.model.receivedInvitation() t.model.receivedInvitationAt() t.model.isRepresentedByEmail() t.model.canVote() t.model.unitNumber() t.model.joinedTheMeetingAt() t.model.leftTheMeetingAt() t.model.isRepresentedByFirstName() t.model.isRepresentedByLastName() t.model.lastName() t.model.isARepresentee() t.model.isRepresentedBy() t.model.property() t.model.meeting() t.model.isRepresentedByProperty() t.model.proxy() t.model.proxyFile() t.model.representedByMeetingParticipant() t.model.user() t.model.meetingParticipantsRepresenting() t.model.activities() t.model.properties() t.model.meetingParticipantUnits() t.model.polls() t.model.pollQuestionAnswers() t.model.users() }, }) export const MeetingParticipantMutation = schema.extendType({ type: 'Mutation', definition: (t) => { t.crud.deleteOneMeetingParticipant({ alias: 'deleteMeetingParticipant' }) t.crud.updateOneMeetingParticipant({ alias: 'updateMeetingParticipant' }) t.crud.createOneMeetingParticipant({ alias: 'createMeetingParticipant' }) t.field('updateMeetingParticipant', { type: 'MeetingParticipant', args: { data: schema.arg({ type: 'MeetingParticipantUpdateInput' }), where: schema.arg({ type: 'MeetingParticipantWhereUniqueInput' }), }, resolve: async (_, { where, data }, ctx: NexusContext) => { const currentUserId = Auth.getUserId(ctx) const currentUserManagingCompany = await ctx.prisma.user .findOne({ where: { id: currentUserId } }) .managingCompany() // Represented by other participant // Make sure the meeting participant doesn't already exists if (data.representedByMeetingParticipant?.create) { const meetingId = data.representedByMeetingParticipant.create.meeting.connect.id if (!meetingId) { throw new Error('need to connect the meeting participant to a meeting') } const meetingParticipant = ( await ctx.prisma.meetingParticipant.findMany({ take: 1, where: { email: data.representedByMeetingParticipant.create.email, meeting: { id: meetingId, }, }, }) )[0] // Connect to the right meeting participant instead if (meetingParticipant) { data.representedByMeetingParticipant.connect = { id: meetingParticipant.id, } delete data.representedByMeetingParticipant.create } else { data.representedByMeetingParticipant.create.isARepresentee = true const currentMeetingParticipant = await ctx.prisma.meetingParticipant.findOne({ where, select: { user: { select: { id: true, properties: { select: { id: true, building: { select: { project: { select: { id: true, }, }, }, }, }, }, }, }, }, }) const currentMeetingProject = await ctx.prisma.meeting .findOne({ where: { id: meetingId } }) .project() const userPropertiesForProject = currentMeetingParticipant.user.properties.filter( (property) => property.building?.project?.id === currentMeetingProject.id ) // Also create a Walter user const user = await UserController.createUser({ role: 'RESIDENT', firstName: data.representedByMeetingParticipant.create.firstName, lastName: data.representedByMeetingParticipant.create.lastName, email: data.representedByMeetingParticipant.create.email, currentProject: { connect: { id: currentMeetingProject.id } }, projects: { connect: [{ id: currentMeetingProject.id }] }, properties: { connect: userPropertiesForProject.map(({ id }) => ({ id })) }, }) data.representedByMeetingParticipant.create.user = { connect: { id: user.id } } } } const updatedMeetingParticipant = await ctx.prisma.meetingParticipant.update({ where, data, include: { meeting: true, representedByMeetingParticipant: true, }, }) // Create registrant in zoom so that this person will be able to login if ( updatedMeetingParticipant.representedByMeetingParticipant && !updatedMeetingParticipant.representedByMeetingParticipant.zoomRegistrantId ) { await MeetingParticipantController.createAndApproveMeetingParticipant({ accessToken: currentUserManagingCompany.zoomAccessToken, meetingParticipant: updatedMeetingParticipant.representedByMeetingParticipant, zoomWebinarId: updatedMeetingParticipant.meeting.zoomWebinarId, }) } return ctx.prisma.meetingParticipant.findOne({ where: { id: updatedMeetingParticipant.id }, }) }, }) }, }) export const MeetingParticipantQuery = schema.extendType({ type: 'Query', definition(t) { t.crud.meetingParticipant() t.crud.meetingParticipants({ filtering: true, ordering: true }) t.field('getCurrentResidentMeetingParticipantForMeeting', { type: 'MeetingParticipant', args: { meetingId: schema.stringArg() }, resolve: async (_, { meetingId }, ctx: NexusContext) => { const currentUserId = Auth.getUserId(ctx) const currentUser = await ctx.prisma.user.findOne({ where: { id: currentUserId } }) const meetingParticipant = ( await ctx.prisma.meetingParticipant.findMany({ take: 1, where: { email: currentUser.email, meeting: { id: meetingId } }, }) )[0] return meetingParticipant }, }) }, })