Skip to content
Snippets Groups Projects
Commit e1609e53 authored by Bernard Roland (Student Com20)'s avatar Bernard Roland (Student Com20)
Browse files

Added the possibility to add tasks

parent f742a0b3
Branches
Tags
No related merge requests found
......@@ -29,7 +29,6 @@ project.get('/', async (req, res) => {
projects: projects,
});
} catch (e) {
console.log(e);
res.status(400).json({
status: 'error',
message: 'failed to get project',
......@@ -76,7 +75,6 @@ project.get('/:uuid', async (req, res) => {
});
}
} catch (e) {
console.log(e);
res.status(400).json({
status: 'error',
message: 'failed to get project',
......
......@@ -57,6 +57,105 @@ task.get('/:uuid', async (req, res) => {
}
});
interface TaskRequirement {
role: string;
time: number;
}
interface AddTaskBody {
project: string;
name: string;
text: string;
priority: string;
dependentcies: Array<string>;
requirements: Array<TaskRequirement>;
token: Token;
}
task.post('/', async (req, res) => {
if (isOfType<AddTaskBody>(req.body, [
['project', 'string'], ['name', 'string'], ['text', 'string'],
['priority', 'string'], ['dependentcies', 'object'], ['requirements', 'object'],
])) {
try {
const project_id = req.body.project;
const dependentcy_ids = req.body.dependentcies;
const requirements = req.body.requirements;
const requirement_ids = requirements.map(req => req.role);
for (const team_id of dependentcy_ids.concat(requirement_ids).concat([ project_id ])) {
if (!validate(team_id)) {
res.status(400).json({
status: 'error',
message: 'malformed uuid',
});
return;
}
}
const task_id = uuid();
const project = await database('users')
.innerJoin('team_members', 'users.id', 'team_members.user_id')
.innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id')
.innerJoin('projects', 'team_projects.project_id', 'projects.id')
.select({ id: 'projects.id' })
.where({
'users.id': req.body.token.id,
'projects.id': project_id,
})
.groupBy('projects.id');
if (project.length >= 1) {
await database.transaction(async transaction => {
await transaction('tasks').insert({
id: task_id,
project_id: project_id,
name: req.body.name,
text: req.body.text,
priority: req.body.priority,
status: 'open',
created: new Date(),
edited: new Date(),
});
if (requirements.length !== 0) {
await transaction('task_requirements').insert(
requirements.map(requirement => ({
task_id: task_id,
role_id: requirement.role,
time: requirement.time,
}))
);
}
if (dependentcy_ids.length !== 0) {
await transaction('task_dependencies').insert(
dependentcy_ids.map(dependentcy_id => ({
task_id: task_id,
requires_id: dependentcy_id,
}))
);
}
});
res.status(200).json({
status: 'success',
id: task_id,
});
} else {
res.status(404).json({
status: 'error',
message: 'project not found',
});
}
} catch (e) {
console.error(e);
res.status(400).json({
status: 'error',
message: 'failed to create task',
});
}
} else {
res.status(400).json({
status: 'error',
message: 'missing request fields',
});
}
});
export default task;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment