ConvsServ.js
2.4 KB
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
var ConvModl = require('../models/ConvModl');
var DosssServ = require('../services/DosssServ');
var async = require('async');
var ConvsServ = module.exports = {
simple: ['_id', 'titre', 'parent', 'hidden'],
simpleData: function (convD, cb) {
// TODO Démarré par
// TODO Dernier message
var conv = {};
for (var prop of ConvsServ.simple) {
conv[prop] = convD[prop];
}
cb(null, conv);
},
detailedData: function (convD, cb) {
async.parallel([
function (cba) {
var conv = {};
for (var prop of ConvsServ.simple) {
conv[prop] = convD[prop];
}
cba(null, conv);
},
function (cba) {
MessServ = require('../services/MessServ')
MessServ.children(convD._id, function (err, children) {
if (err) {
cba(err);
} else {
async.map(children, MessServ.simpleData, cba);
}
});
}
], function (err, res) {
if (err) {
cb(err);
} else {
conv = res[0];
conv.messs = res[1];
cb(null, conv);
}
});
},
get: function (id, cb) {
ConvModl.findById(id, cb);
},
children: function (id, cb) { // Conversations filles du dossier en paramètre
ConvModl.find({
parent: id,
$or: [{
hidden: false
}, {
hidden: undefined
}]
}, cb);
},
assert: function (conv, cb) {
cb(null, conv.titre, conv.parent);
},
add: function (data, cb) {
ConvModl.create({
titre: data.titre,
parent: data.parent
}, cb);
},
remove: function (id, cb) {
async.waterfall([function (cba) {
ConvsServ.get(id, cba);
}, function (conv, cba) {
cba(conv ? null : 'notfound', conv);
}, function (conv, cba) {
DosssServ.get('trash', function (err, trash) {
console.log(21, trash);
cba(err, conv, trash);
});
}, function (conv, trash, cba) {
conv.parent = trash._id;
conv.save(cba);
}], cb);
}
};