-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-chat-box.controller.ts
More file actions
137 lines (121 loc) · 3.56 KB
/
simple-chat-box.controller.ts
File metadata and controls
137 lines (121 loc) · 3.56 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
import {
Controller,
Get,
Param,
Render,
Body,
Res,
Post,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import * as crypto from 'crypto';
import { UpdateUserDto } from './update-user.dto';
import axios from 'axios';
import { Response } from 'express';
const BRAND_AUTH_HASH_ALGORITHM = 'sha1';
const BRAND_AUTH_HASH_DIGEST = 'hex';
@Controller()
export class SimpleChatBoxController {
constructor(private readonly configService: ConfigService) {}
@Get('simple-chat-box')
@Render('simple-chat-box')
root() {
const chatBoxData = this.getChatBoxData();
const requestToken = this.generateToken(
this.configService.get('brandId'),
this.configService.get('chatBoxSecretKey'),
chatBoxData['xNonce'],
chatBoxData['timestamp'],
);
console.log('requestToken', requestToken);
return {
chatBoxLibraryUrl: this.configService.get('chatBoxLibraryUrl'),
chatBoxData: {
...chatBoxData,
token: requestToken,
},
};
}
generateToken(brandId, secretKey, xNonce, xTimestamp) {
const headerMissingToken = {
'x-nonce': xNonce,
'x-timestamp': xTimestamp,
'x-brand-id': brandId,
};
const sortedRequestProperties = Object.keys(headerMissingToken)
.sort()
.reduce((accumulator, key) => {
accumulator[key] = headerMissingToken[key];
return accumulator;
}, {});
console.log('sortedRequestProperties', sortedRequestProperties);
const requestString = new URLSearchParams(
sortedRequestProperties,
).toString();
console.log('requestString', requestString);
return crypto
.createHmac(BRAND_AUTH_HASH_ALGORITHM, secretKey)
.update(requestString)
.digest(BRAND_AUTH_HASH_DIGEST);
}
@Post('update-brand-user/:id')
updateUser(
@Param('id') id: string,
@Body() updateUserDto: UpdateUserDto,
@Res() res: Response,
) {
const displayName = updateUserDto.displayName;
const status = [1, true, '1', 'true'].includes(updateUserDto.status);
const xBrandId = this.configService.get('brandId');
const secretKey = this.configService.get('chatBoxSecretKey');
const xNonce = randomStringGenerator();
const xTimeStamp = Date.now();
const requestToken = this.generateToken(
xBrandId,
secretKey,
xNonce,
xTimeStamp,
);
return axios
.patch(
this.configService.get('chatBoxApiBaseUrl') + 'brand-chat/user/' + id,
{
displayName: displayName,
enabled: status,
},
{
headers: {
'x-brand-id': xBrandId,
'x-timestamp': xTimeStamp,
'x-nonce': xNonce,
'x-token': requestToken,
'Content-Type': 'application/json',
},
},
)
.then((response) => {
if (response.status !== 200) {
return res.send(JSON.stringify(response));
}
return res.redirect('/simple-chat-box');
})
.catch((error) => {
return res.send(error.toJSON());
});
}
getChatBoxData() {
return {
brandId: this.configService.get('brandId'),
chatName: 'One88 Góc bàn đề',
channelId: 'BANDE_123',
channelName: 'Bàn Đề - One88',
channelRoomId: 'BD-TG',
channelRoomName: 'Đài Tiền Giang',
userId: 'sample-user-id',
userDisplayName: 'sample-user-display-name',
timestamp: Date.now(),
xNonce: randomStringGenerator(),
};
}
}