diff --git a/src/ServiceFeedback/servicefeedback.schema.ts b/src/ServiceFeedback/servicefeedback.schema.ts index 0ea15a5..9864e1d 100644 --- a/src/ServiceFeedback/servicefeedback.schema.ts +++ b/src/ServiceFeedback/servicefeedback.schema.ts @@ -2,6 +2,7 @@ import { Inject, Injectable } 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, Role, ScheduleStatus } from '@prisma/client' @Injectable() export class ServiceFeedbackSchema extends PothosSchema { @@ -70,5 +71,60 @@ export class ServiceFeedbackSchema extends PothosSchema { }, }), })) + this.builder.mutationFields((t) => ({ + createServiceFeedback: t.prismaField({ + type: this.serviceFeedback(), + args: { + orderId: t.arg({ + type: 'String', + required: true, + }), + rating: t.arg({ + type: 'Float', + required: true, + }), + comments: t.arg({ + type: 'String', + required: false, + }), + }, + description: 'Create a new service feedback.', + resolve: async (_, _root, args, ctx, _info) => { + if (ctx.isSubscription) throw new Error('Not allowed') + if (!ctx.http?.me) throw new Error('Unauthorized') + // allow only when user is CUSTOMER and order is completed + if (ctx.http?.me?.role !== Role.CUSTOMER) throw new Error('Unauthorized') + const order = await this.prisma.order.findFirst({ + where: { + id: args.orderId, + userId: ctx.http?.me?.id, + status: OrderStatus.PAID, + schedule: { + dates: { + every: { + status: ScheduleStatus.COMPLETED, + }, + }, + }, + }, + }) + if (!order) throw new Error('Order not found') + if (order.userId !== ctx.http?.me?.id) throw new Error('Unauthorized') + if (order.status !== OrderStatus.PAID) throw new Error('Order not completed') + // validate rating + if (args.rating < 0 || args.rating > 5) throw new Error('Invalid rating') + // validate comments + if (args.comments && args.comments.length > 1024) throw new Error('Comments too long') + return await this.prisma.serviceFeedback.create({ + data: { + userId: ctx.http?.me?.id, + serviceId: order.serviceId, + rating: args.rating, + comments: args.comments, + }, + }) + }, + }), + })) } }