Commit 63f92223229010c0a4ec921e8ac1dbe0211d3a64
1 parent
278868c0
Fixé création de dossiers et corbeille
Résolution un peu à l'arrache des dépendances circulaires en les appelant lors de l'execution...
Showing
6 changed files
with
484 additions
and
481 deletions
Show diff stats
app/services/ConvsServ.js
1 | 1 | var ConvModl = require('../models/ConvModl'); |
2 | -var MessServ = require('../services/MessServ'); | |
2 | +var DosssServ = require('../services/DosssServ'); | |
3 | 3 | var async = require('async'); |
4 | 4 | |
5 | -var ConvsServ = {}; | |
5 | +var ConvsServ = module.exports = { | |
6 | 6 | |
7 | -ConvsServ.simple = ['_id', 'titre', 'parent', 'hidden']; | |
7 | + simple: ['_id', 'titre', 'parent', 'hidden'], | |
8 | 8 | |
9 | -ConvsServ.simpleData = function (convD, cb) { | |
10 | - // TODO Démarré par | |
11 | - // TODO Dernier message | |
12 | - var conv = {}; | |
13 | - for (var prop of ConvsServ.simple) { | |
14 | - conv[prop] = convD[prop]; | |
15 | - } | |
16 | - cb(null, conv); | |
17 | -}; | |
18 | - | |
19 | -ConvsServ.detailedData = function (convD, cb) { | |
20 | - async.parallel([ | |
21 | - function (cba) { | |
22 | - var conv = {}; | |
23 | - for (var prop of ConvsServ.simple) { | |
24 | - conv[prop] = convD[prop]; | |
25 | - } | |
26 | - cba(null, conv); | |
27 | - }, | |
28 | - function (cba) { | |
29 | - MessServ.children(convD._id, function (err, children) { | |
9 | + simpleData: function (convD, cb) { | |
10 | + // TODO Démarré par | |
11 | + // TODO Dernier message | |
12 | + var conv = {}; | |
13 | + for (var prop of ConvsServ.simple) { | |
14 | + conv[prop] = convD[prop]; | |
15 | + } | |
16 | + cb(null, conv); | |
17 | + }, | |
30 | 18 | |
31 | - if (err) { | |
32 | - cba(err); | |
33 | - } else { | |
34 | - async.map(children, MessServ.simpleData, cba); | |
19 | + detailedData: function (convD, cb) { | |
20 | + async.parallel([ | |
21 | + function (cba) { | |
22 | + var conv = {}; | |
23 | + for (var prop of ConvsServ.simple) { | |
24 | + conv[prop] = convD[prop]; | |
35 | 25 | } |
36 | - }); | |
37 | - } | |
38 | - ], function (err, res) { | |
39 | - if (err) { | |
40 | - cb(err); | |
41 | - } else { | |
42 | - conv = res[0]; | |
43 | - conv.messs = res[1]; | |
26 | + cba(null, conv); | |
27 | + }, | |
28 | + function (cba) { | |
29 | + MessServ = require('../services/MessServ') | |
30 | + MessServ.children(convD._id, function (err, children) { | |
31 | + if (err) { | |
32 | + cba(err); | |
33 | + } else { | |
34 | + async.map(children, MessServ.simpleData, cba); | |
35 | + } | |
36 | + }); | |
37 | + } | |
38 | + ], function (err, res) { | |
39 | + if (err) { | |
40 | + cb(err); | |
41 | + } else { | |
42 | + conv = res[0]; | |
43 | + conv.messs = res[1]; | |
44 | 44 | |
45 | - cb(null, conv); | |
46 | - } | |
47 | - }); | |
48 | -}; | |
45 | + cb(null, conv); | |
46 | + } | |
47 | + }); | |
48 | + }, | |
49 | 49 | |
50 | -ConvsServ.get = function (id, cb) { | |
51 | - ConvModl.findById(id, cb); | |
52 | -}; | |
50 | + get: function (id, cb) { | |
51 | + ConvModl.findById(id, cb); | |
52 | + }, | |
53 | 53 | |
54 | -ConvsServ.children = function (id, cb) { // Conversations filles du dossier en paramètre | |
55 | - ConvModl.find({ | |
56 | - parent: id, | |
57 | - $or: [{ | |
58 | - hidden: false | |
59 | - }, { | |
60 | - hidden: undefined | |
61 | - }] | |
62 | - }, cb); | |
63 | -}; | |
54 | + children: function (id, cb) { // Conversations filles du dossier en paramètre | |
55 | + ConvModl.find({ | |
56 | + parent: id, | |
57 | + $or: [{ | |
58 | + hidden: false | |
59 | + }, { | |
60 | + hidden: undefined | |
61 | + }] | |
62 | + }, cb); | |
63 | + }, | |
64 | 64 | |
65 | -ConvsServ.assert = function (conv, cb) { | |
66 | - cb(null, conv.titre, conv.parent); | |
67 | -}; | |
65 | + assert: function (conv, cb) { | |
66 | + cb(null, conv.titre, conv.parent); | |
67 | + }, | |
68 | 68 | |
69 | -ConvsServ.add = function (data, cb) { | |
70 | - ConvModl.create({ | |
71 | - titre: data.titre, | |
72 | - parent: data.parent | |
73 | - }, cb); | |
74 | -}; | |
69 | + add: function (data, cb) { | |
70 | + ConvModl.create({ | |
71 | + titre: data.titre, | |
72 | + parent: data.parent | |
73 | + }, cb); | |
74 | + }, | |
75 | 75 | |
76 | -ConvsServ.remove = function (id, cb) { | |
77 | - async.waterfall([function (cba) { | |
78 | - ConvsServ.get(id, cba); | |
79 | - }, function (conv, cba) { | |
80 | - cba(conv ? null : 'notfound', conv); | |
81 | - }, function (conv, cba) { | |
82 | - conv.parent = 'trash'; | |
83 | - conv.save(cba); | |
84 | - }], cb); | |
76 | + remove: function (id, cb) { | |
77 | + async.waterfall([function (cba) { | |
78 | + ConvsServ.get(id, cba); | |
79 | + }, function (conv, cba) { | |
80 | + cba(conv ? null : 'notfound', conv); | |
81 | + }, function (conv, cba) { | |
82 | + DosssServ.get('trash', function (err, trash) { | |
83 | + console.log(21, trash); | |
84 | + cba(err, conv, trash); | |
85 | + }); | |
86 | + }, function (conv, trash, cba) { | |
87 | + conv.parent = trash._id; | |
88 | + conv.save(cba); | |
89 | + }], cb); | |
90 | + } | |
85 | 91 | }; |
86 | - | |
87 | -module.exports = ConvsServ; | ... | ... |
app/services/DecryptServ.js
1 | 1 | var ursa = require('ursa'); |
2 | 2 | var fs = require('fs'); |
3 | 3 | |
4 | -var DecryptServ = {}; | |
4 | +var DecryptServ = { | |
5 | 5 | |
6 | -DecryptServ.decrypter = false; | |
6 | + decrypter: false, | |
7 | 7 | |
8 | -DecryptServ.whenOk = function (cb) { | |
9 | - if (this.encrypter) { | |
10 | - cb(); | |
11 | - } else { | |
12 | - this.prepare(cb); | |
13 | - } | |
14 | -}; | |
15 | - | |
16 | -DecryptServ.prepare = function (cb) { | |
17 | - fs.readFile('config/ci_com.pem', function (err, data) { | |
18 | - if (err) { | |
19 | - throw err; | |
8 | + whenOk: function (cb) { | |
9 | + if (this.encrypter) { | |
10 | + cb(); | |
11 | + } else { | |
12 | + this.prepare(cb); | |
20 | 13 | } |
21 | - this.decrypter = ursa.createPrivateKey(data); | |
22 | - cb(); | |
23 | - }); | |
24 | -}; | |
14 | + }, | |
25 | 15 | |
26 | -DecryptServ.preload = function (cb) { | |
27 | - this.whenOk(cb); | |
28 | -}; | |
16 | + prepare: function (cb) { // TODO Juste charger au lancement du script | |
17 | + fs.readFile('config/ci_com.pem', function (err, data) { | |
18 | + if (err) { | |
19 | + throw err; | |
20 | + } | |
21 | + this.decrypter = ursa.createPrivateKey(data); | |
22 | + cb(); | |
23 | + }); | |
24 | + }, | |
25 | + | |
26 | + preload: function (cb) { | |
27 | + this.whenOk(cb); | |
28 | + }, | |
29 | 29 | |
30 | -DecryptServ.decrypt = function (string, cb) { | |
31 | - this.whenOk(function () { | |
32 | - cb(this.decrypter.decrypt(string, 'base64', 'utf8', ursa.RSA_PKCS1_PADDING)); | |
33 | - }); | |
30 | + decrypt: function (string, cb) { | |
31 | + this.whenOk(function () { | |
32 | + cb(this.decrypter.decrypt(string, 'base64', 'utf8', ursa.RSA_PKCS1_PADDING)); | |
33 | + }); | |
34 | + } | |
34 | 35 | }; |
35 | 36 | |
36 | 37 | module.exports = DecryptServ; | ... | ... |
app/services/DosssServ.js
1 | 1 | var DossModl = require('../models/DossModl'); |
2 | -var ConvsServ = require('../services/ConvsServ'); | |
3 | 2 | var async = require('async'); |
4 | 3 | |
5 | -var DosssServ = {}; | |
6 | - | |
7 | 4 | (function init() { |
8 | 5 | DossModl.find({ |
9 | 6 | special: 'root' |
... | ... | @@ -27,107 +24,112 @@ var DosssServ = {}; |
27 | 24 | }); |
28 | 25 | })(); |
29 | 26 | |
30 | -DosssServ.simple = ['_id', 'titre', 'parent', 'special', 'hidden']; | |
31 | - | |
32 | -DosssServ.simpleData = function (dossD, cb) { | |
33 | - var doss = {}; | |
34 | - for (var prop of DosssServ.simple) { | |
35 | - doss[prop] = dossD[prop]; | |
36 | - } | |
37 | - // TODO Dernier message | |
38 | - cb(null, doss); | |
39 | -}; | |
27 | +var DosssServ = module.exports = { | |
40 | 28 | |
29 | + simple: ['_id', 'titre', 'parent', 'special', 'hidden'], | |
41 | 30 | |
42 | -DosssServ.detailedData = function (dossD, cb) { | |
43 | - async.parallel([ | |
44 | - function (cba) { | |
45 | - var doss = {}; | |
46 | - for (var prop of DosssServ.simple) { | |
47 | - doss[prop] = dossD[prop]; | |
48 | - } | |
49 | - cba(null, doss); | |
50 | - }, | |
51 | - function (cba) { | |
52 | - DosssServ.children(dossD._id, function (err, children) { | |
53 | - if (err) { | |
54 | - cba(err); | |
55 | - } else { | |
56 | - async.map(children, DosssServ.simpleData, cba); | |
57 | - } | |
58 | - }); | |
59 | - }, | |
60 | - function (cba) { | |
61 | - ConvsServ.children(dossD._id, function (err, children) { | |
62 | - if (err) { | |
63 | - cba(err); | |
64 | - } else { | |
65 | - async.map(children, ConvsServ.simpleData, cba); | |
66 | - } | |
67 | - }); | |
31 | + simpleData: function (dossD, cb) { | |
32 | + var doss = {}; | |
33 | + for (var prop of DosssServ.simple) { | |
34 | + doss[prop] = dossD[prop]; | |
68 | 35 | } |
69 | - ], function (err, res) { | |
70 | - if (err) { | |
71 | - cb(err); | |
72 | - } else { | |
73 | - doss = res[0]; | |
74 | - doss.dosss = res[1]; | |
75 | - doss.convs = res[2]; | |
76 | - cb(null, doss); | |
77 | - } | |
78 | - }); | |
79 | -}; | |
36 | + // TODO Dernier message | |
37 | + cb(null, doss); | |
38 | + }, | |
39 | + | |
80 | 40 | |
81 | -DosssServ.get = function (special, cb) { | |
82 | - console.log(special); | |
83 | - DossModl.findById(special).exec(function (err, doss) { | |
84 | - if (!err && doss) { | |
85 | - cb(null, doss); | |
86 | - } else { | |
87 | - DossModl.findOne({ | |
88 | - special: special | |
89 | - }).exec(function (err2, doss) { | |
90 | - if (err2) { | |
91 | - cb(err); | |
92 | - } else { | |
93 | - cb(null, doss ? doss : null); | |
41 | + detailedData: function (dossD, cb) { | |
42 | + async.parallel([ | |
43 | + function (cba) { | |
44 | + var doss = {}; | |
45 | + for (var prop of DosssServ.simple) { | |
46 | + doss[prop] = dossD[prop]; | |
94 | 47 | } |
95 | - }); | |
96 | - } | |
97 | - }); | |
98 | -}; | |
48 | + cba(null, doss); | |
49 | + }, | |
50 | + function (cba) { | |
51 | + DosssServ.children(dossD._id, function (err, children) { | |
52 | + if (err) { | |
53 | + cba(err); | |
54 | + } else { | |
55 | + async.map(children, DosssServ.simpleData, cba); | |
56 | + } | |
57 | + }); | |
58 | + }, | |
59 | + function (cba) { | |
60 | + ConvsServ = require('../services/ConvsServ'); | |
61 | + ConvsServ.children(dossD._id, function (err, children) { | |
62 | + if (err) { | |
63 | + cba(err); | |
64 | + } else { | |
65 | + async.map(children, ConvsServ.simpleData, cba); | |
66 | + } | |
67 | + }); | |
68 | + } | |
69 | + ], function (err, res) { | |
70 | + if (err) { | |
71 | + cb(err); | |
72 | + } else { | |
73 | + doss = res[0]; | |
74 | + doss.dosss = res[1]; | |
75 | + doss.convs = res[2]; | |
76 | + cb(null, doss); | |
77 | + } | |
78 | + }); | |
79 | + }, | |
99 | 80 | |
100 | -DosssServ.children = function (id, cb) { | |
101 | - DossModl.find({ | |
102 | - parent: id, | |
103 | - $or: [{ | |
104 | - hidden: false | |
105 | - }, { | |
106 | - hidden: undefined | |
107 | - }] | |
108 | - }, cb); | |
109 | -}; | |
81 | + get: function (special, cb) { | |
82 | + DossModl.findById(special).exec(function (err, doss) { | |
83 | + if (!err && doss) { | |
84 | + cb(null, doss); | |
85 | + } else { | |
86 | + DossModl.findOne({ | |
87 | + special: special | |
88 | + }).exec(function (err2, doss) { | |
89 | + if (err2) { | |
90 | + cb(err); | |
91 | + } else { | |
92 | + cb(null, doss ? doss : null); | |
93 | + } | |
94 | + }); | |
95 | + } | |
96 | + }); | |
97 | + }, | |
110 | 98 | |
111 | -DosssServ.assert = function (data, cb) { | |
112 | - cb(data.titre && data.parent); | |
113 | -}; | |
99 | + children: function (id, cb) { | |
100 | + DossModl.find({ | |
101 | + parent: id, | |
102 | + $or: [{ | |
103 | + hidden: false | |
104 | + }, { | |
105 | + hidden: undefined | |
106 | + }] | |
107 | + }, cb); | |
108 | + }, | |
114 | 109 | |
115 | -DosssServ.add = function (data, cb) { | |
116 | - DossModl.create({ | |
117 | - titre: data.titre, | |
118 | - parent: data.parent | |
119 | - }, cb); | |
120 | -}; | |
110 | + assert: function (data, cb) { | |
111 | + cb(null, data.titre && data.parent); | |
112 | + }, | |
121 | 113 | |
122 | -DosssServ.remove = function (id, cb) { | |
123 | - async.waterfall([function (cba) { | |
124 | - DosssServ.get(id, cba); | |
125 | - }, function (doss, cba) { | |
126 | - cba(doss ? null : 'notfound', doss); | |
127 | - }, function (doss, cba) { | |
128 | - doss.parent = 'trash'; | |
129 | - doss.save(cba); | |
130 | - }], cb); | |
131 | -}; | |
114 | + add: function (data, cb) { | |
115 | + DossModl.create({ | |
116 | + titre: data.titre, | |
117 | + parent: data.parent | |
118 | + }, cb); | |
119 | + }, | |
132 | 120 | |
133 | -module.exports = DosssServ; | |
121 | + remove: function (id, cb) { | |
122 | + async.waterfall([function (cba) { | |
123 | + DosssServ.get(id, cba); | |
124 | + }, function (doss, cba) { | |
125 | + cba(doss ? null : 'notfound', doss); | |
126 | + }, function (doss, cba) { | |
127 | + DosssServ.get('trash', function (err, trash) { | |
128 | + cba(err, doss, trash); | |
129 | + }); | |
130 | + }, function (doss, trash, cba) { | |
131 | + doss.parent = trash._id; | |
132 | + doss.save(cba); | |
133 | + }], cb); | |
134 | + } | |
135 | +}; | ... | ... |
app/services/MembresServ.js
... | ... | @@ -2,105 +2,103 @@ var MembreModl = require('../models/MembreModl'); |
2 | 2 | var PolyUserServ = require('../services/PolyUserServ'); |
3 | 3 | var async = require('async'); |
4 | 4 | |
5 | -var MembresServ = {}; | |
6 | - | |
7 | -MembresServ.public = ['_id', 'login', 'role']; | |
8 | - | |
9 | -MembresServ.simpleData = function (membreD, cb) { | |
10 | - async.parallel([ | |
11 | - function (cba) { | |
12 | - var membre = {}; | |
13 | - for (var prop of MembresServ.public) { | |
14 | - membre[prop] = membreD[prop]; | |
5 | +var MembresServ = module.exports = { | |
6 | + | |
7 | + public: ['_id', 'login', 'role'], | |
8 | + | |
9 | + simpleData: function (membreD, cb) { | |
10 | + async.parallel([ | |
11 | + function (cba) { | |
12 | + var membre = {}; | |
13 | + for (var prop of MembresServ.public) { | |
14 | + membre[prop] = membreD[prop]; | |
15 | + } | |
16 | + | |
17 | + cba(null, membre); | |
18 | + }, | |
19 | + function (cba) { | |
20 | + PolyUserServ.get(membreD.login, cba); | |
21 | + }, | |
22 | + function (cba) { | |
23 | + MembresServ.estMembre(membreD.login, cba); | |
24 | + }, | |
25 | + function (cba) { | |
26 | + MembresServ.estBureau(membreD.login, cba); | |
15 | 27 | } |
16 | - | |
17 | - cba(null, membre); | |
18 | - }, | |
19 | - function (cba) { | |
20 | - PolyUserServ.get(membreD.login, cba); | |
21 | - }, | |
22 | - function (cba) { | |
23 | - MembresServ.estMembre(membreD.login, cba); | |
24 | - }, | |
25 | - function (cba) { | |
26 | - MembresServ.estBureau(membreD.login, cba); | |
27 | - } | |
28 | - ], function (err, res) { | |
29 | - if (err) { | |
30 | - cb(err); | |
31 | - } else { | |
32 | - membre = res[0]; | |
33 | - | |
34 | - membre.nom = res[1].nom; | |
35 | - membre.section = res[1].section; | |
36 | - membre.membre = res[2]; | |
37 | - membre.bureau = res[3]; | |
38 | - cb(null, membre); | |
39 | - } | |
40 | - }); | |
41 | -}; | |
42 | - | |
43 | -MembresServ.get = function (id, cb) { | |
44 | - MembreModl.findById(id, cb); | |
45 | -}; | |
46 | - | |
47 | -MembresServ.getLogin = function (login, cb) { | |
48 | - MembreModl.findOne({ | |
49 | - login: login | |
50 | - }, cb); | |
51 | -}; | |
52 | - | |
53 | -MembresServ.list = function (cb) { | |
54 | - MembreModl.find({ | |
55 | - $or: [{ | |
56 | - hidden: false | |
57 | - }, { | |
58 | - hidden: undefined | |
59 | - }] | |
60 | - }, cb); | |
61 | -}; | |
62 | - | |
63 | -MembresServ.assert = function (membre, cb) { | |
64 | - | |
65 | - cb(null, membre.login && membre.role); | |
66 | -}; | |
67 | - | |
68 | -MembresServ.add = function (data, cb) { | |
69 | - | |
70 | - MembreModl.create({ | |
71 | - login: data.login, | |
72 | - role: data.role | |
73 | - }, cb); | |
74 | -}; | |
75 | - | |
76 | -MembresServ.remove = function (id, cb) { | |
77 | - MembreModl.remove({ | |
78 | - _id: id | |
79 | - }, cb); | |
80 | -}; | |
81 | - | |
82 | -MembresServ.estMembre = function (login, cb) { | |
83 | - MembreModl.findOne({ | |
84 | - login: login | |
85 | - }, function (err, data) { | |
86 | - if (!err && data) { | |
87 | - cb(null, true); | |
88 | - } else { | |
89 | - cb(null, false); | |
90 | - } | |
91 | - }); | |
92 | -}; | |
93 | - | |
94 | -MembresServ.estBureau = function (login, cb) { | |
95 | - MembreModl.findOne({ | |
96 | - login: login | |
97 | - }, function (err, data) { | |
98 | - if (!err && data && ['Président', 'Vice-président', 'Trésorier', 'Secrétaire'].indexOf(data.role) > -1) { | |
99 | - cb(null, true); | |
100 | - } else { | |
101 | - cb(null, false); | |
102 | - } | |
103 | - }); | |
28 | + ], function (err, res) { | |
29 | + if (err) { | |
30 | + cb(err); | |
31 | + } else { | |
32 | + membre = res[0]; | |
33 | + membre.nom = res[1].nom; | |
34 | + membre.section = res[1].section; | |
35 | + membre.membre = res[2]; | |
36 | + membre.bureau = res[3]; | |
37 | + cb(null, membre); | |
38 | + } | |
39 | + }); | |
40 | + }, | |
41 | + | |
42 | + get: function (id, cb) { | |
43 | + MembreModl.findById(id, cb); | |
44 | + }, | |
45 | + | |
46 | + getLogin: function (login, cb) { | |
47 | + MembreModl.findOne({ | |
48 | + login: login | |
49 | + }, cb); | |
50 | + }, | |
51 | + | |
52 | + list: function (cb) { | |
53 | + MembreModl.find({ | |
54 | + $or: [{ | |
55 | + hidden: false | |
56 | + }, { | |
57 | + hidden: undefined | |
58 | + }] | |
59 | + }, cb); | |
60 | + }, | |
61 | + | |
62 | + assert: function (membre, cb) { | |
63 | + | |
64 | + cb(null, membre.login && membre.role); | |
65 | + }, | |
66 | + | |
67 | + add: function (data, cb) { | |
68 | + | |
69 | + MembreModl.create({ | |
70 | + login: data.login, | |
71 | + role: data.role | |
72 | + }, cb); | |
73 | + }, | |
74 | + | |
75 | + remove: function (id, cb) { | |
76 | + MembreModl.remove({ | |
77 | + _id: id | |
78 | + }, cb); | |
79 | + }, | |
80 | + | |
81 | + estMembre: function (login, cb) { | |
82 | + MembreModl.findOne({ | |
83 | + login: login | |
84 | + }, function (err, data) { | |
85 | + if (!err && data) { | |
86 | + cb(null, true); | |
87 | + } else { | |
88 | + cb(null, false); | |
89 | + } | |
90 | + }); | |
91 | + }, | |
92 | + | |
93 | + estBureau: function (login, cb) { | |
94 | + MembreModl.findOne({ | |
95 | + login: login | |
96 | + }, function (err, data) { | |
97 | + if (!err && data && ['Président', 'Vice-président', 'Trésorier', 'Secrétaire'].indexOf(data.role) > -1) { | |
98 | + cb(null, true); | |
99 | + } else { | |
100 | + cb(null, false); | |
101 | + } | |
102 | + }); | |
103 | + } | |
104 | 104 | }; |
105 | - | |
106 | -module.exports = MembresServ; | ... | ... |
app/services/MessServ.js
... | ... | @@ -2,84 +2,83 @@ var MessModl = require('../models/MessModl'); |
2 | 2 | var MembresServ = require('../services/MembresServ'); |
3 | 3 | var async = require('async'); |
4 | 4 | |
5 | -var MesssServ = {}; | |
5 | +var MesssServ = module.exports = { | |
6 | 6 | |
7 | -MesssServ.simple = ['_id', 'login', 'content', 'conv', 'date', 'hidden']; | |
7 | + simple: ['_id', 'login', 'content', 'conv', 'date', 'hidden'], | |
8 | 8 | |
9 | -MesssServ.simpleData = function (messD, cb) { | |
9 | + simpleData: function (messD, cb) { | |
10 | 10 | |
11 | - async.parallel([ | |
12 | - function (cba) { | |
13 | - var mess = {}; | |
14 | - for (var prop of MesssServ.simple) { | |
15 | - mess[prop] = messD[prop]; | |
16 | - } | |
17 | - cba(null, mess); | |
18 | - }, | |
19 | - function (cba) { | |
20 | - async.waterfall([ | |
21 | - function (cbaa) { | |
22 | - | |
23 | - MembresServ.getLogin(messD.login, cbaa); | |
24 | - }, | |
25 | - function (membre, cbaa) { | |
26 | - MembresServ.simpleData(membre, cbaa); | |
11 | + async.parallel([ | |
12 | + function (cba) { | |
13 | + var mess = {}; | |
14 | + for (var prop of MesssServ.simple) { | |
15 | + mess[prop] = messD[prop]; | |
27 | 16 | } |
28 | - ], cba); | |
29 | - } | |
30 | - ], function (err, res) { | |
31 | - if (err) { | |
32 | - cb(err); | |
33 | - } else { | |
34 | - mess = res[0]; | |
35 | - mess.auteur = res[1]; | |
36 | - cb(null, mess); | |
37 | - } | |
38 | - }); | |
39 | -}; | |
17 | + cba(null, mess); | |
18 | + }, | |
19 | + function (cba) { | |
20 | + async.waterfall([ | |
21 | + function (cbaa) { | |
40 | 22 | |
41 | -MesssServ.get = function (id, cb) { | |
42 | - MessModl.findById(id, cb); | |
43 | -}; | |
23 | + MembresServ.getLogin(messD.login, cbaa); | |
24 | + }, | |
25 | + function (membre, cbaa) { | |
26 | + MembresServ.simpleData(membre, cbaa); | |
27 | + } | |
28 | + ], cba); | |
29 | + } | |
30 | + ], function (err, res) { | |
31 | + if (err) { | |
32 | + cb(err); | |
33 | + } else { | |
34 | + mess = res[0]; | |
35 | + mess.auteur = res[1]; | |
36 | + cb(null, mess); | |
37 | + } | |
38 | + }); | |
39 | + }, | |
44 | 40 | |
45 | -MesssServ.children = function (conv, cb) { | |
46 | - MessModl.find({ | |
47 | - conv: conv, | |
48 | - $or: [{ | |
49 | - hidden: false | |
50 | - }, { | |
51 | - hidden: undefined | |
52 | - }] | |
53 | - }, cb); | |
54 | -}; | |
41 | + get: function (id, cb) { | |
42 | + MessModl.findById(id, cb); | |
43 | + }, | |
55 | 44 | |
56 | -MesssServ.assert = function (mess, cb) { | |
57 | - cb(null, mess.login && mess.content && mess.conv); | |
58 | -}; | |
45 | + children: function (conv, cb) { | |
46 | + MessModl.find({ | |
47 | + conv: conv, | |
48 | + $or: [{ | |
49 | + hidden: false | |
50 | + }, { | |
51 | + hidden: undefined | |
52 | + }] | |
53 | + }, cb); | |
54 | + }, | |
59 | 55 | |
60 | -MesssServ.add = function (data, cb) { | |
61 | - MessModl.create({ | |
62 | - content: data.content, | |
63 | - login: data.login, | |
64 | - conv: data.conv | |
65 | - }, cb); | |
66 | -}; | |
56 | + assert: function (mess, cb) { | |
57 | + cb(null, mess.login && mess.content && mess.conv); | |
58 | + }, | |
67 | 59 | |
68 | -MesssServ.edit = function (mess, data, cb) { | |
69 | - mess.content = data.content; | |
70 | - // TODO Edit date | |
71 | - mess.save(cb); | |
72 | -}; | |
60 | + add: function (data, cb) { | |
61 | + MessModl.create({ | |
62 | + content: data.content, | |
63 | + login: data.login, | |
64 | + conv: data.conv | |
65 | + }, cb); | |
66 | + }, | |
73 | 67 | |
74 | -MesssServ.remove = function (id, cb) { | |
75 | - async.waterfall([function (cba) { | |
76 | - MesssServ.get(id, cba); | |
77 | - }, function (mess, cba) { | |
78 | - cba(mess ? null : 'notfound', mess); | |
79 | - }, function (mess, cba) { | |
80 | - mess.hidden = true; | |
81 | - mess.save(cba); | |
82 | - }], cb); | |
83 | -}; | |
68 | + edit: function (mess, data, cb) { | |
69 | + mess.content = data.content; | |
70 | + // TODO Edit date | |
71 | + mess.save(cb); | |
72 | + }, | |
84 | 73 | |
85 | -module.exports = MesssServ; | |
74 | + remove: function (id, cb) { | |
75 | + async.waterfall([function (cba) { | |
76 | + MesssServ.get(id, cba); | |
77 | + }, function (mess, cba) { | |
78 | + cba(mess ? null : 'notfound', mess); | |
79 | + }, function (mess, cba) { | |
80 | + mess.hidden = true; | |
81 | + mess.save(cba); | |
82 | + }], cb); | |
83 | + } | |
84 | +}; | ... | ... |
app/services/PolyUserServ.js
... | ... | @@ -8,132 +8,131 @@ var cache = new NodeCache({ |
8 | 8 | stdTTL: 24 * 60 * 60 |
9 | 9 | }); |
10 | 10 | |
11 | -var PolyUserServ = {}; | |
11 | +var PolyUserServ = module.exports = { | |
12 | 12 | |
13 | -PolyUserServ.readPasswd = function (login, cb) { | |
14 | - passwdF = 'config/passwd'; | |
15 | - fs.readFile(passwdF, function (err, file) { | |
16 | - if (err) { | |
17 | - cb(err); | |
18 | - } else { | |
19 | - lines = file.toString('utf8').split('\n'); | |
20 | - async.detect(lines, function (line, cba) { | |
21 | - ex = line.split(':'); | |
22 | - cba(ex[0] == login); | |
23 | - }, function (res) { | |
24 | - if (res) { | |
25 | - ex = res.split(':'); | |
26 | - cb(null, { | |
27 | - 'username': ex[0], | |
28 | - 'password': ex[1], | |
29 | - 'UID': ex[2], | |
30 | - 'GID': ex[3], | |
31 | - 'GECOS': ex[4], | |
32 | - 'home': ex[5], | |
33 | - 'shell': ex[6] | |
34 | - }); | |
35 | - } else { | |
36 | - cb(null, null); | |
37 | - } | |
38 | - }); | |
39 | - } | |
40 | - }); | |
41 | -}; | |
13 | + readPasswd: function (login, cb) { | |
14 | + passwdF = 'config/passwd'; | |
15 | + fs.readFile(passwdF, function (err, file) { | |
16 | + if (err) { | |
17 | + cb(err); | |
18 | + } else { | |
19 | + lines = file.toString('utf8').split('\n'); | |
20 | + async.detect(lines, function (line, cba) { | |
21 | + ex = line.split(':'); | |
22 | + cba(ex[0] == login); | |
23 | + }, function (res) { | |
24 | + if (res) { | |
25 | + ex = res.split(':'); | |
26 | + cb(null, { | |
27 | + 'username': ex[0], | |
28 | + 'password': ex[1], | |
29 | + 'UID': ex[2], | |
30 | + 'GID': ex[3], | |
31 | + 'GECOS': ex[4], | |
32 | + 'home': ex[5], | |
33 | + 'shell': ex[6] | |
34 | + }); | |
35 | + } else { | |
36 | + cb(null, null); | |
37 | + } | |
38 | + }); | |
39 | + } | |
40 | + }); | |
41 | + }, | |
42 | 42 | |
43 | -PolyUserServ.readGroup = function (gid, cb) { | |
44 | - groupF = 'config/group'; | |
45 | - fs.readFile(groupF, function (err, file) { | |
46 | - if (err) { | |
47 | - cb(err); | |
48 | - } else { | |
49 | - lines = file.toString('utf8').split('\n'); | |
50 | - async.detect(lines, function (line, cba) { | |
51 | - ex = line.split(':'); | |
52 | - cba(ex[2] == gid); | |
53 | - }, function (res) { | |
54 | - if (res) { | |
55 | - ex = res.split(':'); | |
56 | - cb(null, { | |
57 | - 'name': ex[0], | |
58 | - 'password': ex[1], | |
59 | - 'GID': ex[2], | |
60 | - 'list': ex[3] | |
43 | + readGroup: function (gid, cb) { | |
44 | + groupF = 'config/group'; | |
45 | + fs.readFile(groupF, function (err, file) { | |
46 | + if (err) { | |
47 | + cb(err); | |
48 | + } else { | |
49 | + lines = file.toString('utf8').split('\n'); | |
50 | + async.detect(lines, function (line, cba) { | |
51 | + ex = line.split(':'); | |
52 | + cba(ex[2] == gid); | |
53 | + }, function (res) { | |
54 | + if (res) { | |
55 | + ex = res.split(':'); | |
56 | + cb(null, { | |
57 | + 'name': ex[0], | |
58 | + 'password': ex[1], | |
59 | + 'GID': ex[2], | |
60 | + 'list': ex[3] | |
61 | + }); | |
62 | + } else { | |
63 | + cb(null, null); | |
64 | + } | |
65 | + }); | |
66 | + } | |
67 | + }); | |
68 | + }, | |
69 | + | |
70 | + grabInfos: function (login, cb) { | |
71 | + async.waterfall([ | |
72 | + function (cba) { | |
73 | + PolyUserServ.readPasswd(login, cba); | |
74 | + }, | |
75 | + function (passwd, cba) { | |
76 | + if (passwd && passwd.GID) { | |
77 | + PolyUserServ.readGroup(passwd.GID, function (err, group) { | |
78 | + cba(err, passwd, group); | |
61 | 79 | }); |
62 | 80 | } else { |
63 | - cb(null, null); | |
81 | + cba(null, passwd, null); | |
64 | 82 | } |
65 | - }); | |
66 | - } | |
67 | - }); | |
68 | -}; | |
69 | - | |
70 | -PolyUserServ.grabInfos = function (login, cb) { | |
71 | - async.waterfall([ | |
72 | - function (cba) { | |
73 | - PolyUserServ.readPasswd(login, cba); | |
74 | - }, | |
75 | - function (passwd, cba) { | |
76 | - if (passwd && passwd.GID) { | |
77 | - PolyUserServ.readGroup(passwd.GID, function (err, group) { | |
78 | - cba(err, passwd, group); | |
79 | - }); | |
83 | + } | |
84 | + ], function (err, passwd, group) { | |
85 | + if (err) { | |
86 | + cb(err); | |
80 | 87 | } else { |
81 | - cba(null, passwd, null); | |
88 | + cb(null, { | |
89 | + nom: (passwd && passwd.GECOS) ? passwd.GECOS : login.toUpperCase(), | |
90 | + section: (group && group.name) ? group.name.toUpperCase() : ((passwd && passwd.GID) ? passwd.GID : 'Inconnu') | |
91 | + }); | |
82 | 92 | } |
83 | - } | |
84 | - ], function (err, passwd, group) { | |
85 | - if (err) { | |
86 | - cb(err); | |
87 | - } else { | |
88 | - cb(null, { | |
89 | - nom: (passwd && passwd.GECOS) ? passwd.GECOS : login.toUpperCase(), | |
90 | - section: (group && group.name) ? group.name.toUpperCase() : ((passwd && passwd.GID) ? passwd.GID : 'Inconnu') | |
91 | - }); | |
92 | - } | |
93 | - }); | |
94 | -}; | |
93 | + }); | |
94 | + }, | |
95 | 95 | |
96 | -PolyUserServ.add = function (login, cb) { | |
97 | - PolyUserServ.grabInfos(login, function (err, data) { | |
98 | - if (err) { | |
99 | - cb(err); | |
100 | - } else { | |
101 | - cb(null, data); | |
102 | - cache.set(login, data); | |
103 | - } | |
104 | - }); | |
105 | -}; | |
106 | - | |
107 | -PolyUserServ.get = function (login, cb) { | |
108 | - cache.get(login, function (err, data) { | |
109 | - if (err) { | |
110 | - cb(err); | |
111 | - } else { | |
112 | - if (data) { | |
96 | + add: function (login, cb) { | |
97 | + PolyUserServ.grabInfos(login, function (err, data) { | |
98 | + if (err) { | |
99 | + cb(err); | |
100 | + } else { | |
113 | 101 | cb(null, data); |
102 | + cache.set(login, data); | |
103 | + } | |
104 | + }); | |
105 | + }, | |
106 | + | |
107 | + get: function (login, cb) { | |
108 | + cache.get(login, function (err, data) { | |
109 | + if (err) { | |
110 | + cb(err); | |
114 | 111 | } else { |
115 | - PolyUserServ.add(login, cb); | |
112 | + if (data) { | |
113 | + cb(null, data); | |
114 | + } else { | |
115 | + PolyUserServ.add(login, cb); | |
116 | + } | |
116 | 117 | } |
117 | - } | |
118 | - }); | |
119 | -}; | |
118 | + }); | |
119 | + }, | |
120 | 120 | |
121 | -PolyUserServ.verify = function (login, pass, cb) { | |
122 | - var conn = new Client(); | |
123 | - conn.on('ready', function () { | |
124 | - cb(null, true); | |
125 | - }).on('error', function (err) { | |
126 | - if (err.level === 'client-authentication') { | |
127 | - cb(null, false); | |
128 | - } else { | |
129 | - cb(err); | |
130 | - } | |
131 | - }).connect({ | |
132 | - host: creds.host, | |
133 | - port: creds.port, | |
134 | - username: login, | |
135 | - password: pass | |
136 | - }); | |
121 | + verify: function (login, pass, cb) { | |
122 | + var conn = new Client(); | |
123 | + conn.on('ready', function () { | |
124 | + cb(null, true); | |
125 | + }).on('error', function (err) { | |
126 | + if (err.level === 'client-authentication') { | |
127 | + cb(null, false); | |
128 | + } else { | |
129 | + cb(err); | |
130 | + } | |
131 | + }).connect({ | |
132 | + host: creds.host, | |
133 | + port: creds.port, | |
134 | + username: login, | |
135 | + password: pass | |
136 | + }); | |
137 | + } | |
137 | 138 | }; |
138 | - | |
139 | -module.exports = PolyUserServ; | ... | ... |