-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.js
271 lines (234 loc) · 8 KB
/
book.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const Book = require("../models/Boook");
const { addDays, isToday, isAfter } = require("date-fns");
const createChapters = require("../utils/createChapters");
const error = require("../utils/error");
const {
setCompletedBook,
setCompletedChapter,
} = require("../utils/setCompleted");
const userServices = require("./user");
const { ObjectId } = require("mongodb");
const compare = require("../utils/compareId");
const findAll = (user, all = false) => {
if (Boolean(all)) {
if (user.roles === "SUPER_ADMIN" || user.roles === "ADMIN") {
return Book.find();
} else if (user.roles === "USER") {
return Book.find({ user });
}
} else {
return Book.find({ user });
}
};
const findByProperty = (key, value) => {
if (key === "id") {
return Book.findById(value);
}
return Book.find({ [key]: value });
};
const createBook = async (data, user) => {
// name, time, description, chapters -> date object
const time = Math.floor(data.time / data.chapters.length);
const trmTime = time * data.chapters.length;
const unbindTime = data.time - trmTime;
const book = new Book({
name: data.name,
time: data.time - unbindTime,
// left: data.time - unbindTime,
extra: 0,
unbindTime,
start: new Date(),
end: addDays(new Date(), data.time - unbindTime - 1),
note: "",
completed: 0,
description: data.description,
user,
chapters: createChapters(data.chapters, time),
});
return book.save();
};
const updateBook = async (id, data, user) => {
try {
const book = await findByProperty("id", id);
if (!book) throw error("Book not found for update", 404);
if (!compare(user._id, book.user._id)) {
throw error("This is not your book", 400);
}
const obj = {
name: data.name || book.name,
description: data.description || book.description,
note: typeof data.note !== "undefined" ? data.note : book.note,
};
return Book.findByIdAndUpdate(id, { ...obj }, { new: true });
} catch (err) {
throw error(err.message);
}
};
const updateChapter = async (bookId, chapterId, data, user) => {
try {
const book = await findByProperty("id", bookId);
if (!book) throw error("Book not found for update chapter");
if (!compare(user._id, book.user._id))
throw error("This is not your book", 400);
let chapters = JSON.stringify(book.chapters);
chapters = JSON.parse(chapters);
const cpter = chapters.filter((chapter) => chapter._id == chapterId)[0];
const obj = {
name: data.name || cpter.name,
note: typeof data.note !== "undefined" ? data.note : cpter.note,
};
chapters = chapters.map((chapter) => {
if (chapter._id == chapterId) {
let newObj = { ...chapter, ...obj };
return newObj;
} else {
return chapter;
}
});
return Book.findByIdAndUpdate(bookId, { chapters }, { new: true });
} catch (err) {
throw error(err.message, 400);
}
};
const updateMilestone = async (bookId, chapterId, milestoneId, data, user) => {
try {
const book = await findByProperty("id", bookId);
if (!book) throw error("Book not found for update milestone", 404);
if (!compare(user._id, book.user._id)) {
throw error("This is not your book", 400);
}
let chapters = JSON.stringify(book.chapters);
chapters = JSON.parse(chapters);
const { milestones } = chapters.filter(
(chapter) => chapter._id === chapterId
)[0];
const mile = milestones.filter((ml) => ml._id == milestoneId)[0];
const more =
typeof data.add !== "undefined"
? data.add
? mile.extra + 1
: mile.extra !== 0
? mile.extra - 1
: mile.extra
: mile.extra;
const terr = () => {
throw error("The milestone end date has not come yet.");
return mile.date;
};
const obj = {
isDone:
typeof data.isDone !== "undefined"
? data.isDone
? isToday(new Date(mile.data)) || isAfter(new Date(), new Date(mile.date))
? true
: terr()
: mile.isDone
: mile.isDone,
note: typeof data.note !== "undefined" ? data.note : mile.note,
extra: more,
end: addDays(new Date(mile.end), more),
};
chapters = chapters.map((chapter) => {
const chapterMore =
typeof data.add !== "undefined"
? data.add
? chapter.extra + 1
: mile.extra > 0
? chapter.extra - 1
: chapter.extra
: chapter.extra;
if (chapter._id == chapterId) {
return {
...chapter,
extra: chapterMore,
milestones: chapter.milestones.map((mile) => {
if (mile._id == milestoneId) {
return {
...mile,
...obj,
};
} else {
return mile;
}
}),
completed: setCompletedChapter(
chapter,
data.isDone !== "undefined" ? (data.isDone ? 1 : 0) : 0
),
end: addDays(new Date(chapter.end), chapterMore),
};
} else {
return chapter;
}
});
const bookMore =
typeof data.add !== "undefined"
? data.add
? book.extra + 1
: mile.extra > 0
? book.extra - 1
: book.extra
: book.extra;
return Book.findByIdAndUpdate(
bookId,
{
extra: bookMore,
chapters,
completed: setCompletedBook({ ...book, chapters }),
end: addDays(new Date(book.end), bookMore),
},
{ new: true }
);
} catch (err) {
throw error(err, 400);
}
};
const deleteBook = async (id, user) => {
try {
const book = await findByProperty("id", id);
if (!book) throw error("Book not found for delete", 400);
if (!compare(user._id, book.user._id))
throw error("This is not your book", 400);
const bookId = new ObjectId(id);
let isFavourite = false;
let isBookmark = false;
// Is favourite
user.favourites.filter((fav) => {
const favId = new ObjectId(fav._id);
if (bookId.equals(favId)) {
isFavourite = true;
} else {
isFavourite = false;
}
});
// Remove from favourites
if (isFavourite) {
await userServices.removeFavourite(user, id);
}
// Is bookmark
user.bookmarks.filter((bmrk) => {
const bmrkId = new ObjectId(bmrk._id);
if (bookId.equals(bmrkId)) {
isBookmark = true;
} else {
isBookmark = false;
}
});
// Remove from bookmarks
if (isBookmark) {
await userServices.removeBookmark(user, id);
}
return Book.findByIdAndDelete(id);
} catch (err) {
throw error(err.message, 400);
}
};
module.exports = {
findAll,
findByProperty,
createBook,
updateBook,
updateChapter,
updateMilestone,
deleteBook,
};