diff --git a/schema.gql b/schema.gql index 16f414e..9f6cb97 100644 --- a/schema.gql +++ b/schema.gql @@ -135,6 +135,7 @@ type Query { isUserLogged: Boolean! findMentorAvailability(mentorId: String!): User! findEvents(learnerId: String, mentorId: String): [CreateEventOutput!]! + findEventsPerWeek(learnerId: String, mentorId: String, weeks: Float): [CreateEventOutput!]! findOneEvent(id: String!): CreateEventOutput! findAllSkills: [Skill!]! skill(id: Int!): Skill! diff --git a/src/modules/event/event.resolver.ts b/src/modules/event/event.resolver.ts index 895c278..f4ced8b 100644 --- a/src/modules/event/event.resolver.ts +++ b/src/modules/event/event.resolver.ts @@ -26,6 +26,16 @@ export class EventResolver { return this.eventService.findAll({ learnerId, mentorId }); } + @UseGuards(AuthGuard) + @Query(() => [CreateEventOutput], { name: 'findEventsPerWeek' }) + findEventsPerWeek( + @Args('learnerId', { nullable: true }) learnerId?: string, + @Args('mentorId', { nullable: true }) mentorId?: string, + @Args('weeks', { nullable: true }) weeks?: number, + ) { + return this.eventService.findEventsPerWeek({ learnerId, mentorId, weeks }); + } + @UseGuards(AuthGuard) @Query(() => CreateEventOutput, { name: 'findOneEvent' }) findOne(@Args('id', { type: () => String }) id: string) { diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 492462e..4f2e1cb 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -14,6 +14,7 @@ import { MailService } from '@common/services/mail/mail.service'; import { eventScheduledEmailProps } from '@providers/mails'; import { formatDate, formatHour } from '@common/utils/date'; import { NotificationsService } from '@modules/notifications/notifications.service'; +import { Role } from '@common/decorators/roles.decorator'; @Injectable() export class EventService { @@ -199,6 +200,62 @@ export class EventService { }); } + @Role('ADMIN') + async findEventsPerWeek({ + learnerId, + mentorId, + weeks, + }: { + learnerId?: string; + mentorId?: string; + weeks?: number; + }) { + const options = { + ...(mentorId && { + mentorId, + }), + ...(learnerId && { + participants: { + some: { + user: { + id: learnerId, + }, + }, + }, + }), + }; + + const currentDate = dayjs(); + let subtractWeeks = currentDate; + + if (weeks) { + subtractWeeks = currentDate.subtract(weeks, 'weeks'); + subtractWeeks.toDate(); + } + + const eventsPerWeek = await this.prisma.event.findMany({ + where: { + ...options, + ...subtractWeeks, + }, + include: { + participants: { + include: { + user: true, + }, + }, + }, + orderBy: { + startDate: 'asc', + }, + }); + + return eventsPerWeek.map((events) => { + if (!weeks) this.findAll(events); + return events; + }); + } + async findOne(id: string) { return this.prisma.event.findUnique({ where: {