-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathContactDetails.ts
73 lines (62 loc) · 2.41 KB
/
ContactDetails.ts
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
import express from 'express';
import type { Response } from 'express';
import { getLogger } from '../handler/LoggerHandler';
import { getHeadersWithAuthenticationToken, getTokenFromCookies } from '../utils';
import { jsonRequest } from '../handler/ManagerHandler';
import type {
ContactDetails,
HubspotResponse,
GetContactDetailsResponse,
PostContactDetailsResponse,
PostContactDetailsRequestBody
} from './ContactDetails.types';
import type { GenericErrorResponse } from '../types';
const logger = getLogger('ContactDetails');
const router = express.Router();
const sendDataToHubspot = (contactDetails: ContactDetails, token: string): Promise<HubspotResponse> => {
return jsonRequest('post', '/contacts', getHeadersWithAuthenticationToken(token), contactDetails);
};
const sendHubspotDataToManager = (hubspotData: HubspotResponse, token: string) => {
return jsonRequest('post', '/license', getHeadersWithAuthenticationToken(token), hubspotData);
};
const checkContactDetailsExistance = (token: string) => {
return jsonRequest('get', '/license-check', getHeadersWithAuthenticationToken(token));
};
const submitContactDetails = async (contactDetails: ContactDetails, token: string) => {
const hubspotResponse = await sendDataToHubspot(contactDetails, token);
return sendHubspotDataToManager(hubspotResponse, token);
};
router.use(express.json());
router.get('/', async (req, res: Response<GetContactDetailsResponse>) => {
const token = getTokenFromCookies(req);
try {
await checkContactDetailsExistance(token);
res.send({
contactDetailsReceived: true
});
} catch (error) {
// Customer ID not found
logger.warn(error);
res.send({
contactDetailsReceived: false
});
}
});
router.post<never, PostContactDetailsResponse | GenericErrorResponse, PostContactDetailsRequestBody>(
'/',
async (req, res): Promise<void> => {
const token = getTokenFromCookies(req);
const contactDetails = req.body;
try {
await submitContactDetails(contactDetails, token);
res.send({
status: 'ok'
});
} catch (error: any) {
const errorMessage = `Cannot submit contact details. Error: ${error.message}`;
logger.error(errorMessage);
res.status(400).send({ message: errorMessage });
}
}
);
export default router;