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:
2024-12-05 21:31:38 +07:00
parent 0d973a6544
commit 1b7329bb44
2 changed files with 47 additions and 2 deletions

View File

@@ -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) => ({