From 1b7329bb440b3cc9062f798030e5065eaa7fa42f Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Thu, 5 Dec 2024 21:31:38 +0700 Subject: [PATCH] feat: add completedOrders field to Order schema and enhance workshop subscription error handling - Introduced a new `completedOrders` field in the Order schema to retrieve a list of completed orders for the authenticated user, filtering by order status and schedule dates. - Enhanced error handling in the WorkshopSubscription schema to prevent retrieval in subscription context, improving the robustness of the API. - Updated imports in order.schema.ts to include additional status types from Prisma for better functionality. --- src/Order/order.schema.ts | 46 ++++++++++++++++++- .../workshopsubscription.schema.ts | 3 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Order/order.schema.ts b/src/Order/order.schema.ts index 49a72c4..4170da4 100644 --- a/src/Order/order.schema.ts +++ b/src/Order/order.schema.ts @@ -2,7 +2,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common' import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos' import { Builder } from '../Graphql/graphql.builder' import { PrismaService } from '../Prisma/prisma.service' -import { OrderStatus } from '@prisma/client' +import { OrderStatus, ScheduleDateStatus, ScheduleStatus } from '@prisma/client' import { DateTimeUtils } from '../common/utils/datetime.utils' import { PayosService } from '../Payos/payos.service' import _ from 'lodash' @@ -112,6 +112,50 @@ export class OrderSchema extends PothosSchema { }) }, }), + completedOrders: t.prismaField({ + type: [this.order()], + description: 'Retrieve a list of completed orders', + args: this.builder.generator.findManyArgs('Order'), + resolve: async (query, _root, args, ctx, _info) => { + if (ctx.isSubscription) throw new Error('Orders cannot be retrieved in subscription context') + if (!ctx.http.me) throw new Error('Unauthorized') + // return orders where user is the one who made the order and status is PAID and schedule.dates is in the past + return await this.prisma.order.findMany({ + ...query, + where: { + AND: [ + ...(args.filter ? [args.filter] : []), + { + userId: ctx.http.me.id, + status: OrderStatus.PAID, + schedule: { + OR: [ + { + dates: { + every: { + OR: [ + { + end: { lte: DateTimeUtils.now().toJSDate() }, + }, + { + status: ScheduleDateStatus.COMPLETED, + }, + ], + }, + }, + }, + // or cancelled + { + status: ScheduleStatus.REFUNDED, + }, + ], + }, + }, + ], + }, + }) + }, + }), })) // mutation section this.builder.mutationFields((t) => ({ diff --git a/src/WorkshopSubscription/workshopsubscription.schema.ts b/src/WorkshopSubscription/workshopsubscription.schema.ts index d075e39..1c4df8b 100644 --- a/src/WorkshopSubscription/workshopsubscription.schema.ts +++ b/src/WorkshopSubscription/workshopsubscription.schema.ts @@ -58,7 +58,8 @@ export class WorkshopSubscriptionSchema extends PothosSchema { type: [this.workshopSubscription()], args: this.builder.generator.findManyArgs('WorkshopSubscription'), description: 'Retrieve a list of workshop subscriptions with optional filtering, ordering, and pagination.', - resolve: async (query, _root, args) => { + resolve: async (query, _root, args, ctx) => { + if (ctx.isSubscription) throw new Error('Workshops cannot be retrieved in subscription context') return await this.prisma.workshopSubscription.findMany({ ...query, skip: args.skip ?? undefined,