-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject.js
More file actions
251 lines (235 loc) · 7.01 KB
/
project.js
File metadata and controls
251 lines (235 loc) · 7.01 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const { matchedData } = require('express-validator');
const debug = require('debug')('project');
async function addSuffix(name, noSuffix, req) {
let newName;
try {
const results = await req.client.query('SELECT name FROM projects WHERE name=$1', [name]);
if (results.rowCount === 0) {
return name;
}
if (noSuffix) {
newName = `${name}_1`;
} else {
const indexLastUnderScore = name.lastIndexOf('_');
const suffix = name.slice(indexLastUnderScore + 1);
newName = name.substring(0, indexLastUnderScore + 1) + (parseInt(suffix, 10) + 1);
}
} catch (error) {
req.error = {
msg: error.toString(),
code: 500,
function: 'addSuffix',
};
}
return addSuffix(newName, false, req);
}
async function insertProject(name, req) {
debug(`Insertion du projet ${name}`);
let idProject;
try {
const results = await req.client.query('INSERT INTO projects (name) VALUES ($1) RETURNING id', [name]);
idProject = results.rows[0].id;
req.idProjects.push(idProject);
} catch (error) {
req.error = {
msg: error.toString(),
code: 500,
function: 'insertProject',
};
debug('Erreur dans insertProject');
}
debug('Fin insertion projet');
return idProject;
}
async function insertJob(name, command, idProject, tags, geometry, req) {
debug(`Insertion du job ${name}`);
let idJob;
try {
const results = await req.client.query('INSERT INTO jobs (name, command, id_project, tags, geometry) VALUES ($1, $2, $3, $4, ST_Transform($5, 4326)) RETURNING id', [name, command, idProject, tags, geometry]);
idJob = results.rows[0].id;
req.idJobs.push(idJob);
} catch (error) {
req.error = {
msg: error.toString(),
code: 500,
function: 'insertJob',
};
debug('Erreur dans insertJob');
}
debug('Fin insertion job');
return idJob;
}
async function insertJobDependencies(values, downstream, req) {
debug(`Insertion du bloc de dependances pour le job : ${downstream}`);
await req.client.query(
`INSERT INTO jobdependencies (upstream, downstream) VALUES ${values}`,
)
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'insertJobDependencies',
};
});
debug('Fin insertion job dependences');
}
async function insertProjectDependency(upstream, downstream, req) {
debug(`Insertion de la dependance entre le projet ${upstream} et ${downstream}`);
await req.client.query(
'INSERT INTO projectdependencies (upstream, downstream) VALUES ($1, $2)', [upstream, downstream],
)
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'insertProjectDependency',
};
});
debug('Fin insertion project dependence');
}
async function insertProjectFromJson(req, res, next) {
const { projects } = req.body;
req.idProjects = [];
req.idJobs = [];
/* eslint-disable no-restricted-syntax */
for (const project of projects) {
/* eslint-disable no-await-in-loop */
project.name = await addSuffix(project.name, true, req);
const idProject = await insertProject(project.name, req);
debug(`id_project = ${idProject}`);
/* eslint-disable no-restricted-syntax */
for (const job of project.jobs) {
/* eslint-disable no-await-in-loop */
const idJob = await insertJob(job.name,
job.command, idProject, job.tags ? job.tags : [], job.geometry, req);
debug(`id_job = ${idJob}`);
// Si il y a des dépendances entre les jobs
if (job.deps) {
/* eslint no-undef-init: "error" */
let values = '';
for (const dep of job.deps) {
const upstream = req.idJobs[dep.id];
if (dep !== job.deps[0]) values += ',';
values += `(${upstream},${idJob})`;
}
await insertJobDependencies(values, idJob, req);
}
}
if (project.deps) {
/* eslint-disable no-restricted-syntax */
for (const dep of project.deps) {
const upstream = req.idProjects[dep.id];
const downstream = idProject;
/* eslint-disable no-await-in-loop */
await insertProjectDependency(upstream, downstream, req);
}
}
// Il faut vider le tableau des identifiants de jobs
// lorsqu'on insére les jobs du projet suivant
// sinon au moment d'insérer les dépendances entre job du projet suivant
// il va se baser sur les identifiants du projet précédent
req.idJobs = [];
}
req.result = req.idProjects;
next();
}
async function getAllProjects(req, res, next) {
await req.client.query('SELECT * FROM view_project_status_by_jobs ORDER BY project_id DESC')
.then((results) => {
req.result = results.rows;
})
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'getAllProjects',
};
});
next();
}
async function getProjectStatus(req, res, next) {
await req.client.query('SELECT * FROM view_project_status')
.then((results) => {
req.result = results.rows;
})
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'getProjectStatus',
};
});
next();
}
async function getJobsOfProject(req, res, next) {
const params = matchedData(req);
const { id } = params;
await req.client.query('SELECT * FROM view_jobs WHERE job_id_project=$1', [id])
.then((results) => { req.result = results.rows; })
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'getJobsOfProject',
};
});
next();
}
async function getProject(req, res, next) {
const params = matchedData(req);
const { id } = params;
debug('id : ', id);
await req.client.query('SELECT * FROM view_projects WHERE project_id=$1', [id])
.then((results) => { req.result = results.rows; })
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'getProject',
};
});
next();
}
async function setPriority(req, res, next) {
const { ids } = req.body;
const params = matchedData(req);
const { priority } = params;
debug('ids : ', ids);
debug('priority : ', priority);
await req.client.query('UPDATE projects SET priority=$1 WHERE id = ANY($2::int[])',
[priority, ids])
.then((results) => {
req.result = results.rows;
})
.catch((error) => {
req.error = {
msg: error.toString(),
code: 404,
function: 'setPriority',
};
});
next();
}
async function deleteProjects(req, res, next) {
const { ids } = req.body;
debug('ids : ', ids);
await req.client.query('DELETE FROM projects WHERE id = ANY($1::int[])', [ids])
.then((results) => { req.result = results.rows; })
.catch((error) => {
req.error = {
msg: error.toString(),
code: 500,
function: 'deleteProjects',
};
});
next();
}
module.exports = {
insertProjectFromJson,
getAllProjects,
getProject,
getProjectStatus,
getJobsOfProject,
setPriority,
deleteProjects,
};