From a5e9ad5ac1e9ed19d342abacf8cb09fd99086ec7 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Sun, 27 Oct 2024 18:00:26 +0700 Subject: [PATCH] update order and fix approve center --- src/Center/center.schema.ts | 11 ++++++++++- src/Graphql/graphql.service.ts | 3 --- src/Order/order.schema.ts | 29 ++++++++++++++++++++++++++--- src/Prisma/prisma.service.ts | 23 +++++++++++++++-------- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/Center/center.schema.ts b/src/Center/center.schema.ts index 01f2231..6324a32 100644 --- a/src/Center/center.schema.ts +++ b/src/Center/center.schema.ts @@ -8,7 +8,7 @@ import { import { Builder } from '../Graphql/graphql.builder'; import { PrismaService } from '../Prisma/prisma.service'; import { MinioService } from '../Minio/minio.service'; -import { CenterStatus } from '@prisma/client'; +import { CenterStatus, Role } from '@prisma/client'; import { MailService } from '../Mail/mail.service'; @Injectable() export class CenterSchema extends PothosSchema { @@ -231,6 +231,15 @@ export class CenterSchema extends PothosSchema { if (!centerOwner) { throw new Error('Center owner not found'); } + await prisma.user.update({ + where: { + id: centerOwnerId, + }, + data: { + role: Role.CENTER_OWNER, + }, + }); + // update center status const updatedCenter = await prisma.center.update({ ...query, where: { diff --git a/src/Graphql/graphql.service.ts b/src/Graphql/graphql.service.ts index b4fdf5f..8b678f1 100644 --- a/src/Graphql/graphql.service.ts +++ b/src/Graphql/graphql.service.ts @@ -20,9 +20,6 @@ export class GraphqlService { } throw new UnauthorizedException('Must provide a session ID'); } - if (!sessionId) { - throw new UnauthorizedException('Session ID is required'); - } // check if the token is valid const session = await clerkClient.sessions.getSession(sessionId as string); if (!session) { diff --git a/src/Order/order.schema.ts b/src/Order/order.schema.ts index f1b91cb..f561664 100644 --- a/src/Order/order.schema.ts +++ b/src/Order/order.schema.ts @@ -125,9 +125,32 @@ export class OrderSchema extends PothosSchema { }), }, resolve: async (query, root, args, ctx, info) => { - return await this.prisma.order.create({ - ...query, - data: args.data, + return this.prisma.$transaction(async (prisma) => { + const order = await prisma.order.create({ + ...query, + data: args.data, + }); + // check if service is valid + if (!args.data.service.connect) { + throw new Error('Service not found'); + } + // check if service price is free + if (args.data.service.connect.price === 0) { + return order; + } + // generate payment code by prefix 'EPESS' + 6 hex digits + const paymentCode = + 'EPESS' + Math.random().toString(16).slice(2, 8); + // create payment + await prisma.payment.create({ + data: { + orderId: order.id, + amount: args.data.service.connect.price as number, + paymentCode: paymentCode, + expiredAt: new Date(Date.now() + 1000 * 60 * 60 * 24), + }, + }); + return order; }); }, }), diff --git a/src/Prisma/prisma.service.ts b/src/Prisma/prisma.service.ts index 0aecccf..5d463ca 100644 --- a/src/Prisma/prisma.service.ts +++ b/src/Prisma/prisma.service.ts @@ -41,25 +41,32 @@ export class PrismaService extends PrismaClient implements OnModuleInit { async onModuleInit() { this.logger.log('Try to connect database...'); - for (let attempt = 1; attempt <= 3; attempt++) { + const maxRetry = parseInt(process.env.PRISMA_MAX_RETRY as string) ?? 3; + const retryDelay = 10000; + + for (let attempt = 1; attempt <= maxRetry; attempt++) { try { await this.$connect(); - break; // Exit loop if connection is successful + this.logger.log('Connected.'); + return; } catch (error) { - if (attempt < (parseInt(process.env.PRISMA_MAX_RETRY as string) ?? 3)) { + if (attempt < maxRetry) { this.logger.warn( - `Connection attempt ${attempt} failed. Retrying in 10000ms...`, + `Connection attempt ${attempt} failed. Retrying in ${retryDelay}ms...`, ); - await new Promise((resolve) => setTimeout(resolve, 10000)); + await this.delay(retryDelay); } else { this.logger.error( - 'Failed to connect to the database after 3 attempts.', + `Failed to connect to the database after ${maxRetry} attempts.`, ); - throw error; // Rethrow the error after 3 failed attempts + throw error; } } } - this.logger.log('Connected.'); + } + + private delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); } async enableShutdownHooks(app: INestApplication) {