update preview date

This commit is contained in:
2024-10-30 17:56:19 +07:00
parent 3114357515
commit a09785ec71
8 changed files with 347 additions and 23 deletions

View File

@@ -8,7 +8,7 @@ import {
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { MinioService } from '../Minio/minio.service'
import { ServiceStatus } from '@prisma/client'
import { Role, ServiceStatus } from '@prisma/client'
import { MailService } from '../Mail/mail.service'
@Injectable()
export class ServiceSchema extends PothosSchema {
@@ -119,7 +119,7 @@ export class ServiceSchema extends PothosSchema {
type: this.service(),
cursor: 'id',
args: this.builder.generator.findManyArgs('Service'),
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, _args, _ctx, _info) => {
return await this.prisma.service.findMany({
...query,
})
@@ -136,9 +136,57 @@ export class ServiceSchema extends PothosSchema {
description:
'Retrieve a list of services with optional filtering, ordering, and pagination.',
type: [this.service()],
args: this.builder.generator.findManyArgs('Service'),
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.service.findMany({
...query,
where: args.filter ?? undefined,
orderBy: args.orderBy ?? undefined,
skip: args.skip ?? undefined,
take: args.take ?? undefined,
cursor: args.cursor ?? undefined,
})
},
}),
servicesByCenter: t.prismaField({
description:
'Retrieve a list of services with optional filtering, ordering, and pagination.',
type: [this.service()],
args: this.builder.generator.findManyArgs('Service'),
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Not allowed')
}
// check role if user is mentor or center owner
const role = ctx.http.me.role
if (role !== Role.CENTER_MENTOR && role !== Role.CENTER_OWNER) {
throw new Error('Not allowed')
}
if (role === Role.CENTER_MENTOR) {
// load only service belong to center of current user
const managedServices = await this.prisma.managedService.findMany({
where: { mentorId: ctx.http.me.id },
})
if (!managedServices) {
throw new Error('Managed services not found')
}
// query services that have serviceId in managedServices
args.filter = {
id: { in: managedServices.map((service) => service.serviceId) },
}
}
// if role is center owner, load all services belong to center of current user
if (role === Role.CENTER_OWNER) {
const center = await this.prisma.center.findUnique({
where: { centerOwnerId: ctx.http.me.id },
})
if (!center) {
throw new Error('Center not found')
}
args.filter = { centerId: { in: [center.id] } }
}
return await this.prisma.service.findMany({
...query,
where: args.filter ?? undefined,
@@ -158,7 +206,7 @@ export class ServiceSchema extends PothosSchema {
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.service.findUnique({
...query,
where: args.input,
@@ -177,11 +225,16 @@ export class ServiceSchema extends PothosSchema {
type: this.service(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Service'),
type: this.builder.generator.getCreateInput('Service', ['user']),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Not allowed')
}
// replace userId with current user id
args.input.user = { connect: { id: ctx.http.me.id } }
return await this.prisma.service.create({
...query,
data: args.input,
@@ -201,7 +254,7 @@ export class ServiceSchema extends PothosSchema {
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.service.update({
...query,
where: args.where,
@@ -218,7 +271,7 @@ export class ServiceSchema extends PothosSchema {
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.service.delete({
...query,
where: args.where,
@@ -242,7 +295,7 @@ export class ServiceSchema extends PothosSchema {
required: false,
}),
},
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Not allowed')
}