forked from mojaloop/central-event-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndcBreach.js
113 lines (103 loc) · 4.07 KB
/
ndcBreach.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
/*****
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 <[email protected]>
* Valentin Genev <[email protected]>
* Deon Botha <[email protected]>
--------------
******/
'use strict'
const RuleEngine = require('json-rules-engine')
const Rx = require('rxjs')
const LimitModel = require('../../models/limits').limitModel
const EventModel = require('../../models/events').eventModel
const ActionModel = require('../../models/action').actionModel
const Enums = require('../../lib/enum')
let engine = new RuleEngine.Engine()
const createRules = async (position) => {
let rules = []
let [limit, dbEvent] = await Promise.all([
LimitModel.findOne({ name: position.name, currency: position.currency, type: Enums.limitNotificationMap.NET_DEBIT_CAP.enum }),
EventModel.findOne({
name: position.name,
currency: position.currency,
limitType: Enums.limitNotificationMap.NET_DEBIT_CAP.enum,
notificationEndpointType: Enums.limitNotificationMap.NET_DEBIT_CAP.NET_DEBIT_CAP_THRESHOLD_BREACH_EMAIL.enum,
isActive: true
})
])
let conditions = {
any: [{
fact: 'percentage',
operator: 'lessThanInclusive',
value: limit.threshold,
params: limit.type
}]
}
let event = {
type: limit.type,
params: {
dfsp: position.name,
limitType: limit.type,
value: position.percentage.toFixed(2),
position: position.positionValue,
triggeredBy: position.id,
repetitionsAllowed: limit.repetitions,
fromEvent: dbEvent.id,
action: dbEvent.action,
notificationEndpointType: dbEvent.notificationEndpointType,
templateType: dbEvent.templateType,
language: dbEvent.language,
messageSubject: `${limit.type} BREACH CONDITION REACHED`
}
}
let breachRule = new RuleEngine.Rule({ conditions, event })
rules.push(breachRule)
return { rules, dbEvent }
}
const ndcBreachObservable = ({ positions, message }) => {
return Rx.Observable.create(async observer => {
for (let position of positions) {
let { rules, dbEvent } = await createRules(position)
rules.forEach(rule => engine.addRule(rule))
let fact = Object.assign({}, position.toObject())
let actions = await engine.run(fact)
if (actions.length) {
actions.forEach(action => {
observer.next({
action: 'produceToKafkaTopic',
params: action.params,
message
})
})
} else {
observer.next({ action: 'finish' })
let activeActions = await ActionModel.find({ fromEvent: dbEvent.id, isActive: true })
if (activeActions) {
for (let activeAction of activeActions) {
await ActionModel.findByIdAndUpdate(activeAction.id, { isActive: false })
}
}
}
rules.forEach(rule => engine.removeRule(rule))
}
})
}
module.exports = {
ndcBreachObservable
}