-
Notifications
You must be signed in to change notification settings - Fork 1
/
review-ukef-decision.controller.js
140 lines (128 loc) · 5.42 KB
/
review-ukef-decision.controller.js
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
const { format, differenceInBusinessDays, addBusinessDays } = require('date-fns');
const db = require('../../../drivers/db-client');
const CONSTANTS = require('../../../constants');
// helper function to retrieve the unissued facilities for MIN/AIN deals
const getUkefDecision = async (decision, bankId) => {
const dealsCollection = 'deals';
const queryDb = await db.getCollection(dealsCollection);
// return an array of objects that has the following format
// [{
// dealId: '12345678'
// bankInternalRefName: "HSBC 123"
// companyName: "Exporter Name"
// dateCreatedEpoch: 1642762583805
// dateOfApprovalEpoch: 1642753187
// dealType: "GEF"
// submissionDateEpoch: "1642762644833"
// }]
const deals = await queryDb.aggregate([
{ $unwind: '$ukefDecision' },
{ $match: { 'ukefDecision.decision': decision, 'bank.id': bankId } },
{
$project: {
_id: 0,
status: '$status',
dealId: '$_id',
bankInternalRefName: '$bankInternalRefName',
dealType: '$dealType',
companyName: '$exporter.companyName',
dateCreatedEpoch: {
$switch: {
branches: [
{
case: { $eq: ['$dealType', 'GEF'] },
then: '$createdAt'
},
{
case: { $eq: ['$dealType', 'BSS/EWCS'] },
then: '$details.created'
},
],
},
},
dateOfApprovalEpoch: '$ukefDecision.timestamp',
submissionDateEpoch: {
$switch: {
branches: [
{
case: { $eq: ['$dealType', 'GEF'] },
then: '$submissionDate'
},
{
case: { $eq: ['$dealType', 'BSS/EWCS'] },
then: '$details.submissionDate'
},
],
},
},
}
},
{
$match: {
// show only deals that have the `UKEF_APPROVED_WITHOUT_CONDITIONS` and `UKEF_APPROVED_WITH_CONDITIONS` status
status: {
$in: [CONSTANTS.DEAL.DEAL_STATUS.UKEF_APPROVED_WITHOUT_CONDITIONS, CONSTANTS.DEAL.DEAL_STATUS.UKEF_APPROVED_WITH_CONDITIONS],
},
},
},
{ $sort: { dateOfApprovalEpoch: 1 } }
]).toArray();
return deals;
};
// this function is used to retrieve the ukefDecision reports
// for Deals approved with or without conditions
exports.reviewUkefDecisionReports = async (req, res) => {
try {
// get the bankId to ensure that reports are specific for a specific bank
const bankId = req.user.bank.id;
// get the decision - this can be either approved with or without conditions
const ukefDecision = req.body.ukefDecision || req.query.ukefDecision || '';
const ukefDecisions = [];
if (ukefDecision === CONSTANTS.DEAL.DEAL_STATUS.UKEF_APPROVED_WITHOUT_CONDITIONS
|| ukefDecision === CONSTANTS.DEAL.DEAL_STATUS.UKEF_APPROVED_WITH_CONDITIONS) {
// ensure that the API call is performed only if a bankId is provided
const deals = bankId ? await getUkefDecision(ukefDecision, bankId) : [];
// check to see if there are any decisions waiting to be reviewed
if (deals.length) {
let deal;
let setDateToMidnight;
let dateOfApproval;
let defaultDate;
const todaysDate = new Date();
// loop through deals
deals.forEach((item) => {
deal = item;
// check if the date created is not null
defaultDate = item.dateCreatedEpoch || '';
setDateToMidnight = (new Date(parseInt(defaultDate, 10))).setHours(2, 0, 1, 0);
// format the date DD LLL YYYY (i.e. 18 April 2022)
deal.dateCreated = deal.dateCreatedEpoch ? format(setDateToMidnight, 'dd LLL yyyy') : '';
// check if the submission date is not null
defaultDate = item.submissionDateEpoch || '';
setDateToMidnight = (new Date(parseInt(defaultDate, 10))).setHours(2, 0, 1, 0);
// format the date DD LLL YYYY (i.e. 18 April 2022)
deal.submissionDate = deal.submissionDateEpoch ? format(setDateToMidnight, 'dd LLL yyyy') : '';
// check if the date of approval is not null
defaultDate = item.dateOfApprovalEpoch || '';
setDateToMidnight = (new Date(parseInt(defaultDate, 10))).setHours(2, 0, 1, 0);
// format the date DD LLL YYYY (i.e. 18 April 2022)
deal.dateOfApproval = deal.dateOfApprovalEpoch ? format(setDateToMidnight, 'dd LLL yyyy') : '';
if (ukefDecision === CONSTANTS.DEAL.DEAL_STATUS.UKEF_APPROVED_WITHOUT_CONDITIONS) {
// add `10 business days` to the date of approval if the deal is approved without conditions - as per ticket
dateOfApproval = addBusinessDays(setDateToMidnight, 10);
} else if (ukefDecision === CONSTANTS.DEAL.DEAL_STATUS.UKEF_APPROVED_WITH_CONDITIONS) {
// add `20 business days` to the date of approval if the deal is approved with conditions - as per ticket
dateOfApproval = addBusinessDays(setDateToMidnight, 20);
}
deal.daysToReview = defaultDate ? (differenceInBusinessDays(todaysDate, dateOfApproval) * -1 + 0) : 0; // +0 will cancel out negative zeros
ukefDecisions.push(deal);
setDateToMidnight = '';
});
}
}
res.status(200).send(ukefDecisions);
} catch (error) {
console.error('Unable to retrieve ukef\'s decision', { error });
}
res.status(200).send();
};