mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-04-11 18:08:16 +00:00
fixed cors
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Server
|
# Server
|
||||||
PORT=4100
|
PORT=4100
|
||||||
CORS_ORIGIN=http://localhost:5173
|
CORS_ORIGINS=http://localhost:5173,http://localhost:8000
|
||||||
|
|
||||||
# Fortytwo Platform
|
# Fortytwo Platform
|
||||||
PLATFORM_GRAPHQL_URL=http://localhost:4000/graphql
|
PLATFORM_GRAPHQL_URL=http://localhost:4000/graphql
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
export default () => ({
|
export default () => ({
|
||||||
port: parseInt(process.env.PORT ?? '4100', 10),
|
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: {
|
platform: {
|
||||||
graphqlUrl: process.env.PLATFORM_GRAPHQL_URL ?? 'http://localhost:4000/graphql',
|
graphqlUrl: process.env.PLATFORM_GRAPHQL_URL ?? 'http://localhost:4000/graphql',
|
||||||
apiKey: process.env.PLATFORM_API_KEY ?? '',
|
apiKey: process.env.PLATFORM_API_KEY ?? '',
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,9 @@
|
|||||||
import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { PlatformModule } from '../platform/platform.module';
|
import { PlatformModule } from '../platform/platform.module';
|
||||||
import { LeadEmbedController } from './lead-embed.controller';
|
import { LeadEmbedController } from './lead-embed.controller';
|
||||||
import { EmbedCorsMiddleware } from './embed-cors.middleware';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [PlatformModule],
|
imports: [PlatformModule],
|
||||||
controllers: [LeadEmbedController],
|
controllers: [LeadEmbedController],
|
||||||
})
|
})
|
||||||
export class EmbedModule implements NestModule {
|
export class EmbedModule {}
|
||||||
configure(consumer: MiddlewareConsumer) {
|
|
||||||
consumer
|
|
||||||
.apply(EmbedCorsMiddleware)
|
|
||||||
.forRoutes(
|
|
||||||
{ path: 'embed/*', method: RequestMethod.ALL }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export class LeadEmbedController {
|
|||||||
|
|
||||||
@Post('create')
|
@Post('create')
|
||||||
async handleLeadCreation(@Body() body: Record<string, any>) {
|
async handleLeadCreation(@Body() body: Record<string, any>) {
|
||||||
|
console.log("Lead creation from embed received:", body);
|
||||||
this.logger.log(`Lead creation from embed received: ${JSON.stringify(body)}`);
|
this.logger.log(`Lead creation from embed received: ${JSON.stringify(body)}`);
|
||||||
|
|
||||||
const authHeader = this.apiKey ? `Bearer ${this.apiKey}` : '';
|
const authHeader = this.apiKey ? `Bearer ${this.apiKey}` : '';
|
||||||
@@ -138,7 +139,7 @@ export class LeadEmbedController {
|
|||||||
try {
|
try {
|
||||||
const activityType = body.type === 'consultation' || body.type === 'appointment'
|
const activityType = body.type === 'consultation' || body.type === 'appointment'
|
||||||
? 'APPOINTMENT_BOOKED'
|
? 'APPOINTMENT_BOOKED'
|
||||||
: 'FORM_SUBMITTED';
|
: 'CALL_RECEIVED';
|
||||||
|
|
||||||
let summary = 'Lead submitted via web form';
|
let summary = 'Lead submitted via web form';
|
||||||
if (body.type) {
|
if (body.type) {
|
||||||
|
|||||||
11
src/main.ts
11
src/main.ts
@@ -6,11 +6,14 @@ async function bootstrap() {
|
|||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
const config = app.get(ConfigService);
|
const config = app.get(ConfigService);
|
||||||
|
|
||||||
app.enableCors({
|
const corsOrigins = config.get<string[]>('corsOrigins') || ['http://localhost:5173'];
|
||||||
origin: config.get('corsOrigin'),
|
|
||||||
credentials: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
app.enableCors({
|
||||||
|
origin: corsOrigins,
|
||||||
|
credentials: true,
|
||||||
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
||||||
|
allowedHeaders: ['Content-Type', 'Accept', 'Authorization'],
|
||||||
|
});
|
||||||
|
|
||||||
const port = config.get('port');
|
const port = config.get('port');
|
||||||
await app.listen(port);
|
await app.listen(port);
|
||||||
|
|||||||
Reference in New Issue
Block a user