/
dashboard-activity-controller.js
188 lines (164 loc) · 5.8 KB
/
dashboard-activity-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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict'
// NPM dependencies
const _ = require('lodash')
const moment = require('moment-timezone')
const AWSXRay = require('aws-xray-sdk')
const getNamespace = require('continuation-local-storage').getNamespace
// Custom dependencies
const logger = require('../../utils/logger')(__filename)
const response = require('../../utils/response').response
const CORRELATION_HEADER = require('../../utils/correlation_header').CORRELATION_HEADER
const LedgerClient = require('../../services/clients/ledger_client')
const { isADirectDebitAccount } = require('../../services/clients/direct_debit_connector_client.js')
const auth = require('../../services/auth_service.js')
const { datetime } = require('@govuk-pay/pay-js-commons').nunjucksFilters
const {
NOT_STARTED,
ENTERED_ORGANISATION_NAME,
ENTERED_ORGANISATION_ADDRESS,
CHOSEN_PSP_STRIPE,
CHOSEN_PSP_WORLDPAY,
CHOSEN_PSP_SMARTPAY,
CHOSEN_PSP_EPDQ,
TERMS_AGREED_STRIPE,
TERMS_AGREED_WORLDPAY,
TERMS_AGREED_SMARTPAY,
TERMS_AGREED_EPDQ,
LIVE,
DENIED
} = require('../../models/go-live-stage')
// Constants
const clsXrayConfig = require('../../../config/xray-cls')
const links = {
manageService: 0,
demoPayment: 1,
testPaymentLink: 2,
directDebitPaymentFlow: 3,
paymentLinks: 4,
goLive: 5
}
const goLiveStartedStages = [
ENTERED_ORGANISATION_NAME,
ENTERED_ORGANISATION_ADDRESS,
CHOSEN_PSP_STRIPE,
CHOSEN_PSP_EPDQ,
CHOSEN_PSP_SMARTPAY,
CHOSEN_PSP_WORLDPAY
]
const goLiveRequestedStages = [
TERMS_AGREED_STRIPE,
TERMS_AGREED_EPDQ,
TERMS_AGREED_SMARTPAY,
TERMS_AGREED_WORLDPAY
]
const goLiveLinkNotDisplayedStages = [
LIVE,
DENIED
]
const getLinksToDisplay = function getLinksToDisplay (service, account, user) {
const linksToDisplay = [links.manageService]
if (account.payment_provider === 'sandbox') {
linksToDisplay.push(links.demoPayment)
linksToDisplay.push(links.testPaymentLink)
} else if (account.paymentMethod === 'direct debit') {
linksToDisplay.push(links.directDebitPaymentFlow)
} else {
linksToDisplay.push(links.paymentLinks)
}
if (displayGoLiveLink(service, account, user)) {
linksToDisplay.push(links.goLive)
}
return linksToDisplay
}
const displayGoLiveLink = (service, account, user) => {
return account.type === 'test' &&
(!goLiveLinkNotDisplayedStages.includes(service.currentGoLiveStage) &&
user.hasPermission(service.externalId, 'go-live-stage:read'))
}
module.exports = (req, res) => {
const gatewayAccountId = auth.getCurrentGatewayAccountId((req))
const correlationId = _.get(req, 'headers.' + CORRELATION_HEADER, '')
const period = _.get(req, 'query.period', 'today')
const linksToDisplay = getLinksToDisplay(req.service, req.account, req.user)
const model = {
name: req.user.username,
serviceId: req.service.externalId,
period,
links,
linksToDisplay,
goLiveNotStarted: req.service.currentGoLiveStage === NOT_STARTED,
goLiveStarted: goLiveStartedStages.includes(req.service.currentGoLiveStage),
goLiveRequested: goLiveRequestedStages.includes(req.service.currentGoLiveStage),
account: req.account
}
try {
const { fromDateTime, toDateTime } = getTransactionDateRange(period)
const transactionsPeriodString = `fromDate=${encodeURIComponent(datetime(fromDateTime, 'date'))}&fromTime=${encodeURIComponent(datetime(fromDateTime, 'time'))}&toDate=${encodeURIComponent(datetime(toDateTime, 'date'))}&toTime=${encodeURIComponent(datetime(toDateTime, 'time'))}`
logger.info(`[${correlationId}] successfully logged in`)
if (isADirectDebitAccount(gatewayAccountId)) {
// todo implement transaction list for direct debit
return response(req, res, 'dashboard/index', Object.assign(model, {
activityError: true
}))
}
const namespace = getNamespace(clsXrayConfig.nameSpaceName)
const clsSegment = namespace.get(clsXrayConfig.segmentKeyName)
AWSXRay.captureAsyncFunc('ledgerClient_getTransactionSummary', function (subsegment) {
LedgerClient.transactionSummary(gatewayAccountId, fromDateTime, toDateTime, { correlationId: correlationId })
.then(result => {
subsegment.close()
response(req, res, 'dashboard/index', Object.assign(model, {
activity: result,
successfulTransactionsState: 'payment-success',
fromDateTime,
toDateTime,
transactionsPeriodString
}))
})
.catch((error, ledgerResponse) => {
subsegment.close(error)
const status = _.get(ledgerResponse, 'statusCode', 404)
logger.error(`[${correlationId}] Calling ledger to get transactions summary failed`, {
service: 'ledger',
method: 'GET',
status,
error
})
res.status(status)
response(req, res, 'dashboard/index', Object.assign(model, {
activityError: true
}))
})
}, clsSegment)
} catch (err) {
logger.error(`[${correlationId}] ${err.message}`, {
service: 'frontend',
method: 'GET',
error: err,
status: err.status
})
res.status(err.status)
response(req, res, 'dashboard/index', Object.assign(model, {
activityError: true
}))
}
}
function getTransactionDateRange (period) {
const toDateTime = period === 'today'
? moment().tz('Europe/London').format()
: moment().tz('Europe/London').startOf('day').format()
let daysAgo = 0
switch (period) {
case 'yesterday':
daysAgo = 1
break
case 'previous-seven-days':
daysAgo = 8 // 7+1 because we count starting from yesterday
break
case 'previous-thirty-days':
daysAgo = 31 // 30+1 because we count starting from yesterday
break
}
const fromDateTime = moment().tz('Europe/London').startOf('day').subtract(daysAgo, 'days').format()
return { fromDateTime, toDateTime }
}