-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.controller.ts
More file actions
169 lines (157 loc) · 7.3 KB
/
app.controller.ts
File metadata and controls
169 lines (157 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Body, Controller, Post, Req, Res, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Request, Response } from 'express';
import {
ConversationCallbackWebhooks,
FaxCallbackWebhooks,
NumbersCallbackWebhooks,
SmsCallbackWebhooks,
VerificationCallbackWebhooks,
VoiceCallbackWebhooks,
} from '@sinch/sdk-core';
import { NumbersEventService } from '../services/numbers-event.service';
import { SmsEventService } from '../services/sms-event.service';
import { VerificationEventService } from '../services/verification-event.service';
import { VoiceEventService } from '../services/voice-event.service';
import { ConversationEventService } from '../services/conversation-event.service';
import { FaxEventService } from '../services/fax-event.service';
require('dotenv').config();
// Const for Conversation API
const SINCH_CONVERSATION_APP_SECRET = process.env.SINCH_CONVERSATION_APP_SECRET || '';
// Const for Numbers API
const SINCH_NUMBERS_CALLBACK_SECRET = process.env.SINCH_NUMBERS_CALLBACK_SECRET || '';
// Const for Voice and Verification APIs
const SINCH_APPLICATION_KEY = process.env.SINCH_APPLICATION_KEY || '';
const SINCH_APPLICATION_SECRET = process.env.SINCH_APPLICATION_SECRET || '';
@Controller()
export class AppController {
constructor(
private readonly conversationEventService: ConversationEventService,
private readonly faxEventService: FaxEventService,
private readonly numbersEventService: NumbersEventService,
private readonly smsEventService: SmsEventService,
private readonly verificationEventService: VerificationEventService,
private readonly voiceEventService: VoiceEventService) {}
@Post('/conversation')
public conversation(@Req() request: Request, @Res() res: Response) {
// Initialize the class that will be used to validate the request and parse it
const conversationCallbackWebhook = new ConversationCallbackWebhooks(SINCH_CONVERSATION_APP_SECRET);
// 1 - The first thing to do is to verify the request is legit and has not been tampered with
const validated = conversationCallbackWebhook.validateAuthenticationHeader(request.headers, request['rawBody']);
if (!validated) {
res.status(401).send('Invalid webhook signature');
return;
}
try {
// 2 - Before acting on the request, it must be parsed to verify it's supported and to revive its content
const event = conversationCallbackWebhook.parseEvent(request.body);
// 3 - Once steps 1 and 2 are ok, delegate the event management to the Conversation service
this.conversationEventService.handleEvent(event);
res.status(200).send();
} catch (error) {
console.error(error);
res.status(500).send();
}
}
@Post('/fax')
@UseInterceptors(FileInterceptor('file'))
public fax(@UploadedFile() file: Express.Multer.File, @Req() request: Request, @Res() res: Response) {
// Initialize the class that will be used to validate the request and parse it
const faxCallbackWebhook = new FaxCallbackWebhooks();
try {
// There is no request validation for the Fax API, so we can parse it and revive its content directly
const event = faxCallbackWebhook.parseEvent(request.body);
// Once the request has been revived, delegate the event management to the Fax service
const contentType = request.headers['content-type'];
this.faxEventService.handleEvent(event, contentType, file);
res.status(200).send();
} catch (error) {
console.error(error);
res.status(500).send();
}
}
@Post('/numbers')
public numbers(@Req() request: Request, @Res() res: Response) {
// Initialize the class that will be used to validate the request and parse it
const numbersCallbackWebhook = new NumbersCallbackWebhooks(SINCH_NUMBERS_CALLBACK_SECRET);
// 1 - The first thing to do is to verify the request is legit and has not been tampered
const validated = numbersCallbackWebhook.validateAuthenticationHeader(
request.headers, request['rawBody']);
if (!validated) {
res.status(401).send('Invalid signature');
return;
}
try {
// 2 - Before acting on the request, it must be parsed to verify it's supported and to revive its content
const event = numbersCallbackWebhook.parseEvent(request.body);
// 3 - Once steps 1 and 2 are ok, delegate the event management to the Numbers service
this.numbersEventService.handleEvent(event);
res.status(200).send();
} catch (error) {
console.error(error);
res.status(500).send();
}
}
@Post('/sms')
public sms(@Body() eventBody: any, @Res() res: Response) {
const smsCallbackWebhooks = new SmsCallbackWebhooks();
try {
// There is no request validation for the SMS API, so we can parse it and revive its content directly
const event = smsCallbackWebhooks.parseEvent(eventBody);
// Once the request has been revived, delegate the event management to the SMS service
this.smsEventService.handleEvent(event);
res.status(200).send();
} catch (error) {
console.error(error);
res.status(500).send();
}
}
@Post('/verification')
public verification(@Req() request: Request, @Res() res: Response) {
// Initialize the class that will be used to validate the request and parse it
const verificationCallbackWebhooks = new VerificationCallbackWebhooks({
applicationKey: SINCH_APPLICATION_KEY,
applicationSecret: SINCH_APPLICATION_SECRET
});
// 1 - The first thing to do is to verify the request is legit and has not been tampered
const validated = verificationCallbackWebhooks.validateAuthenticationHeader(
request.headers, request['rawBody'], request.path, request.method);
if (!validated) {
res.status(401).send('Invalid authorization');
return;
}
try {
// 2 - Before acting on the request, it must be parsed to verify it's supported and to revive its content
const event = verificationCallbackWebhooks.parseEvent(request.body);
// 3 - Once steps 1 and 2 are ok, delegate the event management to the Verifications service
this.verificationEventService.handleEvent(event, res);
} catch (error) {
console.error(error);
res.status(500).send();
}
}
@Post('/voice')
public voice (@Req() request: Request, @Res() res: Response) {
// Initialize the class that will be used to validate the request and parse it
const voiceCallbackWebhooks = new VoiceCallbackWebhooks({
applicationKey: SINCH_APPLICATION_KEY,
applicationSecret: SINCH_APPLICATION_SECRET
});
// 1 - The first thing to do is to verify the request is legit and has not been tampered
const validated = voiceCallbackWebhooks.validateAuthenticationHeader(
request.headers, request['rawBody'], request.path, request.method);
if (!validated) {
res.status(401).send('Invalid authorization');
return;
}
try {
// 2 - Before acting on the request, it must be parsed to verify it's supported and to revive its content
const event = voiceCallbackWebhooks.parseEvent(request.body);
// 3 - Once steps 1 and 2 are ok, delegate the event management to the Voice service
this.voiceEventService.handleEvent(event, res);
} catch (error) {
console.error(error);
res.status(500).send();
}
}
}