This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cd-organisations.js
84 lines (77 loc) · 2.24 KB
/
cd-organisations.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
const _ = require('lodash');
const { promisify } = require('bluebird');
const orgEntity = require('./lib/organisations/entities/org');
const userOrgEntity = require('./lib/organisations/entities/userOrg');
const orgController = require('./lib/organisations/controllers/org/index');
const userOrgController = require('./lib/organisations/controllers/userOrg/index');
// Utils
const pingCmd = require('./lib/ping');
function cdOrganisations() {
// https://github.com/senecajs/seneca/issues/112
const seneca = this.root;
const org = orgEntity.bind(seneca)();
const userOrg = userOrgEntity.bind(seneca)();
const acts = {};
const plugin = 'cd-organisations';
const senecaActP = promisify(seneca.act, { context: seneca });
function keyCb(entityName, key) {
return args =>
senecaActP(
_.extend(
{
role: plugin,
entity: entityName,
cmd: key,
},
args,
),
);
}
// Load primitives
[org, userOrg].forEach((entity) => {
acts[entity.name] = {};
Object.entries(entity.acts).forEach(([key, { validation, cb }]) => {
const act = _.extend(
{
role: plugin,
entity: entity.name,
cmd: key,
},
validation,
);
seneca.add(act, cb);
// Add a promise shortcut for controllers
acts[entity.name][key] = keyCb(entity.name, key);
seneca.log.debug(`added act role:${entity.name} cmd:${key}`);
});
});
// Load controllers
const ctrls = {};
ctrls.org = orgController.bind(seneca)();
ctrls.userOrg = userOrgController.bind(seneca)();
_.each(ctrls, (ctrl, entity) => {
Object.entries(ctrl.acts).forEach(([key, { validation, cb }]) => {
const act = _.extend(
{
role: plugin,
ctrl: entity,
cmd: key,
},
validation,
);
seneca.add(act, cb);
seneca.log.info('added act', act, { joi$: validation });
// No promise are added, we shouldn't have to reuse the same function twice.
// If we do, create an utility
});
});
// Load utilities
seneca.add({ role: plugin, cmd: 'ping' }, pingCmd);
return {
name: plugin,
exportmap: {
acts,
},
};
}
module.exports = cdOrganisations;