forked from mojaloop/central-event-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndcAdjustment.js
120 lines (112 loc) · 4.06 KB
/
ndcAdjustment.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
/*****
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>
* Valentin Genev <valentin.genev@modusbox.com>
* Deon Botha <deon.botha@modusbox.com>
--------------
******/
'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
let engine = new RuleEngine.Engine()
const createRules = async (limit) => {
let rules = []
let { name, currency, type } = limit
try {
let dbEvent = await EventModel.findOne({
name,
currency,
limitType: type,
notificationEndpointType: `${type}_ADJUSTMENT_EMAIL`,
isActive: true
})
let conditions = {
all: [{
fact: 'name',
operator: 'equal',
value: name
}, {
fact: 'type',
operator: 'equal',
value: limit.type
}, {
fact: 'value',
operator: 'notEqual',
value: limit.oldValue
}]
}
let event = {
type: `${type}_ADJUSTMENT_EMAIL`,
params: {
dfsp: name,
limitType: type,
value: limit.value,
currency: limit.currency,
triggeredBy: limit._id,
repetitionsAllowed: limit.repetitions,
fromEvent: dbEvent.id,
action: dbEvent.action,
notificationEndpointType: dbEvent.notificationEndpointType,
templateType: dbEvent.templateType,
language: dbEvent.language,
messageSubject: `${type} LIMIT ADJUSTMENT`
}
}
let adjustmentRule = new RuleEngine.Rule({ conditions, event })
rules.push(adjustmentRule)
return { rules, event }
} catch (err) {
throw err
}
}
const ndcAdjustmentObservable = (limit) => {
return Rx.Observable.create(async observer => {
try {
let { rules, event } = await createRules(limit)
rules.forEach(rule => engine.addRule(rule)) // TODO check if it is a loop atm and if not remove the forEach and push
let actions = await engine.run(limit)
if (actions.length) {
actions.forEach(action => {
observer.next({
action: 'produceToKafkaTopic',
params: action.params
})
})
} else {
observer.next({ action: 'finish' })
let activeActions = await ActionModel.find({ fromEvent: event.params.fromEvent, isActive: true }) // TODO move this into the action observerbale
if (activeActions.length) {
for (let activeAction of activeActions) {
await ActionModel.findByIdAndUpdate(activeAction.id, { isActive: false })
}
}
}
rules.forEach(rule => engine.removeRule(rule))
observer.complete()
} catch (err) {
observer.error(err)
}
})
}
module.exports = {
ndcAdjustmentObservable
}