update order and fix approve center

This commit is contained in:
2024-10-27 18:00:26 +07:00
parent 71971d7ae4
commit a5e9ad5ac1
4 changed files with 51 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ import {
import { Builder } from '../Graphql/graphql.builder'; import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service'; import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from '../Minio/minio.service'; import { MinioService } from '../Minio/minio.service';
import { CenterStatus } from '@prisma/client'; import { CenterStatus, Role } from '@prisma/client';
import { MailService } from '../Mail/mail.service'; import { MailService } from '../Mail/mail.service';
@Injectable() @Injectable()
export class CenterSchema extends PothosSchema { export class CenterSchema extends PothosSchema {
@@ -231,6 +231,15 @@ export class CenterSchema extends PothosSchema {
if (!centerOwner) { if (!centerOwner) {
throw new Error('Center owner not found'); 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({ const updatedCenter = await prisma.center.update({
...query, ...query,
where: { where: {

View File

@@ -20,9 +20,6 @@ export class GraphqlService {
} }
throw new UnauthorizedException('Must provide a session ID'); throw new UnauthorizedException('Must provide a session ID');
} }
if (!sessionId) {
throw new UnauthorizedException('Session ID is required');
}
// check if the token is valid // check if the token is valid
const session = await clerkClient.sessions.getSession(sessionId as string); const session = await clerkClient.sessions.getSession(sessionId as string);
if (!session) { if (!session) {

View File

@@ -125,9 +125,32 @@ export class OrderSchema extends PothosSchema {
}), }),
}, },
resolve: async (query, root, args, ctx, info) => { resolve: async (query, root, args, ctx, info) => {
return await this.prisma.order.create({ return this.prisma.$transaction(async (prisma) => {
...query, const order = await prisma.order.create({
data: args.data, ...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;
}); });
}, },
}), }),

View File

@@ -41,25 +41,32 @@ export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() { async onModuleInit() {
this.logger.log('Try to connect database...'); 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 { try {
await this.$connect(); await this.$connect();
break; // Exit loop if connection is successful this.logger.log('Connected.');
return;
} catch (error) { } catch (error) {
if (attempt < (parseInt(process.env.PRISMA_MAX_RETRY as string) ?? 3)) { if (attempt < maxRetry) {
this.logger.warn( 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 { } else {
this.logger.error( 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<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
} }
async enableShutdownHooks(app: INestApplication) { async enableShutdownHooks(app: INestApplication) {