refactor api

This commit is contained in:
2024-10-13 18:07:13 +07:00
parent 263ff4e207
commit 1f86786e1a
7 changed files with 29 additions and 9 deletions

View File

@@ -10,13 +10,14 @@ services:
volumes: volumes:
- ./src:/app/src - ./src:/app/src
environment: environment:
- NODE_ENV=development - NODE_ENV=production
- DATABASE_URL=postgresql://your_username:your_password@10.0.27.1:5432/epess - DATABASE_URL=postgresql://your_username:your_password@10.0.27.1:5432/epess
- CLERK_PUBLISHABLE_KEY=pk_test_aW4tY2hpbXAtOTcuY2xlcmsuYWNjb3VudHMuZGV2JA - CLERK_PUBLISHABLE_KEY=pk_test_aW4tY2hpbXAtOTcuY2xlcmsuYWNjb3VudHMuZGV2JA
- CLERK_SECRET_KEY=sk_test_sA5lsb1GHwUNXWQCp5ev70QkaoF5EmdAHNWiCGwZF6 - 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 - 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 - LISTEN_PORT=3069
- SWAGGER_PATH=/v1 - SWAGGER_PATH=/swagger
- API_PATH=/v1
labels: labels:
- 'traefik.enable=true' - 'traefik.enable=true'
- 'traefik.http.routers.api.rule=Host(`api.epess.org`)' - 'traefik.http.routers.api.rule=Host(`api.epess.org`)'

View File

@@ -18,7 +18,7 @@ export class ClerkController {
@Post('webhook') @Post('webhook')
@ApiOperation({ summary: 'Clerk Webhook' }) @ApiOperation({ summary: 'Clerk Webhook' })
@ApiResponse({ status: 200, description: 'Webhook created successfully' }) @ApiResponse({ status: 200, description: 'Webhook created successfully' })
webhook(@Body() body: any) { webhook(@Headers() headers: any, @Body() body: any) {
return this.clerkService.webhook(body); return this.clerkService.webhook(body);
} }
} }

View File

@@ -4,6 +4,7 @@ import { PrismaService } from '../Prisma/prisma.service';
export class ClerkService { export class ClerkService {
constructor(private readonly prisma: PrismaService) {} constructor(private readonly prisma: PrismaService) {}
webhook(body: any) { webhook(body: any) {
// get the event type // get the event type
const eventType = body.type; const eventType = body.type;
// dispatch the event // dispatch the event
@@ -51,7 +52,6 @@ export class ClerkService {
} }
async eventUserCreated(data: any) { async eventUserCreated(data: any) {
console.log(data); console.log(data);
// create a user in the database
const primary_email_address_id = data.primary_email_address_id; 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 // get primary email address on email_addresses by querying email_addresses with primary_email_address_id
let primary_email_address = data.email_addresses.find( 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); 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) { eventSessionCreated(data: any) {

View File

@@ -1,6 +1,7 @@
import { ApolloDriverConfig } from '@nestjs/apollo'; import { ApolloDriverConfig } from '@nestjs/apollo';
import { Global, MiddlewareConsumer, Module } from '@nestjs/common'; import { Global, MiddlewareConsumer, Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql'; import { GraphQLModule } from '@nestjs/graphql';
import { ConfigModule } from '@nestjs/config';
import { PothosModule } from '@smatch-corp/nestjs-pothos'; import { PothosModule } from '@smatch-corp/nestjs-pothos';
import { PothosApolloDriver } from '@smatch-corp/nestjs-pothos-apollo-driver'; import { PothosApolloDriver } from '@smatch-corp/nestjs-pothos-apollo-driver';
import { Builder } from './graphql.builder'; import { Builder } from './graphql.builder';
@@ -31,6 +32,7 @@ import { UploadedDocumentModule } from '../UploadedDocument/uploadeddocument.mod
@Global() @Global()
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot(),
PrismaModule, PrismaModule,
UserModule, UserModule,
CenterModule, CenterModule,
@@ -60,6 +62,9 @@ import { UploadedDocumentModule } from '../UploadedDocument/uploadeddocument.mod
}), }),
GraphQLModule.forRoot<ApolloDriverConfig>({ GraphQLModule.forRoot<ApolloDriverConfig>({
driver: PothosApolloDriver, driver: PothosApolloDriver,
path: process.env.API_PATH + '/graphql',
playground: true,
introspection: true,
}), }),
], ],
providers: [ providers: [
@@ -80,6 +85,6 @@ export class GraphqlModule {
configure(consumer: MiddlewareConsumer) { configure(consumer: MiddlewareConsumer) {
consumer consumer
.apply(GraphQLValidationMiddleware) // Apply the custom middleware .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
} }
} }

View File

@@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export class RestfulService { export class RestfulService {
getAllItems() { getAllItems() {
throw new Error('Method not implemented.'); return 'Hello World';
} }
} }

View File

@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { GraphqlModule } from './Graphql/graphql.module'; import { GraphqlModule } from './Graphql/graphql.module';
import { ClerkModule } from './Clerk/clerk.module'; import { ClerkModule } from './Clerk/clerk.module';
import { RestfulModule } from './Restful/restful.module'; import { RestfulModule } from './Restful/restful.module';
@Module({ @Module({
imports: [GraphqlModule, ClerkModule, RestfulModule], imports: [ConfigModule.forRoot(), GraphqlModule, ClerkModule, RestfulModule],
}) })
export class AppModule {} export class AppModule {}

View File

@@ -13,6 +13,9 @@ async function bootstrap() {
credentials: true, credentials: true,
}); });
// set base path for api
app.setGlobalPrefix(process.env.API_PATH ?? '/v1');
const config = new DocumentBuilder() const config = new DocumentBuilder()
.setTitle('EPESS API') .setTitle('EPESS API')
.setDescription('API documentation for EPESS application') .setDescription('API documentation for EPESS application')
@@ -22,9 +25,13 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, config); const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup(process.env.SWAGGER_PATH ?? 'v1', app, document); 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: { get: {
tags: ['GraphQL'],
summary: 'GraphQL Playground', summary: 'GraphQL Playground',
description: description:
'Access the GraphQL Playground to interact with the GraphQL API.', 'Access the GraphQL Playground to interact with the GraphQL API.',