From 1f86786e1a2d110f3495f0ca4cf37d5809afd0ad Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Sun, 13 Oct 2024 18:07:13 +0700 Subject: [PATCH] refactor api --- compose.yaml | 5 +++-- src/Clerk/clerk.controller.ts | 2 +- src/Clerk/clerk.service.ts | 10 ++++++++-- src/Graphql/graphql.module.ts | 7 ++++++- src/Restful/restful.service.ts | 2 +- src/app.module.ts | 3 ++- src/main.ts | 9 ++++++++- 7 files changed, 29 insertions(+), 9 deletions(-) diff --git a/compose.yaml b/compose.yaml index 0a12f32..8250ba7 100644 --- a/compose.yaml +++ b/compose.yaml @@ -10,13 +10,14 @@ services: volumes: - ./src:/app/src environment: - - NODE_ENV=development + - NODE_ENV=production - DATABASE_URL=postgresql://your_username:your_password@10.0.27.1:5432/epess - CLERK_PUBLISHABLE_KEY=pk_test_aW4tY2hpbXAtOTcuY2xlcmsuYWNjb3VudHMuZGV2JA - CLERK_SECRET_KEY=sk_test_sA5lsb1GHwUNXWQCp5ev70QkaoF5EmdAHNWiCGwZF6 - CORS_ORIGIN=https://epess.org,https://admin.epess.org,https://center.epess.org,http://localhost:3000,http://localhost:3069,http://localhost:3001 - LISTEN_PORT=3069 - - SWAGGER_PATH=/v1 + - SWAGGER_PATH=/swagger + - API_PATH=/v1 labels: - 'traefik.enable=true' - 'traefik.http.routers.api.rule=Host(`api.epess.org`)' diff --git a/src/Clerk/clerk.controller.ts b/src/Clerk/clerk.controller.ts index 77aacee..c724735 100644 --- a/src/Clerk/clerk.controller.ts +++ b/src/Clerk/clerk.controller.ts @@ -18,7 +18,7 @@ export class ClerkController { @Post('webhook') @ApiOperation({ summary: 'Clerk Webhook' }) @ApiResponse({ status: 200, description: 'Webhook created successfully' }) - webhook(@Body() body: any) { + webhook(@Headers() headers: any, @Body() body: any) { return this.clerkService.webhook(body); } } diff --git a/src/Clerk/clerk.service.ts b/src/Clerk/clerk.service.ts index 40b00c8..e198cf8 100644 --- a/src/Clerk/clerk.service.ts +++ b/src/Clerk/clerk.service.ts @@ -4,6 +4,7 @@ import { PrismaService } from '../Prisma/prisma.service'; export class ClerkService { constructor(private readonly prisma: PrismaService) {} webhook(body: any) { + // get the event type const eventType = body.type; // dispatch the event @@ -51,7 +52,6 @@ export class ClerkService { } async eventUserCreated(data: any) { console.log(data); - // create a user in the database const primary_email_address_id = data.primary_email_address_id; // get primary email address on email_addresses by querying email_addresses with primary_email_address_id let primary_email_address = data.email_addresses.find( @@ -82,8 +82,14 @@ export class ClerkService { }); } - eventUserUpdated(data: any) { + async eventUserUpdated(data: any) { console.log(data); + const user_id = data.id; + const name = `${data.first_name} ${data.last_name}`; + await this.prisma.user.update({ + where: { id: user_id }, + data: { name: name }, + }); } eventSessionCreated(data: any) { diff --git a/src/Graphql/graphql.module.ts b/src/Graphql/graphql.module.ts index 98e711e..f2ad91b 100644 --- a/src/Graphql/graphql.module.ts +++ b/src/Graphql/graphql.module.ts @@ -1,6 +1,7 @@ import { ApolloDriverConfig } from '@nestjs/apollo'; import { Global, MiddlewareConsumer, Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; +import { ConfigModule } from '@nestjs/config'; import { PothosModule } from '@smatch-corp/nestjs-pothos'; import { PothosApolloDriver } from '@smatch-corp/nestjs-pothos-apollo-driver'; import { Builder } from './graphql.builder'; @@ -31,6 +32,7 @@ import { UploadedDocumentModule } from '../UploadedDocument/uploadeddocument.mod @Global() @Module({ imports: [ + ConfigModule.forRoot(), PrismaModule, UserModule, CenterModule, @@ -60,6 +62,9 @@ import { UploadedDocumentModule } from '../UploadedDocument/uploadeddocument.mod }), GraphQLModule.forRoot({ driver: PothosApolloDriver, + path: process.env.API_PATH + '/graphql', + playground: true, + introspection: true, }), ], providers: [ @@ -80,6 +85,6 @@ export class GraphqlModule { configure(consumer: MiddlewareConsumer) { consumer .apply(GraphQLValidationMiddleware) // Apply the custom middleware - .forRoutes('graphql'); // Ensure it only applies to the /graphql endpoint + .forRoutes(process.env.API_PATH + '/graphql'); // Ensure it only applies to the /graphql endpoint } } diff --git a/src/Restful/restful.service.ts b/src/Restful/restful.service.ts index 557478f..cacfa89 100644 --- a/src/Restful/restful.service.ts +++ b/src/Restful/restful.service.ts @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class RestfulService { getAllItems() { - throw new Error('Method not implemented.'); + return 'Hello World'; } } diff --git a/src/app.module.ts b/src/app.module.ts index 07e21ab..dd8b39b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; import { GraphqlModule } from './Graphql/graphql.module'; import { ClerkModule } from './Clerk/clerk.module'; import { RestfulModule } from './Restful/restful.module'; @Module({ - imports: [GraphqlModule, ClerkModule, RestfulModule], + imports: [ConfigModule.forRoot(), GraphqlModule, ClerkModule, RestfulModule], }) export class AppModule {} diff --git a/src/main.ts b/src/main.ts index c50cbff..9284c12 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,9 @@ async function bootstrap() { credentials: true, }); + // set base path for api + app.setGlobalPrefix(process.env.API_PATH ?? '/v1'); + const config = new DocumentBuilder() .setTitle('EPESS API') .setDescription('API documentation for EPESS application') @@ -22,9 +25,13 @@ async function bootstrap() { const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup(process.env.SWAGGER_PATH ?? 'v1', app, document); + console.log(process.env.API_PATH); + - document.paths['/graphql'] = { + + document.paths[process.env.API_PATH + '/graphql'] = { get: { + tags: ['GraphQL'], summary: 'GraphQL Playground', description: 'Access the GraphQL Playground to interact with the GraphQL API.',