import { schema } from 'nexus' import * as ZoomUtils from '../../utils/zoom' // import * as logger from '../../utils/logger' export const Poll = schema.objectType({ name: 'Poll', definition(t) { t.model.id() t.model.createdAt() t.model.updatedAt() t.model.expiryDate() t.model.title() t.model.question() t.model.zoomPollId() t.model.isLaunched() t.model.startedAt() t.model.endedAt() t.model.posts() t.model.completedByParticipants() t.model.meetings() t.model.questions() t.model.completedByUsers() }, }) export const PollQueries = schema.extendType({ type: 'Query', definition(t) { t.crud.poll() t.crud.polls({ filtering: true, ordering: true }) }, }) export const PollMutation = schema.extendType({ type: 'Mutation', definition: (t) => { t.crud.deleteOnePoll({ alias: 'deletePoll' }) t.crud.updateOnePoll({ alias: 'updatePoll' }) t.crud.createOnePoll({ alias: 'createPoll' }) t.field('startPoll', { type: 'Poll', args: { pollId: schema.stringArg(), }, resolve: async (_, { pollId }, ctx: NexusContext) => { return ctx.prisma.poll.update({ where: { id: pollId }, data: { startedAt: new Date(), isLaunched: true }, }) }, }) t.field('endPoll', { type: 'Poll', args: { pollId: schema.stringArg(), }, resolve: async (_, { pollId }, ctx: NexusContext) => { return ctx.prisma.poll.update({ where: { id: pollId }, data: { endedAt: new Date() }, }) }, }) t.field('resetPoll', { type: 'Poll', args: { pollId: schema.stringArg(), }, resolve: async (_, { pollId }, ctx: NexusContext) => { const poll = await ctx.prisma.poll.findOne({ where: { id: pollId }, select: { id: true, questions: { select: { id: true, answers: { select: { id: true, }, }, }, }, }, }) // Remove all answered questions from participants await Promise.all( poll.questions.map((question) => Promise.all( question.answers.map((answer) => ctx.prisma.pollQuestionAnswer.update({ where: { id: answer.id, }, data: { answeredByMeetingParticipants: { set: [], }, }, }) ) ) ) ) return ctx.prisma.poll.update({ where: { id: pollId }, data: { isLaunched: false, startedAt: null, endedAt: null, completedByUsers: { set: [], }, completedByParticipants: { set: [], }, }, }) }, }) }, })