MessServ.js
2.09 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
var MessModl = require('../models/MessModl');
var MembresServ = require('../services/MembresServ');
var async = require('async');
var MesssServ = module.exports = {
simple: ['_id', 'login', 'content', 'conv', 'date', 'hidden'],
simpleData: function (messD, cb) {
async.parallel([
function (cba) {
var mess = {};
for (var prop of MesssServ.simple) {
mess[prop] = messD[prop];
}
cba(null, mess);
},
function (cba) {
async.waterfall([
function (cbaa) {
MembresServ.getLogin(messD.login, cbaa);
},
function (membre, cbaa) {
MembresServ.simpleData(membre, cbaa);
}
], cba);
}
], function (err, res) {
if (err) {
cb(err);
} else {
mess = res[0];
mess.auteur = res[1];
cb(null, mess);
}
});
},
get: function (id, cb) {
MessModl.findById(id, cb);
},
children: function (conv, cb) {
MessModl.find({
conv: conv,
$or: [{
hidden: false
}, {
hidden: undefined
}]
}, cb);
},
assert: function (mess, cb) {
cb(null, mess.login && mess.content && mess.conv);
},
add: function (data, cb) {
MessModl.create({
content: data.content,
login: data.login,
conv: data.conv
}, cb);
},
edit: function (mess, data, cb) {
mess.content = data.content;
// TODO Edit date
mess.save(cb);
},
remove: function (id, cb) {
async.waterfall([function (cba) {
MesssServ.get(id, cba);
}, function (mess, cba) {
cba(mess ? null : 'notfound', mess);
}, function (mess, cba) {
mess.hidden = true;
mess.save(cba);
}], cb);
}
};