Newer
Older
import express from 'express';
import { v4 as uuid, validate } from 'uuid';
import database from '../database';
import { requireVerification, Token } from './auth';
import { isOfType } from '../util';
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
const task = express();
task.use(requireVerification);
task.get('/:uuid', async (req, res) => {
try {
const id = req.params.uuid;
if (validate(id)) {
const task = await database('users')
.innerJoin('team_members', 'users.id', 'team_members.user_id')
.innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id')
.innerJoin('tasks', 'team_projects.project_id', 'task.team_id')
.select({
id: 'tasks.id',
project: 'tasks.project_id',
name: 'tasks.name',
text: 'tasks.text',
status: 'tasks.status',
priority: 'tasks.priority',
created: 'tasks.created',
edited: 'tasks.edited',
})
.where({
'users.id': req.body.token.id,
'tasks.id': id,
});
if (task.length === 1) {
res.status(200).json({
status: 'success',
task: task[0],
});
} else {
res.status(404).json({
status: 'error',
message: 'task not found',
});
}
} else {
res.status(400).json({
status: 'error',
message: 'malformed uuid',
});
}
} catch (e) {
res.status(400).json({
status: 'error',
message: 'failed get task',
});
}
});
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
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;