forked from mojaloop/central-settlement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
311 lines (287 loc) · 13.5 KB
/
Copy pathindex.js
File metadata and controls
311 lines (287 loc) · 13.5 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Gates Foundation organization for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>
* ModusBox
- Deon Botha <deon.botha@modusbox.com>
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Miguel de Barros <miguel.debarros@modusbox.com>
- Rajiv Mothilal <rajiv.mothilal@modusbox.com>
- Valentin Genev <valentin.genev@modusbox.com>
--------------
******/
'use strict'
const arrayDiff = require('lodash').difference
const SettlementModel = require('../../models/settlement')
const SettlementModelModel = require('../../models/settlement/settlementModel')
const SettlementWindowContentModel = require('../../models/settlementWindowContent')
const SettlementWindowModel = require('../../models/settlementWindow')
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const prepareParticipantsResult = (participantCurrenciesList) => {
const participantAccounts = {}
for (const account of participantCurrenciesList) {
const { id } = account
const formattedAccount = {
id: account.participantCurrencyId,
state: account.state,
reason: account.reason,
netSettlementAmount: {
amount: account.netAmount,
currency: account.currency
}
}
if (id in participantAccounts) {
const accountList = participantAccounts[id].accounts
accountList.push(formattedAccount)
participantAccounts[id] = {
id: id,
accounts: accountList
}
} else {
participantAccounts[id] = {
id: id,
accounts: [formattedAccount]
}
}
}
return Array.from(Object.keys(participantAccounts).map(participantId => participantAccounts[participantId]))
}
const groupSettlementWindowContentBySettlementWindow = (records) => {
const settlementWindows = {}
for (const record of records) {
const id = record.settlementWindowId
delete record.settlementWindowId
if (id in settlementWindows) {
settlementWindows[id].push(record)
} else {
settlementWindows[id] = record
}
}
return settlementWindows
}
module.exports = {
getById: async function ({ settlementId }, enums) {
const settlement = await SettlementModel.getById({ settlementId }, enums)
if (settlement) {
const settlementWindowsList = await SettlementWindowModel.getBySettlementId({ settlementId }, enums)
const participantCurrenciesList = await SettlementModel.settlementParticipantCurrency.getParticipantCurrencyBySettlementId({ settlementId }, enums)
const participants = prepareParticipantsResult(participantCurrenciesList)
return {
id: settlement.settlementId,
state: settlement.state,
reason: settlement.reason,
createdDate: settlement.createdDate,
changedDate: settlement.changedDate,
settlementWindows: settlementWindowsList,
participants
}
} else {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Settlement not found')
}
},
putById: SettlementModel.putById,
abortById: SettlementModel.abortById,
getSettlementsByParams: async function (params, enums) {
// 7 filters - at least one should be used
Object.keys(params.query).forEach(key => params.query[key] === undefined && delete params.query[key])
if (Object.keys(params.query).length && Object.keys(params.query).length < 10) {
const settlements = {}
let settlement
let participant
const settlementsData = await SettlementModel.getByParams(params.query, enums)
if (settlementsData && settlementsData.length > 0) {
for (const s of settlementsData) {
if (!settlements[s.settlementId]) {
settlements[s.settlementId] = {
id: s.settlementId,
state: s.settlementStateId
}
}
settlement = settlements[s.settlementId]
if (!settlement.settlementWindows) {
settlement.settlementWindows = {}
}
if (!settlement.settlementWindows[s.settlementWindowId]) {
settlement.settlementWindows[s.settlementWindowId] = {
id: s.settlementWindowId,
state: s.settlementWindowStateId,
reason: s.settlementWindowReason,
createdDate: s.createdDate,
changedDate: s.changedDate
}
}
if (!settlement.participants) {
settlement.participants = {}
}
if (!settlement.participants[s.participantId]) {
settlement.participants[s.participantId] = {
id: s.participantId
}
}
participant = settlement.participants[s.participantId]
if (!participant.accounts) {
participant.accounts = {}
}
participant.accounts[s.participantCurrencyId] = {
id: s.participantCurrencyId,
state: s.accountState,
reason: s.accountReason,
netSettlementAmount: {
amount: s.accountAmount,
currency: s.accountCurrency
}
}
}
// transform settlements map to result array
const result = Object.keys(settlements).map((i) => {
let settlementWindows = settlements[i].settlementWindows
settlementWindows = Object.keys(settlementWindows).map((j) => {
return settlementWindows[j]
})
settlements[i].settlementWindows = settlementWindows
let participants = settlements[i].participants
participants = Object.keys(participants).map((j) => {
let accounts = participants[j].accounts
accounts = Object.keys(accounts).map((k) => {
return accounts[k]
})
participants[j].accounts = accounts
return participants[j]
})
settlements[i].participants = participants
return settlements[i]
})
return result
} else {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Settlements not found')
}
} else {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Use at least one parameter: state, fromDateTime, toDateTime, currency, settlementWindowId, fromSettlementWindowDateTime, toSettlementWindowDateTime, participantId, accountId')
}
},
settlementEventTrigger: async function (params, enums) {
// validate settlement model
const { settlementModel, reason, settlementWindows } = params
const settlementModelData = await SettlementModelModel.getByName(settlementModel)
if (!settlementModelData) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Settlement model not found')
} else if (settlementModelData.settlementGranularityId !== enums.settlementGranularity.NET ||
settlementModelData.settlementInterchangeId !== enums.settlementInterchange.MULTILATERAL ||
settlementModelData.settlementDelayId !== enums.settlementDelay.DEFERRED) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Invalid settlement model')
}
// validate windows content
const idList = settlementWindows.map(v => v.id)
const applicableWindows = await SettlementWindowModel.getByListOfIds(idList, settlementModelData, enums.settlementWindowStates)
const applicableIdList = applicableWindows.map(v => v.settlementWindowId)
const nonApplicableIdList = arrayDiff(idList, applicableIdList)
if (nonApplicableIdList.length) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Inapplicable windows ${nonApplicableIdList.join(', ')}`)
}
// settlement event trigger
const settlementId = await SettlementModel.triggerSettlementEvent({ idList, reason }, settlementModelData, enums)
// retrieve resulting data for response
const settlement = await SettlementModel.getById({ settlementId })
const settlementWindowsList = await SettlementWindowModel.getBySettlementId({ settlementId })
const settlementWindowContentAll = await SettlementWindowContentModel.getBySettlementId(settlementId)
const settlementWindowsContent = groupSettlementWindowContentBySettlementWindow(settlementWindowContentAll)
const settlementWindowsWithContent = settlementWindowsList.map(record => {
record.content = settlementWindowsContent[record.id]
return record
})
const participantCurrenciesList = await SettlementModel.settlementParticipantCurrency.getParticipantCurrencyBySettlementId({ settlementId })
const participants = prepareParticipantsResult(participantCurrenciesList)
return {
id: settlement.settlementId,
settlementModel,
state: settlement.state,
reason: settlement.reason,
createdDate: settlement.createdDate,
changedDate: settlement.changedDate,
settlementWindows: settlementWindowsWithContent,
participants
}
},
getByIdParticipantAccount: async function ({ settlementId, participantId, accountId = null }, enums) {
let participantFoundInSettlement = false
const accountProvided = accountId > 0
let participantAndAccountMatched = !accountProvided
let accountFoundInSettlement = !accountProvided
const settlement = await SettlementModel.getById({ settlementId }, enums) // 3
const settlementFound = !!settlement
let settlementParticipantCurrencyIdList, account, settlementAccount
if (settlementFound) {
settlementParticipantCurrencyIdList = await SettlementModel.settlementParticipantCurrency.getAccountsInSettlementByIds({
settlementId,
participantId
}, enums) // 6
participantFoundInSettlement = settlementParticipantCurrencyIdList.length > 0
if (participantFoundInSettlement && accountProvided) {
account = await SettlementModel.checkParticipantAccountExists({
participantId,
accountId
}, enums) // 9
participantAndAccountMatched = !!account
if (participantAndAccountMatched) {
settlementAccount = await SettlementModel.getAccountInSettlement({
settlementId,
accountId
}, enums) // 12
accountFoundInSettlement = !!settlementAccount
}
}
}
let settlementWindows
let accounts
let participants
if (settlementFound && participantFoundInSettlement && participantAndAccountMatched && accountFoundInSettlement) {
if (accountProvided) { // 16
settlementWindows = await SettlementModel.settlementSettlementWindow.getWindowsBySettlementIdAndAccountId({
settlementId,
accountId
}, enums)
accounts = await SettlementModel.settlementParticipantCurrency.getSettlementAccountById(settlementAccount.settlementParticipantCurrencyId, enums)
participants = prepareParticipantsResult(accounts)
} else {
settlementWindows = await SettlementModel.settlementSettlementWindow.getWindowsBySettlementIdAndParticipantId({
settlementId,
participantId
}, enums)
const ids = settlementParticipantCurrencyIdList.map(record => record.settlementParticipantCurrencyId)
accounts = await SettlementModel.settlementParticipantCurrency.getSettlementAccountsByListOfIds(ids, enums)
participants = prepareParticipantsResult(accounts)
}
} else {
if (!settlementFound) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Settlement not found')
} else if (!participantFoundInSettlement) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Participant not in settlement')
} else if (!participantAndAccountMatched) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Provided account does not match any participant position account')
} else { // else if (!accountFoundInSettlement) { // else if changed to else for achieving 100% branch coverage (else path not taken)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Account not in settlement')
}
}
return {
id: settlement.settlementId,
state: settlement.state,
settlementWindows,
participants
}
}
}