-
Notifications
You must be signed in to change notification settings - Fork 0
/
dt.js
164 lines (136 loc) · 6.23 KB
/
dt.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module.exports = function dt(options){
/** Affichage une DT si un ID est specifie
* Sinon affiche toutes les DT
*/
this.add('role:dt, cmd:GET', function(msg, respond) {
if (msg.data.id != undefined){
// Recupere la DT dont l'ID correspond
this.make('dt').load$(msg.data.id, function (err, dt) {
if(dt != null)
respond(null, {success: true, msg: "", data:{applicant : dt.applicant, work : dt.work, state: dt.state, id: dt.id}})
else
respond(null, {success: false, msg: "wr not found", data:{}})
})
}else{
// Recupere toutes les DT
this.make('dt').list$({},function (err, list) {
var data = [];
list.forEach(function (dt) {
data.push({applicant : dt.applicant, work : dt.work, state: dt.state, id: dt.id})
});
respond(null, {success: true, msg: "", data:data})
})
}
});
// Creer une nouvelle DT en initialisant sont statut a created
this.add('role:dt, cmd:POST', function(msg, respond) {
msg.data.state = "created";
// Appelle le module de stats pour les mettre a jour
this.act('role:stats', {
cmd: "add",
applicant: msg.data.applicant
})
this.make('dt').data$(msg.data).save$(function (err, dt) {
// Appelle le module d'indexation
this.act('role:indexation', {
cmd: "index",
work: dt.work,
id: dt.id
});
respond(null, {success: true, msg: "", data:{id: dt.id, applicant: dt.applicant, work: dt.work, state: dt.state, date: dt.date}});
})
});
// Traite la suppression d'une DT
this.add('role:dt, cmd:DELETE', function(msg, respond) {
// Suppression unique d'une DT grace a l'ID
if (msg.data.id != undefined){
this.make('dt').load$(msg.data.id, function (err, dt) {
if (dt == null) {
respond(null, {success: false, msg: "wr not found", data: {}})
}else{
// Verification de la possibilite de supprimer la DT en fonction de son etat (created | closed)
if(dt.state == "created"){
//temp permet de stocker la DT a supprimer afin de la renvoyer
var temp = dt;
delete temp.entity$;
// Mise a jour des stats
this.act('role:stats', {
cmd: "delete",
applicant: dt.applicant
})
// Suppression
this.make('dt').remove$(dt.id, function (err, dt) {
respond(null, {success: true, msg: "", data: temp})
})
} else {
// DT cloturee donc suppression impossible
respond(null, {success: false, msg: "wr is already closed", data: dt})
}
}
})
} else {
// Suppression de toutes les DT dont le statut est created
this.make('dt').list$({state:'created'}, function (err, res) {
// that = this car dans le foreach le this fait desormais reference a res
var that = this;
res.forEach(function (dt) {
// Mise a jour des stats
that.act('role:stats', {
cmd: "delete",
applicant: dt.applicant
})
// Suppression
dt.remove$(function (err, dt) {
})
})
respond(null, {success: true, msg: "", data: {}})
})
}
});
// Modification d'une DT
this.add('role:dt, cmd:PUT', function(msg, respond) {
if(msg.data.id == undefined){
respond(null, {success: false, msg: "wr id not provided", data:{}})
}
else{
this.make('dt').load$(msg.data.id, function (err, dt){
if (dt == null) {
respond(null, {success: false, msg: "wr not found", data: {}})
} else {
// Verification de la possibilite de modifier une DT par rapport au statut
if (dt.state == "created") {
/** Update de l'indexation si champ work fourni
* Mise en commentaire car le remove necessaire a l'update
* ne fonctionne pas
*
if (msg.data.work != null) {
this.act('role:indexation', {
cmd: "update",
work: msg.data.work,
id: dt.id
});
}*/
// Recuperation des donnees de la DT enregistree si elles ne sont pas fournies dans la requete
if (msg.data.applicant == null) msg.data.applicant = dt.applicant;
if (msg.data.work == null) msg.data.work = dt.work;
if (msg.data.state == null) msg.data.state = dt.state;
if (msg.data.date == null) msg.data.date = dt.date;
// Update des stats si le statut change
if (msg.data.state === "closed") {
this.act('role:stats', {
cmd: "update",
applicant: msg.data.applicant
})
}
this.make('dt').data$(msg.data).save$(function (err, dt) {
respond(null, {success: true, msg: "", data: {applicant : dt.applicant, work : dt.work, state: dt.state, id: dt.id}})
})
}
else {
respond(null, {success: false, msg: "wr is already closed", data: dt})
}
}
})
}
})
};