refactor: standardize formatting and improve OrderSchema
- Reformatted import statements in order.schema.ts for consistency and readability. - Enhanced field descriptions in the OrderSchema to ensure clarity and uniformity. - Updated error messages for better user feedback during order processing. - Improved the structure of query and mutation fields for better maintainability. These changes aim to enhance code readability, maintainability, and user experience within the Order management features.
This commit is contained in:
@@ -1,14 +1,24 @@
|
|||||||
import { Inject, Injectable, Logger } from '@nestjs/common'
|
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||||
import { OrderStatus, Role, ScheduleDateStatus, ScheduleStatus } from '@prisma/client'
|
import {
|
||||||
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
|
OrderStatus,
|
||||||
import _ from 'lodash'
|
Role,
|
||||||
import { CenterSchema } from 'src/Center/center.schema'
|
ScheduleDateStatus,
|
||||||
import { CenterMentorSchema } from 'src/CenterMentor/centermentor.schema'
|
ScheduleStatus,
|
||||||
import { UserSchema } from 'src/User/user.schema'
|
} from "@prisma/client";
|
||||||
import { Builder } from '../Graphql/graphql.builder'
|
import {
|
||||||
import { PayosService } from '../Payos/payos.service'
|
Pothos,
|
||||||
import { PrismaService } from '../Prisma/prisma.service'
|
PothosRef,
|
||||||
import { DateTimeUtils } from '../common/utils/datetime.utils'
|
PothosSchema,
|
||||||
|
SchemaBuilderToken,
|
||||||
|
} from "@smatch-corp/nestjs-pothos";
|
||||||
|
import _ from "lodash";
|
||||||
|
import { CenterSchema } from "src/Center/center.schema";
|
||||||
|
import { CenterMentorSchema } from "src/CenterMentor/centermentor.schema";
|
||||||
|
import { UserSchema } from "src/User/user.schema";
|
||||||
|
import { Builder } from "../Graphql/graphql.builder";
|
||||||
|
import { PayosService } from "../Payos/payos.service";
|
||||||
|
import { PrismaService } from "../Prisma/prisma.service";
|
||||||
|
import { DateTimeUtils } from "../common/utils/datetime.utils";
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class OrderSchema extends PothosSchema {
|
export class OrderSchema extends PothosSchema {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -17,107 +27,107 @@ export class OrderSchema extends PothosSchema {
|
|||||||
private readonly payosService: PayosService,
|
private readonly payosService: PayosService,
|
||||||
private readonly centerSchema: CenterSchema,
|
private readonly centerSchema: CenterSchema,
|
||||||
private readonly centerMentorSchema: CenterMentorSchema,
|
private readonly centerMentorSchema: CenterMentorSchema,
|
||||||
private readonly userSchema: UserSchema,
|
private readonly userSchema: UserSchema
|
||||||
) {
|
) {
|
||||||
super()
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Types section
|
// Types section
|
||||||
@PothosRef()
|
@PothosRef()
|
||||||
order() {
|
order() {
|
||||||
return this.builder.prismaObject('Order', {
|
return this.builder.prismaObject("Order", {
|
||||||
description: 'An order in the system.',
|
description: "An order in the system.",
|
||||||
fields: (t) => ({
|
fields: (t) => ({
|
||||||
id: t.exposeID('id', {
|
id: t.exposeID("id", {
|
||||||
description: 'The ID of the order.',
|
description: "The ID of the order.",
|
||||||
}),
|
}),
|
||||||
userId: t.exposeID('userId', {
|
userId: t.exposeID("userId", {
|
||||||
description: 'The ID of the user.',
|
description: "The ID of the user.",
|
||||||
}),
|
}),
|
||||||
serviceId: t.exposeID('serviceId', {
|
serviceId: t.exposeID("serviceId", {
|
||||||
description: 'The ID of the service.',
|
description: "The ID of the service.",
|
||||||
}),
|
}),
|
||||||
status: t.expose('status', {
|
status: t.expose("status", {
|
||||||
type: OrderStatus,
|
type: OrderStatus,
|
||||||
description: 'The status of the order.',
|
description: "The status of the order.",
|
||||||
}),
|
}),
|
||||||
total: t.exposeInt('total', {
|
total: t.exposeInt("total", {
|
||||||
description: 'The total price of the order.',
|
description: "The total price of the order.",
|
||||||
}),
|
}),
|
||||||
scheduleId: t.exposeID('scheduleId', {
|
scheduleId: t.exposeID("scheduleId", {
|
||||||
description: 'The ID of the schedule.',
|
description: "The ID of the schedule.",
|
||||||
}),
|
}),
|
||||||
schedule: t.relation('schedule', {
|
schedule: t.relation("schedule", {
|
||||||
description: 'The schedule of the order.',
|
description: "The schedule of the order.",
|
||||||
}),
|
}),
|
||||||
disbursed: t.exposeBoolean('disbursed', {
|
disbursed: t.exposeBoolean("disbursed", {
|
||||||
description: 'Whether the order has been disbursed.',
|
description: "Whether the order has been disbursed.",
|
||||||
}),
|
}),
|
||||||
chatRoomId: t.exposeID('chatRoomId', {
|
chatRoomId: t.exposeID("chatRoomId", {
|
||||||
description: 'The ID of the chat room.',
|
description: "The ID of the chat room.",
|
||||||
}),
|
}),
|
||||||
chatRoom: t.relation('chatRoom', {
|
chatRoom: t.relation("chatRoom", {
|
||||||
description: 'The chat room of the order.',
|
description: "The chat room of the order.",
|
||||||
}),
|
}),
|
||||||
createdAt: t.expose('createdAt', {
|
createdAt: t.expose("createdAt", {
|
||||||
type: 'DateTime',
|
type: "DateTime",
|
||||||
description: 'The date and time the order was created.',
|
description: "The date and time the order was created.",
|
||||||
}),
|
}),
|
||||||
updatedAt: t.expose('updatedAt', {
|
updatedAt: t.expose("updatedAt", {
|
||||||
type: 'DateTime',
|
type: "DateTime",
|
||||||
description: 'The date and time the order was updated.',
|
description: "The date and time the order was updated.",
|
||||||
}),
|
}),
|
||||||
commission: t.exposeFloat('commission', {
|
commission: t.exposeFloat("commission", {
|
||||||
description: 'The commission of the order.',
|
description: "The commission of the order.",
|
||||||
}),
|
}),
|
||||||
user: t.relation('user', {
|
user: t.relation("user", {
|
||||||
description: 'The user who made the order.',
|
description: "The user who made the order.",
|
||||||
}),
|
}),
|
||||||
service: t.relation('service', {
|
service: t.relation("service", {
|
||||||
description: 'The service for the order.',
|
description: "The service for the order.",
|
||||||
}),
|
}),
|
||||||
refundTicket: t.relation('refundTicket', {
|
refundTicket: t.relation("refundTicket", {
|
||||||
description: 'The refund ticket for the order.',
|
description: "The refund ticket for the order.",
|
||||||
}),
|
}),
|
||||||
payment: t.relation('payment', {
|
payment: t.relation("payment", {
|
||||||
description: 'The payment for the order.',
|
description: "The payment for the order.",
|
||||||
}),
|
}),
|
||||||
paymentId: t.exposeString('paymentId', {
|
paymentId: t.exposeString("paymentId", {
|
||||||
description: 'The ID of the payment.',
|
description: "The ID of the payment.",
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@PothosRef()
|
@PothosRef()
|
||||||
orderDetails() {
|
orderDetails() {
|
||||||
return this.builder.simpleObject('OrderDetails', {
|
return this.builder.simpleObject("OrderDetails", {
|
||||||
fields: (t) => ({
|
fields: (t) => ({
|
||||||
order: t.field({
|
order: t.field({
|
||||||
type: this.order(),
|
type: this.order(),
|
||||||
description: 'The order of the details.',
|
description: "The order of the details.",
|
||||||
}),
|
}),
|
||||||
center: t.field({
|
center: t.field({
|
||||||
type: this.centerSchema.center(),
|
type: this.centerSchema.center(),
|
||||||
description: 'The center of the order.',
|
description: "The center of the order.",
|
||||||
}),
|
}),
|
||||||
centerMentor: t.field({
|
centerMentor: t.field({
|
||||||
type: this.centerMentorSchema.centerMentor(),
|
type: this.centerMentorSchema.centerMentor(),
|
||||||
description: 'The mentor of the order.',
|
description: "The mentor of the order.",
|
||||||
}),
|
}),
|
||||||
user: t.field({
|
user: t.field({
|
||||||
type: this.userSchema.user(),
|
type: this.userSchema.user(),
|
||||||
description: 'The user of the order.',
|
description: "The user of the order.",
|
||||||
}),
|
}),
|
||||||
status: t.string(),
|
status: t.string(),
|
||||||
total: t.int(),
|
total: t.int(),
|
||||||
commission: t.float(),
|
commission: t.float(),
|
||||||
completedAt: t.field({
|
completedAt: t.field({
|
||||||
type: 'DateTime',
|
type: "DateTime",
|
||||||
description: 'The date and time the order was completed.',
|
description: "The date and time the order was completed.",
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Pothos()
|
@Pothos()
|
||||||
@@ -126,8 +136,9 @@ export class OrderSchema extends PothosSchema {
|
|||||||
this.builder.queryFields((t) => ({
|
this.builder.queryFields((t) => ({
|
||||||
orders: t.prismaField({
|
orders: t.prismaField({
|
||||||
type: [this.order()],
|
type: [this.order()],
|
||||||
description: 'Retrieve a list of orders with optional filtering, ordering, and pagination.',
|
description:
|
||||||
args: this.builder.generator.findManyArgs('Order'),
|
"Retrieve a list of orders with optional filtering, ordering, and pagination.",
|
||||||
|
args: this.builder.generator.findManyArgs("Order"),
|
||||||
resolve: async (query, _root, args, _ctx, _info) => {
|
resolve: async (query, _root, args, _ctx, _info) => {
|
||||||
return await this.prisma.order.findMany({
|
return await this.prisma.order.findMany({
|
||||||
...query,
|
...query,
|
||||||
@@ -135,27 +146,27 @@ export class OrderSchema extends PothosSchema {
|
|||||||
skip: args.skip ?? undefined,
|
skip: args.skip ?? undefined,
|
||||||
orderBy: args.orderBy ?? undefined,
|
orderBy: args.orderBy ?? undefined,
|
||||||
where: args.filter ?? undefined,
|
where: args.filter ?? undefined,
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
order: t.prismaField({
|
order: t.prismaField({
|
||||||
type: this.order(),
|
type: this.order(),
|
||||||
args: this.builder.generator.findUniqueArgs('Order'),
|
args: this.builder.generator.findUniqueArgs("Order"),
|
||||||
description: 'Retrieve a single order by its unique identifier.',
|
description: "Retrieve a single order by its unique identifier.",
|
||||||
resolve: async (query, _root, args, _ctx, _info) => {
|
resolve: async (query, _root, args, _ctx, _info) => {
|
||||||
return await this.prisma.order.findUnique({
|
return await this.prisma.order.findUnique({
|
||||||
...query,
|
...query,
|
||||||
where: args.where,
|
where: args.where,
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
completedOrders: t.prismaField({
|
completedOrders: t.prismaField({
|
||||||
type: [this.order()],
|
type: [this.order()],
|
||||||
description: 'Retrieve a list of completed orders',
|
description: "Retrieve a list of completed orders",
|
||||||
args: this.builder.generator.findManyArgs('Order'),
|
args: this.builder.generator.findManyArgs("Order"),
|
||||||
resolve: async (query, _root, args, ctx, _info) => {
|
resolve: async (query, _root, args, ctx, _info) => {
|
||||||
if (!ctx.me) {
|
if (!ctx.me) {
|
||||||
throw new Error('Unauthorized')
|
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 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({
|
return await this.prisma.order.findMany({
|
||||||
@@ -191,20 +202,20 @@ export class OrderSchema extends PothosSchema {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
completedOrdersForModerator: t.prismaField({
|
completedOrdersForModerator: t.prismaField({
|
||||||
type: [this.order()],
|
type: [this.order()],
|
||||||
description: 'Retrieve a list of completed orders for moderator',
|
description: "Retrieve a list of completed orders for moderator",
|
||||||
args: this.builder.generator.findManyArgs('Order'),
|
args: this.builder.generator.findManyArgs("Order"),
|
||||||
resolve: async (query, _root, args, ctx, _info) => {
|
resolve: async (query, _root, args, ctx, _info) => {
|
||||||
if (!ctx.me) {
|
if (!ctx.me) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
// only for role moderator
|
// only for role moderator
|
||||||
if (ctx.me.role !== Role.MODERATOR) {
|
if (ctx.me.role !== Role.MODERATOR) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
// return completed order list where schedule status is COMPLETED
|
// return completed order list where schedule status is COMPLETED
|
||||||
return await this.prisma.order.findMany({
|
return await this.prisma.order.findMany({
|
||||||
@@ -220,24 +231,24 @@ export class OrderSchema extends PothosSchema {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
completedOrdersDetails: t.field({
|
completedOrdersDetails: t.field({
|
||||||
type: this.orderDetails(),
|
type: this.orderDetails(),
|
||||||
args: {
|
args: {
|
||||||
orderId: t.arg({
|
orderId: t.arg({
|
||||||
type: 'String',
|
type: "String",
|
||||||
required: true,
|
required: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
description: 'Retrieve a list of completed orders details',
|
description: "Retrieve a list of completed orders details",
|
||||||
resolve: async (_query, args, ctx, _info) => {
|
resolve: async (_query, args, ctx, _info) => {
|
||||||
if (!ctx.me) {
|
if (!ctx.me) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
if (ctx.me.role !== Role.MODERATOR) {
|
if (ctx.me.role !== Role.MODERATOR) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
// get order details
|
// get order details
|
||||||
const order = await this.prisma.order.findUnique({
|
const order = await this.prisma.order.findUnique({
|
||||||
@@ -250,45 +261,45 @@ export class OrderSchema extends PothosSchema {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
if (!order) {
|
if (!order) {
|
||||||
throw new Error('Order not found')
|
throw new Error("Order not found");
|
||||||
}
|
}
|
||||||
// get center details
|
// get center details
|
||||||
const center = await this.prisma.center.findUnique({
|
const center = await this.prisma.center.findUnique({
|
||||||
where: { id: order?.service?.centerId },
|
where: { id: order?.service?.centerId },
|
||||||
})
|
});
|
||||||
if (!center) {
|
if (!center) {
|
||||||
throw new Error('Center not found')
|
throw new Error("Center not found");
|
||||||
}
|
}
|
||||||
// get mentor id from schedule
|
// get mentor id from schedule
|
||||||
const mentorId = order?.schedule?.managedService?.mentorId
|
const mentorId = order?.schedule?.managedService?.mentorId;
|
||||||
// get center mentor details
|
// get center mentor details
|
||||||
const centerMentor = await this.prisma.user.findUnique({
|
const centerMentor = await this.prisma.user.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: mentorId,
|
id: mentorId,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
if (!centerMentor) {
|
if (!centerMentor) {
|
||||||
throw new Error('Center mentor not found')
|
throw new Error("Center mentor not found");
|
||||||
}
|
}
|
||||||
// get service details
|
// get service details
|
||||||
const service = await this.prisma.service.findUnique({
|
const service = await this.prisma.service.findUnique({
|
||||||
where: { id: order?.serviceId },
|
where: { id: order?.serviceId },
|
||||||
})
|
});
|
||||||
if (!service) {
|
if (!service) {
|
||||||
throw new Error('Service not found')
|
throw new Error("Service not found");
|
||||||
}
|
}
|
||||||
// calculate commission based on service price
|
// calculate commission based on service price
|
||||||
const commission = service.price * (service.commission ?? 0.0)
|
const commission = service.price * (service.commission ?? 0.0);
|
||||||
// calculate total price
|
// calculate total price
|
||||||
const total = service.price - commission
|
const total = service.price - commission;
|
||||||
// get user details
|
// get user details
|
||||||
const user = await this.prisma.user.findUnique({
|
const user = await this.prisma.user.findUnique({
|
||||||
where: { id: order?.userId },
|
where: { id: order?.userId },
|
||||||
})
|
});
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error('User not found')
|
throw new Error("User not found");
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
order: {
|
order: {
|
||||||
@@ -330,46 +341,46 @@ export class OrderSchema extends PothosSchema {
|
|||||||
total,
|
total,
|
||||||
commission,
|
commission,
|
||||||
completedAt: order.schedule?.scheduleEnd,
|
completedAt: order.schedule?.scheduleEnd,
|
||||||
}
|
};
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}))
|
}));
|
||||||
|
|
||||||
// mutation section
|
// mutation section
|
||||||
this.builder.mutationFields((t) => ({
|
this.builder.mutationFields((t) => ({
|
||||||
createOrder: t.prismaField({
|
createOrder: t.prismaField({
|
||||||
type: this.order(),
|
type: this.order(),
|
||||||
description: 'Create a new order.',
|
description: "Create a new order.",
|
||||||
args: {
|
args: {
|
||||||
data: t.arg({
|
data: t.arg({
|
||||||
type: this.builder.generator.getCreateInput('Order', [
|
type: this.builder.generator.getCreateInput("Order", [
|
||||||
'id',
|
"id",
|
||||||
'user',
|
"user",
|
||||||
'paymentId',
|
"paymentId",
|
||||||
'payment',
|
"payment",
|
||||||
'refundTicket',
|
"refundTicket",
|
||||||
'status',
|
"status",
|
||||||
'total',
|
"total",
|
||||||
'createdAt',
|
"createdAt",
|
||||||
'updatedAt',
|
"updatedAt",
|
||||||
'commission',
|
"commission",
|
||||||
]),
|
]),
|
||||||
required: true,
|
required: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
resolve: async (query, _root, args, ctx, _info) => {
|
resolve: async (query, _root, args, ctx, _info) => {
|
||||||
if (!ctx.me) {
|
if (!ctx.me) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
if (!args.data.service.connect?.id) {
|
if (!args.data.service.connect?.id) {
|
||||||
throw new Error('Service not found')
|
throw new Error("Service not found");
|
||||||
}
|
}
|
||||||
// query service
|
// query service
|
||||||
const service = await this.prisma.service.findUnique({
|
const service = await this.prisma.service.findUnique({
|
||||||
where: { id: args.data.service.connect.id },
|
where: { id: args.data.service.connect.id },
|
||||||
})
|
});
|
||||||
if (!service) {
|
if (!service) {
|
||||||
throw new Error('Service not found')
|
throw new Error("Service not found");
|
||||||
}
|
}
|
||||||
// check if user has already registered for this service
|
// check if user has already registered for this service
|
||||||
const userService = await this.prisma.schedule.findFirst({
|
const userService = await this.prisma.schedule.findFirst({
|
||||||
@@ -385,21 +396,55 @@ export class OrderSchema extends PothosSchema {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
if (userService) {
|
if (userService) {
|
||||||
throw new Error('User has already registered for this service')
|
throw new Error("User has already registered for this service");
|
||||||
|
}
|
||||||
|
// check if user have any scheduledate overlap time with input schedule
|
||||||
|
const overlapSchedule = await this.prisma.scheduleDate.findFirst({
|
||||||
|
where: {
|
||||||
|
AND: [
|
||||||
|
{
|
||||||
|
start: {
|
||||||
|
equals: args.data.schedule.connect?.scheduleStart as Date,
|
||||||
|
},
|
||||||
|
end: {
|
||||||
|
equals: args.data.schedule.connect?.scheduleEnd as Date,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
participantIds: {
|
||||||
|
has: ctx.me?.id ?? "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: {
|
||||||
|
notIn: [
|
||||||
|
ScheduleDateStatus.NOT_STARTED,
|
||||||
|
ScheduleDateStatus.IN_PROGRESS,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (overlapSchedule) {
|
||||||
|
throw new Error("User has already registered for this service");
|
||||||
}
|
}
|
||||||
// check if input schedule has order id then throw error
|
// check if input schedule has order id then throw error
|
||||||
const schedule = await this.prisma.schedule.findUnique({
|
const schedule = await this.prisma.schedule.findUnique({
|
||||||
where: { id: args.data.schedule.connect?.id ?? '' },
|
where: { id: args.data.schedule.connect?.id ?? "" },
|
||||||
})
|
});
|
||||||
if (schedule?.orderId) {
|
if (schedule?.orderId) {
|
||||||
// check if order status is PAID OR PENDING
|
// check if order status is PAID OR PENDING
|
||||||
const order = await this.prisma.order.findUnique({
|
const order = await this.prisma.order.findUnique({
|
||||||
where: { id: schedule.orderId },
|
where: { id: schedule.orderId },
|
||||||
})
|
});
|
||||||
if (order?.status === OrderStatus.PAID || order?.status === OrderStatus.PENDING) {
|
if (
|
||||||
throw new Error('Schedule already has an order')
|
order?.status === OrderStatus.PAID ||
|
||||||
|
order?.status === OrderStatus.PENDING
|
||||||
|
) {
|
||||||
|
throw new Error("Schedule already has an order");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const order = await this.prisma.order.create({
|
const order = await this.prisma.order.create({
|
||||||
@@ -407,31 +452,31 @@ export class OrderSchema extends PothosSchema {
|
|||||||
data: {
|
data: {
|
||||||
status: OrderStatus.PENDING,
|
status: OrderStatus.PENDING,
|
||||||
total: service.price,
|
total: service.price,
|
||||||
userId: ctx.me?.id ?? '',
|
userId: ctx.me?.id ?? "",
|
||||||
serviceId: service.id,
|
serviceId: service.id,
|
||||||
scheduleId: args.data.schedule.connect?.id ?? '',
|
scheduleId: args.data.schedule.connect?.id ?? "",
|
||||||
commission: service.commission ?? 0.0,
|
commission: service.commission ?? 0.0,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
// check if service is valid
|
// check if service is valid
|
||||||
if (!args.data.service.connect) {
|
if (!args.data.service.connect) {
|
||||||
throw new Error('Service not found')
|
throw new Error("Service not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if order is free
|
// check if order is free
|
||||||
if (order.total === 0) {
|
if (order.total === 0) {
|
||||||
// assign schedule
|
// assign schedule
|
||||||
await this.prisma.schedule.update({
|
await this.prisma.schedule.update({
|
||||||
where: { id: args.data.schedule.connect?.id ?? '' },
|
where: { id: args.data.schedule.connect?.id ?? "" },
|
||||||
data: {
|
data: {
|
||||||
orderId: order.id,
|
orderId: order.id,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
return order
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
// random integer
|
// random integer
|
||||||
const paymentCode = Math.floor(Math.random() * 1000000)
|
const paymentCode = Math.floor(Math.random() * 1000000);
|
||||||
// create payment
|
// create payment
|
||||||
const payment = await this.prisma.payment.create({
|
const payment = await this.prisma.payment.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -440,49 +485,57 @@ export class OrderSchema extends PothosSchema {
|
|||||||
paymentCode: paymentCode.toString(),
|
paymentCode: paymentCode.toString(),
|
||||||
expiredAt: DateTimeUtils.now().plus({ minutes: 15 }).toJSDate(),
|
expiredAt: DateTimeUtils.now().plus({ minutes: 15 }).toJSDate(),
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
const _name = _.deburr(service.name).slice(0, 10)
|
const _name = _.deburr(service.name).slice(0, 10);
|
||||||
Logger.log(`Creating payment for ${_name}`)
|
Logger.log(`Creating payment for ${_name}`);
|
||||||
// generate payment url
|
// generate payment url
|
||||||
const paymentData = await this.payosService.createPayment({
|
const paymentData = await this.payosService.createPayment({
|
||||||
orderCode: paymentCode,
|
orderCode: paymentCode,
|
||||||
amount: service.price,
|
amount: service.price,
|
||||||
description: _name,
|
description: _name,
|
||||||
buyerName: ctx.me?.name ?? '',
|
buyerName: ctx.me?.name ?? "",
|
||||||
buyerEmail: ctx.me?.email ?? '',
|
buyerEmail: ctx.me?.email ?? "",
|
||||||
returnUrl: `${process.env.PAYOS_RETURN_URL}`.replace('<serviceId>', service.id),
|
returnUrl: `${process.env.PAYOS_RETURN_URL}`.replace(
|
||||||
cancelUrl: `${process.env.PAYOS_RETURN_URL}`.replace('<serviceId>', service.id),
|
"<serviceId>",
|
||||||
expiredAt: DateTimeUtils.now().plus({ minutes: 15 }).toUnixInteger(),
|
service.id
|
||||||
})
|
),
|
||||||
|
cancelUrl: `${process.env.PAYOS_RETURN_URL}`.replace(
|
||||||
|
"<serviceId>",
|
||||||
|
service.id
|
||||||
|
),
|
||||||
|
expiredAt: DateTimeUtils.now()
|
||||||
|
.plus({ minutes: 15 })
|
||||||
|
.toUnixInteger(),
|
||||||
|
});
|
||||||
// update order payment id
|
// update order payment id
|
||||||
await this.prisma.order.update({
|
await this.prisma.order.update({
|
||||||
where: { id: order.id },
|
where: { id: order.id },
|
||||||
data: {
|
data: {
|
||||||
paymentId: payment.id,
|
paymentId: payment.id,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
// update payment url
|
// update payment url
|
||||||
await this.prisma.payment.update({
|
await this.prisma.payment.update({
|
||||||
where: { id: payment.id },
|
where: { id: payment.id },
|
||||||
data: {
|
data: {
|
||||||
paymentCode: paymentData.paymentLinkId,
|
paymentCode: paymentData.paymentLinkId,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
// refetch order
|
// refetch order
|
||||||
return await this.prisma.order.findUnique({
|
return await this.prisma.order.findUnique({
|
||||||
where: { id: order.id },
|
where: { id: order.id },
|
||||||
include: {
|
include: {
|
||||||
payment: true,
|
payment: true,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
deleteOrder: t.prismaField({
|
deleteOrder: t.prismaField({
|
||||||
type: this.order(),
|
type: this.order(),
|
||||||
description: 'Delete an existing order.',
|
description: "Delete an existing order.",
|
||||||
args: {
|
args: {
|
||||||
where: t.arg({
|
where: t.arg({
|
||||||
type: this.builder.generator.getWhereUnique('Order'),
|
type: this.builder.generator.getWhereUnique("Order"),
|
||||||
required: true,
|
required: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@@ -490,36 +543,39 @@ export class OrderSchema extends PothosSchema {
|
|||||||
return await this.prisma.order.delete({
|
return await this.prisma.order.delete({
|
||||||
...query,
|
...query,
|
||||||
where: args.where,
|
where: args.where,
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
updateOrder: t.prismaField({
|
updateOrder: t.prismaField({
|
||||||
type: this.order(),
|
type: this.order(),
|
||||||
description: 'Update an existing order.',
|
description: "Update an existing order.",
|
||||||
args: {
|
args: {
|
||||||
data: t.arg({
|
data: t.arg({
|
||||||
type: this.builder.generator.getUpdateInput('Order', ['status', 'total']),
|
type: this.builder.generator.getUpdateInput("Order", [
|
||||||
|
"status",
|
||||||
|
"total",
|
||||||
|
]),
|
||||||
required: true,
|
required: true,
|
||||||
}),
|
}),
|
||||||
where: t.arg({
|
where: t.arg({
|
||||||
type: this.builder.generator.getWhereUnique('Order'),
|
type: this.builder.generator.getWhereUnique("Order"),
|
||||||
required: true,
|
required: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
resolve: async (query, _root, args, ctx, _info) => {
|
resolve: async (query, _root, args, ctx, _info) => {
|
||||||
if (!ctx.me) {
|
if (!ctx.me) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
if (ctx.me.role !== Role.MODERATOR) {
|
if (ctx.me.role !== Role.MODERATOR) {
|
||||||
throw new Error('Unauthorized')
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
return await this.prisma.order.update({
|
return await this.prisma.order.update({
|
||||||
...query,
|
...query,
|
||||||
data: args.data,
|
data: args.data,
|
||||||
where: args.where,
|
where: args.where,
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}))
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user