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.
This commit is contained in:
@@ -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) => ({
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user