-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifyRule.js
34 lines (24 loc) · 986 Bytes
/
modifyRule.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
const generateTemplate = require('./rules/link');
const RULE_STAGE = 'login_success';
const RULE_NAME = 'auth0-otp-extension';
const findIn = rules => rules.find(rule => rule.name === RULE_NAME);
// Allowing partial application to make usage with promises nicer
const persistRule = (api, generatedRule) => (rules = []) => {
const existingRule = rules.find(rule => rule.name === RULE_NAME);
if (existingRule) {
return api.update({ id: existingRule.id }, generatedRule);
}
return api.create({ stage: RULE_STAGE, ...generatedRule });
};
const destroyRule = api => (rules = []) => {
const existingRule = findIn(rules);
if (existingRule) {
api.delete({ id: existingRule.id });
}
};
const install = (api, config) => {
const rule = { name: RULE_NAME, script: generateTemplate(config), enabled: true };
return api.getAll().then(persistRule(api, rule));
};
const uninstall = api => api.getAll().then(destroyRule(api));
module.exports = { install, uninstall };