refactor source code

This commit is contained in:
2024-10-29 17:42:54 +07:00
parent 3b23d9e0b7
commit 152bb50da8
83 changed files with 8473 additions and 7577 deletions

View File

@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { ServiceSchema } from './service.schema';
import { Module } from '@nestjs/common'
import { ServiceSchema } from './service.schema'
@Module({
providers: [ServiceSchema],

View File

@@ -1,15 +1,15 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
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 { MinioService } from '../Minio/minio.service';
import { ServiceStatus } from '@prisma/client';
import { MailService } from '../Mail/mail.service';
} from '@smatch-corp/nestjs-pothos'
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { MinioService } from '../Minio/minio.service'
import { ServiceStatus } from '@prisma/client'
import { MailService } from '../Mail/mail.service'
@Injectable()
export class ServiceSchema extends PothosSchema {
constructor(
@@ -18,7 +18,7 @@ export class ServiceSchema extends PothosSchema {
private readonly minioService: MinioService,
private readonly mailService: MailService,
) {
super();
super()
}
@PothosRef()
@@ -107,7 +107,7 @@ export class ServiceSchema extends PothosSchema {
description: 'The managed service for the service.',
}),
}),
});
})
}
@Pothos()
@@ -122,12 +122,12 @@ export class ServiceSchema extends PothosSchema {
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.service.findMany({
...query,
});
})
},
totalCount: (query) => {
return this.prisma.service.count({
...query,
});
})
},
},
{},
@@ -146,7 +146,7 @@ export class ServiceSchema extends PothosSchema {
skip: args.skip ?? undefined,
take: args.take ?? undefined,
cursor: args.cursor ?? undefined,
});
})
},
}),
service: t.prismaField({
@@ -165,10 +165,10 @@ export class ServiceSchema extends PothosSchema {
include: {
feedbacks: true,
},
});
})
},
}),
}));
}))
// Mutation section
this.builder.mutationFields((t) => ({
@@ -185,7 +185,7 @@ export class ServiceSchema extends PothosSchema {
return await this.prisma.service.create({
...query,
data: args.input,
});
})
},
}),
updateService: t.prismaField({
@@ -206,7 +206,7 @@ export class ServiceSchema extends PothosSchema {
...query,
where: args.where,
data: args.input,
});
})
},
}),
deleteService: t.prismaField({
@@ -222,7 +222,7 @@ export class ServiceSchema extends PothosSchema {
return await this.prisma.service.delete({
...query,
where: args.where,
});
})
},
}),
approveOrRejectService: t.prismaField({
@@ -244,18 +244,18 @@ export class ServiceSchema extends PothosSchema {
},
resolve: async (query, root, args, ctx, info) => {
if (ctx.isSubscription) {
throw new Error('Not allowed');
throw new Error('Not allowed')
}
return await this.prisma.$transaction(async (prisma) => {
// check if service is already approved or rejected
const service = await prisma.service.findUnique({
where: { id: args.serviceId },
});
})
if (!service) {
throw new Error('Service not found');
throw new Error('Service not found')
}
if (service.status !== ServiceStatus.PENDING) {
throw new Error('Service is already approved or rejected');
throw new Error('Service is already approved or rejected')
}
// update service status
const updatedService = await prisma.service.update({
@@ -272,33 +272,33 @@ export class ServiceSchema extends PothosSchema {
},
},
},
});
})
// mail to all mentor or center owner for the center
const center = await prisma.center.findUnique({
where: { id: service.centerId },
});
})
if (!center?.centerOwnerId) {
throw new Error('Center owner not found');
throw new Error('Center owner not found')
}
const centerOwner = await prisma.user.findUnique({
where: { id: center.centerOwnerId },
});
})
if (!centerOwner) {
throw new Error('Center owner not found');
throw new Error('Center owner not found')
}
const centerMentor = await prisma.centerMentor.findMany({
where: { centerId: service.centerId },
});
const mentorIds = centerMentor.map((mentor) => mentor.mentorId);
})
const mentorIds = centerMentor.map((mentor) => mentor.mentorId)
// get mentor emails
const mentorEmails = await prisma.user.findMany({
where: { id: { in: mentorIds } },
});
Logger.log(mentorEmails, 'ServiceSchema');
})
Logger.log(mentorEmails, 'ServiceSchema')
const emails = [
centerOwner.email,
...mentorEmails.map((mentor) => mentor.email),
];
]
if (args.approve) {
await this.mailService.sendTemplateEmail(
emails,
@@ -308,7 +308,7 @@ export class ServiceSchema extends PothosSchema {
SERVICE_NAME: service.name,
CENTER_NAME: center.name,
},
);
)
} else {
await this.mailService.sendTemplateEmail(
emails,
@@ -319,12 +319,12 @@ export class ServiceSchema extends PothosSchema {
CENTER_NAME: center.name,
ADMIN_NOTE: args.adminNote ?? 'Không có lý do',
},
);
)
}
return updatedService;
});
return updatedService
})
},
}),
}));
}))
}
}