update service and category

This commit is contained in:
2024-11-11 18:55:24 +07:00
parent 705c4b53d8
commit e0a8b0aaa0
8 changed files with 130 additions and 20 deletions

View File

@@ -7,7 +7,7 @@ import { Request, Response } from 'express'
import SmartSubscriptionPlugin, {
subscribeOptionsFromIterator,
} from '@pothos/plugin-smart-subscriptions'
import ZodPlugin from '@pothos/plugin-zod'
import AuthzPlugin from '@pothos/plugin-authz'
import ErrorsPlugin from '@pothos/plugin-errors'
import type { FileUpload } from 'graphql-upload/processRequest.js'
@@ -92,6 +92,7 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
RelayPlugin,
ErrorsPlugin,
AuthzPlugin,
ZodPlugin,
],
smartSubscriptions: {
debounceDelay: 1000,
@@ -102,6 +103,13 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
return ctx.http.pubSub.asyncIterator(name)
}),
},
zod: {
// optionally customize how errors are formatted
validationError: (zodError, _args, _context, _info) => {
// the default behavior is to just throw the zod error directly
return zodError
},
},
relay: {},
prisma: {
client: prisma,

View File

@@ -31,6 +31,9 @@ export class ServiceAndCategorySchema extends PothosSchema {
subCategory: t.relation('subCategory', {
description: 'The sub category for the service and category.',
}),
isDeleted: t.exposeBoolean('isDeleted', {
description: 'Whether the service and category is deleted.',
}),
subCategoryId: t.exposeID('subCategoryId', {
description: 'The ID of the sub category.',
}),
@@ -41,12 +44,23 @@ export class ServiceAndCategorySchema extends PothosSchema {
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
serviceAndCategory: t.prismaField({
type: this.serviceAndCategory(),
args: this.builder.generator.findUniqueArgs('ServiceAndCategory'),
description: 'Retrieve a service and category by ID.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.serviceAndCategory.findUnique({
...query,
where: args.where,
})
},
}),
serviceAndCategories: t.prismaField({
type: [this.serviceAndCategory()],
args: this.builder.generator.findManyArgs('ServiceAndCategory'),
description:
'Retrieve a list of service and categories with optional filtering, ordering, and pagination.',
resolve: async (query, root, args, ctx, info) => {
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.serviceAndCategory.findMany({
...query,
skip: args.skip ?? undefined,
@@ -56,6 +70,41 @@ export class ServiceAndCategorySchema extends PothosSchema {
})
},
}),
createServiceAndCategory: t.prismaField({
type: this.serviceAndCategory(),
args: {
data: t.arg({
type: this.builder.generator.getCreateInput('ServiceAndCategory'),
required: true,
}),
},
description: 'Create a service and category.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.serviceAndCategory.create({
...query,
data: args.data,
})
},
}),
deleteServiceAndCategory: t.prismaField({
type: this.serviceAndCategory(),
args: {
where: t.arg({
type: this.builder.generator.getWhereUnique('ServiceAndCategory'),
required: true,
}),
},
description: 'Delete a service and category.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.serviceAndCategory.update({
...query,
where: args.where,
data: {
isDeleted: true,
},
})
},
}),
}))
}
}

View File

@@ -48,6 +48,20 @@ export class UserSchema extends PothosSchema {
bankAccountNumber: t.exposeString('bankAccountNumber', {
description: 'The bank account number of the user.',
}),
packageValue: t.exposeFloat('packageValue', {
description: 'The package value of the user.',
validate: (value) => {
if (typeof value !== 'number') {
return false
}
// value can be only 0 to 1
if (value < 0 || value > 1) {
return false
}
return true
},
nullable: true,
}),
role: t.exposeString('role', {
nullable: true,
description: 'The role of the user.',
@@ -284,15 +298,16 @@ export class UserSchema extends PothosSchema {
}
const buffer = Buffer.concat(chunks)
const { id: userId, imageUrl } = await clerkClient.users.updateUserProfileImage(id, {
file: new Blob([buffer]),
});
const { id: userId, imageUrl } =
await clerkClient.users.updateUserProfileImage(id, {
file: new Blob([buffer]),
})
await this.prisma.user.update({
where: { id: userId },
data: {
avatarUrl: imageUrl,
},
});
})
}
}

View File

@@ -73,20 +73,34 @@ async function bootstrap() {
},
}
// clerk middleware
app.use(clerkMiddleware({}))
try {
// clerk middleware
app.use(clerkMiddleware({}))
// graphql upload
app.use(
graphqlUploadExpress({
maxFileSize: 100 * 1024 * 1024, // 100 MB
maxFiles: 10,
}),
)
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
} catch (error: any) {
Logger.error(
`Error in file upload middleware: ${error.message}`,
'Bootstrap',
)
// Optionally, you can handle the error further or rethrow it
}
// graphql upload
app.use(
graphqlUploadExpress({
maxFileSize: 100 * 1024 * 1024, // 100 MB
maxFiles: 10,
}),
)
const host = process.env.LISTEN_HOST ?? '0.0.0.0'
const port = process.env.LISTEN_PORT ?? 3000 // Default to 3000 if LISTEN_PORT is not set
await app.listen(port, host, () => {
Logger.log(`Server is running on http://${host}:${port}`, 'Bootstrap')
})
}
bootstrap()
// IIFE
;(async () => {
await bootstrap()
})()

File diff suppressed because one or more lines are too long