|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + HttpCode, |
| 5 | + HttpException, |
| 6 | + HttpStatus, |
| 7 | + Param, |
| 8 | + Post, |
| 9 | + Req, |
| 10 | + Res, |
| 11 | + UseGuards, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { ApiExcludeController } from '@nestjs/swagger'; |
| 14 | +import { UserSessionData } from '@novu/shared'; |
| 15 | +import { Request, Response } from 'express'; |
| 16 | +import { RequireAuthentication } from '../auth/framework/auth.decorator'; |
| 17 | +import { ExternalApiAccessible } from '../auth/framework/external-api.decorator'; |
| 18 | +import { UserSession } from '../shared/framework/user.decorator'; |
| 19 | +import { AgentReplyPayloadDto } from './dtos/agent-reply-payload.dto'; |
| 20 | +import { AgentConversationEnabledGuard } from './guards/agent-conversation-enabled.guard'; |
| 21 | +import { ChatSdkService } from './services/chat-sdk.service'; |
| 22 | +import { HandleAgentReplyCommand, Signal } from './usecases/handle-agent-reply/handle-agent-reply.command'; |
| 23 | +import { HandleAgentReply } from './usecases/handle-agent-reply/handle-agent-reply.usecase'; |
| 24 | + |
| 25 | +@Controller('/agents') |
| 26 | +@UseGuards(AgentConversationEnabledGuard) |
| 27 | +@ApiExcludeController() |
| 28 | +export class AgentsWebhookController { |
| 29 | + constructor( |
| 30 | + private chatSdkService: ChatSdkService, |
| 31 | + private handleAgentReplyUsecase: HandleAgentReply |
| 32 | + ) {} |
| 33 | + |
| 34 | + @Post('/:agentId/reply') |
| 35 | + @HttpCode(HttpStatus.OK) |
| 36 | + @RequireAuthentication() |
| 37 | + @ExternalApiAccessible() |
| 38 | + async handleAgentReply( |
| 39 | + @UserSession() user: UserSessionData, |
| 40 | + @Param('agentId') agentId: string, |
| 41 | + @Body() body: AgentReplyPayloadDto |
| 42 | + ) { |
| 43 | + return this.handleAgentReplyUsecase.execute( |
| 44 | + HandleAgentReplyCommand.create({ |
| 45 | + userId: user._id, |
| 46 | + environmentId: user.environmentId, |
| 47 | + organizationId: user.organizationId, |
| 48 | + conversationId: body.conversationId, |
| 49 | + agentIdentifier: agentId, |
| 50 | + integrationIdentifier: body.integrationIdentifier, |
| 51 | + reply: body.reply, |
| 52 | + update: body.update, |
| 53 | + resolve: body.resolve, |
| 54 | + signals: body.signals as Signal[], |
| 55 | + }) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Post('/:agentId/webhook/:integrationIdentifier') |
| 60 | + @HttpCode(HttpStatus.OK) |
| 61 | + async handleInboundWebhook( |
| 62 | + @Param('agentId') agentId: string, |
| 63 | + @Param('integrationIdentifier') integrationIdentifier: string, |
| 64 | + @Req() req: Request, |
| 65 | + @Res() res: Response |
| 66 | + ) { |
| 67 | + try { |
| 68 | + console.log('handleInboundWebhook', agentId, integrationIdentifier); |
| 69 | + await this.chatSdkService.handleWebhook(agentId, integrationIdentifier, req, res); |
| 70 | + console.log('handleInboundWebhook success'); |
| 71 | + } catch (err) { |
| 72 | + console.log(err); |
| 73 | + if (err instanceof HttpException) { |
| 74 | + res.status(err.getStatus()).json(err.getResponse()); |
| 75 | + } else { |
| 76 | + res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: 'Internal server error' }); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments