forked from amand33p/bug-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.ts
More file actions
114 lines (90 loc) · 2.98 KB
/
Copy pathnote.ts
File metadata and controls
114 lines (90 loc) · 2.98 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
import { Request, Response } from 'express';
import { Member } from '../entity/Member';
import { Note } from '../entity/Note';
import { Project } from '../entity/Project';
export const postNote = async (req: Request, res: Response) => {
const { body } = req.body;
const { projectId, bugId } = req.params;
if (!body || body.trim() === '') {
return res
.status(400)
.send({ message: 'Note body field must not be empty.' });
}
const projectMembers = await Member.find({ projectId });
const memberIds = projectMembers.map((m) => m.memberId);
if (!memberIds.includes(req.user)) {
return res
.status(401)
.send({ message: 'Access is denied. Not a member of the project.' });
}
const newNote = Note.create({ body, authorId: req.user, bugId });
await newNote.save();
const relationedNote = await Note.createQueryBuilder('note')
.where('note.id = :noteId', { noteId: newNote.id })
.leftJoinAndSelect('note.author', 'author')
.select([
'note.id',
'note.bugId',
'note.body',
'note.createdAt',
'note.updatedAt',
'author.id',
'author.username',
])
.getOne();
res.status(201).json(relationedNote);
};
export const deleteNote = async (req: Request, res: Response) => {
const { projectId, noteId } = req.params;
const targetProject = await Project.findOne({
where: { id: projectId },
relations: ['members'],
});
if (!targetProject) {
return res.status(404).send({ message: 'Invalid project ID.' });
}
const memberIds = targetProject.members.map((m) => m.memberId);
if (!memberIds.includes(req.user)) {
return res
.status(401)
.send({ message: 'Access is denied. Not a member of the project.' });
}
const targetNote = await Note.findOne({ id: Number(noteId) });
if (!targetNote) {
return res.status(404).send({ message: 'Invalid note ID.' });
}
if (
targetNote.authorId !== req.user &&
targetProject.createdById !== req.user
) {
return res.status(401).send({ message: 'Access is denied.' });
}
await targetNote.remove();
res.status(204).end();
};
export const updateNote = async (req: Request, res: Response) => {
const { body } = req.body;
const { projectId, noteId } = req.params;
if (!body || body.trim() === '') {
return res
.status(400)
.send({ message: 'Note body field must not be empty.' });
}
const projectMembers = await Member.find({ projectId });
const memberIds = projectMembers.map((m) => m.memberId);
if (!memberIds.includes(req.user)) {
return res
.status(401)
.send({ message: 'Access is denied. Not a member of the project.' });
}
const targetNote = await Note.findOne({ id: Number(noteId) });
if (!targetNote) {
return res.status(404).send({ message: 'Invalid note ID.' });
}
if (targetNote.authorId !== req.user) {
return res.status(401).send({ message: 'Access is denied.' });
}
targetNote.body = body;
await targetNote.save();
res.json(targetNote);
};