diff --git a/.env.example b/.env.example index fb41b17..6fed60c 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ # Server PORT=4100 -CORS_ORIGIN=http://localhost:5173 +CORS_ORIGINS=http://localhost:5173,http://localhost:8000 # Fortytwo Platform PLATFORM_GRAPHQL_URL=http://localhost:4000/graphql diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 48ad069..c4b4356 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -1,6 +1,9 @@ export default () => ({ port: parseInt(process.env.PORT ?? '4100', 10), - corsOrigin: process.env.CORS_ORIGIN ?? 'http://localhost:5173', + corsOrigins: (process.env.CORS_ORIGINS ?? 'http://localhost:5173') + .split(',') + .map(origin => origin.trim()) + .filter(origin => origin.length > 0), platform: { graphqlUrl: process.env.PLATFORM_GRAPHQL_URL ?? 'http://localhost:4000/graphql', apiKey: process.env.PLATFORM_API_KEY ?? '', diff --git a/src/embed/embed-cors.middleware.ts b/src/embed/embed-cors.middleware.ts deleted file mode 100644 index ff02539..0000000 --- a/src/embed/embed-cors.middleware.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Injectable, NestMiddleware, Logger } from '@nestjs/common'; -import { Request, Response, NextFunction } from 'express'; - -@Injectable() -export class EmbedCorsMiddleware implements NestMiddleware { - private readonly logger = new Logger(EmbedCorsMiddleware.name); - - use(req: Request, res: Response, next: NextFunction) { - const origin = req.headers.origin || '*'; - this.logger.debug(`Embed CORS middleware - ${req.method} ${req.path} from ${origin}`); - - // Set CORS headers for all requests - res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); - res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization'); - res.setHeader('Access-Control-Max-Age', '86400'); // 24 hours - - // Handle preflight requests - if (req.method === 'OPTIONS') { - this.logger.debug('Handling OPTIONS preflight request'); - res.status(204).end(); - return; - } - - next(); - } -} diff --git a/src/embed/embed.module.ts b/src/embed/embed.module.ts index 010405f..995338a 100644 --- a/src/embed/embed.module.ts +++ b/src/embed/embed.module.ts @@ -1,18 +1,9 @@ -import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common'; +import { Module } from '@nestjs/common'; import { PlatformModule } from '../platform/platform.module'; import { LeadEmbedController } from './lead-embed.controller'; -import { EmbedCorsMiddleware } from './embed-cors.middleware'; @Module({ imports: [PlatformModule], controllers: [LeadEmbedController], }) -export class EmbedModule implements NestModule { - configure(consumer: MiddlewareConsumer) { - consumer - .apply(EmbedCorsMiddleware) - .forRoutes( - { path: 'embed/*', method: RequestMethod.ALL } - ); - } -} +export class EmbedModule {} diff --git a/src/embed/lead-embed.controller.ts b/src/embed/lead-embed.controller.ts index 86b2b5d..ca6ac6c 100644 --- a/src/embed/lead-embed.controller.ts +++ b/src/embed/lead-embed.controller.ts @@ -16,6 +16,7 @@ export class LeadEmbedController { @Post('create') async handleLeadCreation(@Body() body: Record) { + console.log("Lead creation from embed received:", body); this.logger.log(`Lead creation from embed received: ${JSON.stringify(body)}`); const authHeader = this.apiKey ? `Bearer ${this.apiKey}` : ''; @@ -138,7 +139,7 @@ export class LeadEmbedController { try { const activityType = body.type === 'consultation' || body.type === 'appointment' ? 'APPOINTMENT_BOOKED' - : 'FORM_SUBMITTED'; + : 'CALL_RECEIVED'; let summary = 'Lead submitted via web form'; if (body.type) { diff --git a/src/main.ts b/src/main.ts index 4d42889..6ea8785 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,12 +6,15 @@ async function bootstrap() { const app = await NestFactory.create(AppModule); const config = app.get(ConfigService); + const corsOrigins = config.get('corsOrigins') || ['http://localhost:5173']; + app.enableCors({ - origin: config.get('corsOrigin'), + origin: corsOrigins, credentials: true, + methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'], + allowedHeaders: ['Content-Type', 'Accept', 'Authorization'], }); - const port = config.get('port'); await app.listen(port); console.log(`Helix Engage Server running on port ${port}`);