@eeplatform/basic-edu 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.github/workflows/main.yml +17 -0
- package/.github/workflows/publish.yml +39 -0
- package/CHANGELOG.md +7 -0
- package/dist/index.d.ts +134 -0
- package/dist/index.js +1405 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1384 -0
- package/dist/index.mjs.map +1 -0
- package/dist/public/handlebars/forget-password.hbs +14 -0
- package/dist/public/handlebars/member-invite.hbs +17 -0
- package/dist/public/handlebars/sign-up.hbs +14 -0
- package/dist/public/handlebars/user-invite.hbs +13 -0
- package/package.json +31 -0
- package/tsconfig.json +108 -0
- package/tsup.config.ts +10 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1384 @@
|
|
|
1
|
+
// src/models/region.model.ts
|
|
2
|
+
import { BadRequestError } from "@eeplatform/nodejs-utils";
|
|
3
|
+
import Joi from "joi";
|
|
4
|
+
import { ObjectId } from "mongodb";
|
|
5
|
+
var schemaRegion = Joi.object({
|
|
6
|
+
_id: Joi.string().hex().optional().allow(null, ""),
|
|
7
|
+
name: Joi.string().min(1).max(100).required(),
|
|
8
|
+
createdAt: Joi.string().isoDate().optional(),
|
|
9
|
+
updatedAt: Joi.string().isoDate().optional(),
|
|
10
|
+
deletedAt: Joi.string().isoDate().optional().allow(null, "")
|
|
11
|
+
});
|
|
12
|
+
function modelRegion(value) {
|
|
13
|
+
const { error } = schemaRegion.validate(value);
|
|
14
|
+
if (error) {
|
|
15
|
+
throw new BadRequestError(`Invalid region data: ${error.message}`);
|
|
16
|
+
}
|
|
17
|
+
if (value._id && typeof value._id === "string") {
|
|
18
|
+
try {
|
|
19
|
+
value._id = ObjectId.createFromTime(value._id);
|
|
20
|
+
} catch (error2) {
|
|
21
|
+
throw new Error("Invalid _id.");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
_id: value._id,
|
|
26
|
+
name: value.name,
|
|
27
|
+
createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
28
|
+
updatedAt: value.updatedAt ?? "",
|
|
29
|
+
deletedAt: value.deletedAt ?? ""
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/repositories/region.repository.ts
|
|
34
|
+
import {
|
|
35
|
+
AppError,
|
|
36
|
+
BadRequestError as BadRequestError2,
|
|
37
|
+
InternalServerError,
|
|
38
|
+
logger,
|
|
39
|
+
makeCacheKey,
|
|
40
|
+
paginate,
|
|
41
|
+
useAtlas,
|
|
42
|
+
useCache
|
|
43
|
+
} from "@eeplatform/nodejs-utils";
|
|
44
|
+
import { ObjectId as ObjectId2 } from "mongodb";
|
|
45
|
+
function useRegionRepo() {
|
|
46
|
+
const db = useAtlas.getDb();
|
|
47
|
+
if (!db) {
|
|
48
|
+
throw new Error("Unable to connect to server.");
|
|
49
|
+
}
|
|
50
|
+
const namespace_collection = "deped.regions";
|
|
51
|
+
const collection = db.collection(namespace_collection);
|
|
52
|
+
const { getCache, setCache, delNamespace } = useCache(namespace_collection);
|
|
53
|
+
async function createIndexes() {
|
|
54
|
+
try {
|
|
55
|
+
await collection.createIndexes([
|
|
56
|
+
{ key: { name: 1 } },
|
|
57
|
+
{ key: { createdAt: 1 } },
|
|
58
|
+
{ key: { name: "text" } },
|
|
59
|
+
{ key: { name: 1 }, unique: true, name: "unique_name" }
|
|
60
|
+
]);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw new Error("Failed to create index on regions.");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function delCachedData() {
|
|
66
|
+
delNamespace().then(() => {
|
|
67
|
+
logger.log({
|
|
68
|
+
level: "info",
|
|
69
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
70
|
+
});
|
|
71
|
+
}).catch((err) => {
|
|
72
|
+
logger.log({
|
|
73
|
+
level: "error",
|
|
74
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async function add(value, session) {
|
|
79
|
+
try {
|
|
80
|
+
value = modelRegion(value);
|
|
81
|
+
const res = await collection.insertOne(value, { session });
|
|
82
|
+
delCachedData();
|
|
83
|
+
return res.insertedId;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
logger.log({
|
|
86
|
+
level: "error",
|
|
87
|
+
message: error.message
|
|
88
|
+
});
|
|
89
|
+
if (error instanceof AppError) {
|
|
90
|
+
throw error;
|
|
91
|
+
} else {
|
|
92
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
93
|
+
if (isDuplicated) {
|
|
94
|
+
throw new BadRequestError2("Region already exists.");
|
|
95
|
+
}
|
|
96
|
+
throw new Error("Failed to create region.");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function getAll({
|
|
101
|
+
search = "",
|
|
102
|
+
page = 1,
|
|
103
|
+
limit = 10,
|
|
104
|
+
sort = {},
|
|
105
|
+
status = "active"
|
|
106
|
+
} = {}) {
|
|
107
|
+
page = page > 0 ? page - 1 : 0;
|
|
108
|
+
const query = {
|
|
109
|
+
deletedAt: { $in: ["", null] },
|
|
110
|
+
status
|
|
111
|
+
};
|
|
112
|
+
sort = Object.keys(sort).length > 0 ? sort : { _id: 1 };
|
|
113
|
+
const cacheKeyOptions = {
|
|
114
|
+
status,
|
|
115
|
+
page,
|
|
116
|
+
limit,
|
|
117
|
+
sort: JSON.stringify(sort)
|
|
118
|
+
};
|
|
119
|
+
if (search) {
|
|
120
|
+
query.$text = { $search: search };
|
|
121
|
+
cacheKeyOptions.search = search;
|
|
122
|
+
}
|
|
123
|
+
const cacheKey = makeCacheKey(namespace_collection, cacheKeyOptions);
|
|
124
|
+
logger.log({
|
|
125
|
+
level: "info",
|
|
126
|
+
message: `Cache key for getAll regions: ${cacheKey}`
|
|
127
|
+
});
|
|
128
|
+
try {
|
|
129
|
+
const cached = await getCache(cacheKey);
|
|
130
|
+
if (cached) {
|
|
131
|
+
logger.log({
|
|
132
|
+
level: "info",
|
|
133
|
+
message: `Cache hit for getAll regions: ${cacheKey}`
|
|
134
|
+
});
|
|
135
|
+
return cached;
|
|
136
|
+
}
|
|
137
|
+
const items = await collection.aggregate([
|
|
138
|
+
{ $match: query },
|
|
139
|
+
{ $sort: sort },
|
|
140
|
+
{ $skip: page * limit },
|
|
141
|
+
{ $limit: limit }
|
|
142
|
+
]).toArray();
|
|
143
|
+
const length = await collection.countDocuments(query);
|
|
144
|
+
const data = paginate(items, page, limit, length);
|
|
145
|
+
setCache(cacheKey, data, 600).then(() => {
|
|
146
|
+
logger.log({
|
|
147
|
+
level: "info",
|
|
148
|
+
message: `Cache set for getAll regions: ${cacheKey}`
|
|
149
|
+
});
|
|
150
|
+
}).catch((err) => {
|
|
151
|
+
logger.log({
|
|
152
|
+
level: "error",
|
|
153
|
+
message: `Failed to set cache for getAll regions: ${err.message}`
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
return data;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
logger.log({ level: "error", message: `${error}` });
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async function getById(_id) {
|
|
163
|
+
try {
|
|
164
|
+
_id = new ObjectId2(_id);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
throw new BadRequestError2("Invalid ID.");
|
|
167
|
+
}
|
|
168
|
+
const cacheKey = makeCacheKey(namespace_collection, { _id: String(_id) });
|
|
169
|
+
try {
|
|
170
|
+
const cached = await getCache(cacheKey);
|
|
171
|
+
if (cached) {
|
|
172
|
+
logger.log({
|
|
173
|
+
level: "info",
|
|
174
|
+
message: `Cache hit for getById region: ${cacheKey}`
|
|
175
|
+
});
|
|
176
|
+
return cached;
|
|
177
|
+
}
|
|
178
|
+
const result = await collection.findOne({
|
|
179
|
+
_id,
|
|
180
|
+
deletedAt: { $in: ["", null] }
|
|
181
|
+
});
|
|
182
|
+
if (!result) {
|
|
183
|
+
throw new BadRequestError2("Region not found.");
|
|
184
|
+
}
|
|
185
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
186
|
+
logger.log({
|
|
187
|
+
level: "info",
|
|
188
|
+
message: `Cache set for region by id: ${cacheKey}`
|
|
189
|
+
});
|
|
190
|
+
}).catch((err) => {
|
|
191
|
+
logger.log({
|
|
192
|
+
level: "error",
|
|
193
|
+
message: `Failed to set cache for region by id: ${err.message}`
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
return result;
|
|
197
|
+
} catch (error) {
|
|
198
|
+
if (error instanceof AppError) {
|
|
199
|
+
throw error;
|
|
200
|
+
} else {
|
|
201
|
+
throw new InternalServerError("Failed to get region.");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async function getByName(name) {
|
|
206
|
+
const cacheKey = makeCacheKey(namespace_collection, { name });
|
|
207
|
+
try {
|
|
208
|
+
const cached = await getCache(cacheKey);
|
|
209
|
+
if (cached) {
|
|
210
|
+
logger.log({
|
|
211
|
+
level: "info",
|
|
212
|
+
message: `Cache hit for getByName region: ${cacheKey}`
|
|
213
|
+
});
|
|
214
|
+
return cached;
|
|
215
|
+
}
|
|
216
|
+
const result = await collection.findOne({
|
|
217
|
+
name,
|
|
218
|
+
deletedAt: { $in: ["", null] }
|
|
219
|
+
});
|
|
220
|
+
if (!result) {
|
|
221
|
+
throw new BadRequestError2("Region not found.");
|
|
222
|
+
}
|
|
223
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
224
|
+
logger.log({
|
|
225
|
+
level: "info",
|
|
226
|
+
message: `Cache set for region by name: ${cacheKey}`
|
|
227
|
+
});
|
|
228
|
+
}).catch((err) => {
|
|
229
|
+
logger.log({
|
|
230
|
+
level: "error",
|
|
231
|
+
message: `Failed to set cache for region by name: ${err.message}`
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
return result;
|
|
235
|
+
} catch (error) {
|
|
236
|
+
if (error instanceof AppError) {
|
|
237
|
+
throw error;
|
|
238
|
+
} else {
|
|
239
|
+
throw new InternalServerError("Failed to get region.");
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
244
|
+
const allowedFields = ["name"];
|
|
245
|
+
if (!allowedFields.includes(field)) {
|
|
246
|
+
throw new BadRequestError2(
|
|
247
|
+
`Field "${field}" is not allowed to be updated.`
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
_id = new ObjectId2(_id);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
throw new BadRequestError2("Invalid ID.");
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
await collection.updateOne(
|
|
257
|
+
{ _id, deletedAt: { $in: ["", null] } },
|
|
258
|
+
{ $set: { [field]: value, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
|
|
259
|
+
{ session }
|
|
260
|
+
);
|
|
261
|
+
delCachedData();
|
|
262
|
+
return `Successfully updated region ${field}.`;
|
|
263
|
+
} catch (error) {
|
|
264
|
+
throw new InternalServerError(`Failed to update region ${field}.`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
async function deleteById(_id) {
|
|
268
|
+
try {
|
|
269
|
+
_id = new ObjectId2(_id);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
throw new BadRequestError2("Invalid ID.");
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
await collection.updateOne(
|
|
275
|
+
{ _id },
|
|
276
|
+
{ $set: { deletedAt: (/* @__PURE__ */ new Date()).toISOString() } }
|
|
277
|
+
);
|
|
278
|
+
delCachedData();
|
|
279
|
+
return "Successfully deleted region.";
|
|
280
|
+
} catch (error) {
|
|
281
|
+
throw new InternalServerError("Failed to delete region.");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
createIndexes,
|
|
286
|
+
add,
|
|
287
|
+
getAll,
|
|
288
|
+
getById,
|
|
289
|
+
updateFieldById,
|
|
290
|
+
deleteById,
|
|
291
|
+
getByName
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/controllers/region.controller.ts
|
|
296
|
+
import { BadRequestError as BadRequestError3 } from "@eeplatform/nodejs-utils";
|
|
297
|
+
import Joi2 from "joi";
|
|
298
|
+
function useRegionController() {
|
|
299
|
+
const {
|
|
300
|
+
add: _add,
|
|
301
|
+
getAll: _getAll,
|
|
302
|
+
getById: _getById,
|
|
303
|
+
getByName: _getByName,
|
|
304
|
+
updateFieldById: _updateFieldById,
|
|
305
|
+
deleteById: _deleteById
|
|
306
|
+
} = useRegionRepo();
|
|
307
|
+
async function add(req, res, next) {
|
|
308
|
+
const value = req.body;
|
|
309
|
+
const { error } = schemaRegion.validate(value);
|
|
310
|
+
if (error) {
|
|
311
|
+
next(new BadRequestError3(error.message));
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
const data = await _add(value);
|
|
316
|
+
res.json({
|
|
317
|
+
message: "Successfully created region.",
|
|
318
|
+
data
|
|
319
|
+
});
|
|
320
|
+
return;
|
|
321
|
+
} catch (error2) {
|
|
322
|
+
next(error2);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
async function getAll(req, res, next) {
|
|
326
|
+
const query = req.query;
|
|
327
|
+
const validation = Joi2.object({
|
|
328
|
+
page: Joi2.number().min(1).optional().allow("", null),
|
|
329
|
+
limit: Joi2.number().min(1).optional().allow("", null),
|
|
330
|
+
search: Joi2.string().optional().allow("", null),
|
|
331
|
+
status: Joi2.string().optional().allow("", null)
|
|
332
|
+
});
|
|
333
|
+
const { error } = validation.validate(query);
|
|
334
|
+
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
335
|
+
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
336
|
+
const search = req.query.search ?? "";
|
|
337
|
+
const status = req.query.status ?? "active";
|
|
338
|
+
const isPageNumber = isFinite(page);
|
|
339
|
+
if (!isPageNumber) {
|
|
340
|
+
next(new BadRequestError3("Invalid page number."));
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const isLimitNumber = isFinite(limit);
|
|
344
|
+
if (!isLimitNumber) {
|
|
345
|
+
next(new BadRequestError3("Invalid limit number."));
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (error) {
|
|
349
|
+
next(new BadRequestError3(error.message));
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
try {
|
|
353
|
+
const data = await _getAll({ page, limit, search, status });
|
|
354
|
+
res.json(data);
|
|
355
|
+
return;
|
|
356
|
+
} catch (error2) {
|
|
357
|
+
next(error2);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async function getById(req, res, next) {
|
|
361
|
+
const id = req.params.id;
|
|
362
|
+
const validation = Joi2.object({
|
|
363
|
+
id: Joi2.string().hex().required()
|
|
364
|
+
});
|
|
365
|
+
const { error } = validation.validate({ id });
|
|
366
|
+
if (error) {
|
|
367
|
+
next(new BadRequestError3(error.message));
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
const data = await _getById(id);
|
|
372
|
+
res.json({
|
|
373
|
+
message: "Successfully retrieved region.",
|
|
374
|
+
data
|
|
375
|
+
});
|
|
376
|
+
return;
|
|
377
|
+
} catch (error2) {
|
|
378
|
+
next(error2);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
async function getByName(req, res, next) {
|
|
382
|
+
const name = req.params.name;
|
|
383
|
+
const validation = Joi2.object({
|
|
384
|
+
name: Joi2.string().required()
|
|
385
|
+
});
|
|
386
|
+
const { error } = validation.validate({ name });
|
|
387
|
+
if (error) {
|
|
388
|
+
next(new BadRequestError3(error.message));
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
const data = await _getByName(name);
|
|
393
|
+
res.json({
|
|
394
|
+
message: "Successfully retrieved region.",
|
|
395
|
+
data
|
|
396
|
+
});
|
|
397
|
+
return;
|
|
398
|
+
} catch (error2) {
|
|
399
|
+
next(error2);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async function updateField(req, res, next) {
|
|
403
|
+
const _id = req.params.id;
|
|
404
|
+
const { field, value } = req.body;
|
|
405
|
+
const validation = Joi2.object({
|
|
406
|
+
_id: Joi2.string().hex().required(),
|
|
407
|
+
field: Joi2.string().valid("name", "director", "directorName").required(),
|
|
408
|
+
value: Joi2.string().required()
|
|
409
|
+
});
|
|
410
|
+
const { error } = validation.validate({ _id, field, value });
|
|
411
|
+
if (error) {
|
|
412
|
+
next(new BadRequestError3(error.message));
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
const message = await _updateFieldById({ _id, field, value });
|
|
417
|
+
res.json({ message });
|
|
418
|
+
return;
|
|
419
|
+
} catch (error2) {
|
|
420
|
+
next(error2);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
async function deleteById(req, res, next) {
|
|
424
|
+
const _id = req.params.id;
|
|
425
|
+
const validation = Joi2.object({
|
|
426
|
+
_id: Joi2.string().hex().required()
|
|
427
|
+
});
|
|
428
|
+
const { error } = validation.validate({ _id });
|
|
429
|
+
if (error) {
|
|
430
|
+
next(new BadRequestError3(error.message));
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
const message = await _deleteById(_id);
|
|
435
|
+
res.json({ message });
|
|
436
|
+
return;
|
|
437
|
+
} catch (error2) {
|
|
438
|
+
next(error2);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
add,
|
|
443
|
+
getAll,
|
|
444
|
+
getById,
|
|
445
|
+
getByName,
|
|
446
|
+
updateField,
|
|
447
|
+
deleteById
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// src/models/division.model.ts
|
|
452
|
+
import { BadRequestError as BadRequestError4 } from "@eeplatform/nodejs-utils";
|
|
453
|
+
import Joi3 from "joi";
|
|
454
|
+
import { ObjectId as ObjectId3 } from "mongodb";
|
|
455
|
+
var schemaDivision = Joi3.object({
|
|
456
|
+
_id: Joi3.string().hex().optional().allow(null, ""),
|
|
457
|
+
name: Joi3.string().min(1).max(100).required(),
|
|
458
|
+
region: Joi3.string().hex().optional().allow(null, ""),
|
|
459
|
+
regionName: Joi3.string().min(1).max(100).optional().allow(null, ""),
|
|
460
|
+
superintendent: Joi3.string().hex().optional().allow(null, ""),
|
|
461
|
+
superintendentName: Joi3.string().min(1).max(100).optional().allow(null, ""),
|
|
462
|
+
createdAt: Joi3.string().isoDate().optional(),
|
|
463
|
+
updatedAt: Joi3.string().isoDate().optional(),
|
|
464
|
+
deletedAt: Joi3.string().isoDate().optional().allow(null, "")
|
|
465
|
+
});
|
|
466
|
+
function modelDivision(value) {
|
|
467
|
+
const { error } = schemaDivision.validate(value);
|
|
468
|
+
if (error) {
|
|
469
|
+
throw new BadRequestError4(`Invalid sdo data: ${error.message}`);
|
|
470
|
+
}
|
|
471
|
+
if (value._id && typeof value._id === "string") {
|
|
472
|
+
try {
|
|
473
|
+
value._id = ObjectId3.createFromTime(value._id);
|
|
474
|
+
} catch (error2) {
|
|
475
|
+
throw new Error("Invalid _id.");
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
_id: value._id,
|
|
480
|
+
name: value.name,
|
|
481
|
+
region: value.region,
|
|
482
|
+
regionName: value.regionName ?? "",
|
|
483
|
+
superintendent: value.superintendent,
|
|
484
|
+
superintendentName: value.superintendentName ?? "",
|
|
485
|
+
createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
486
|
+
updatedAt: value.updatedAt ?? "",
|
|
487
|
+
deletedAt: value.deletedAt ?? ""
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// src/repositories/division.repository.ts
|
|
492
|
+
import {
|
|
493
|
+
AppError as AppError2,
|
|
494
|
+
BadRequestError as BadRequestError5,
|
|
495
|
+
InternalServerError as InternalServerError2,
|
|
496
|
+
logger as logger2,
|
|
497
|
+
makeCacheKey as makeCacheKey2,
|
|
498
|
+
paginate as paginate2,
|
|
499
|
+
useAtlas as useAtlas2,
|
|
500
|
+
useCache as useCache2
|
|
501
|
+
} from "@eeplatform/nodejs-utils";
|
|
502
|
+
import { ObjectId as ObjectId4 } from "mongodb";
|
|
503
|
+
function useDivisionRepo() {
|
|
504
|
+
const db = useAtlas2.getDb();
|
|
505
|
+
if (!db) {
|
|
506
|
+
throw new Error("Unable to connect to server.");
|
|
507
|
+
}
|
|
508
|
+
const namespace_collection = "deped.divisions";
|
|
509
|
+
const collection = db.collection(namespace_collection);
|
|
510
|
+
const { getCache, setCache, delNamespace } = useCache2(namespace_collection);
|
|
511
|
+
async function createIndexes() {
|
|
512
|
+
try {
|
|
513
|
+
await collection.createIndexes([
|
|
514
|
+
{ key: { name: 1 } },
|
|
515
|
+
{ key: { createdAt: 1 } },
|
|
516
|
+
{ key: { name: "text" } },
|
|
517
|
+
{ key: { name: 1 }, unique: true, name: "unique_name" }
|
|
518
|
+
]);
|
|
519
|
+
} catch (error) {
|
|
520
|
+
throw new Error("Failed to create index on divisions.");
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function delCachedData() {
|
|
524
|
+
delNamespace().then(() => {
|
|
525
|
+
logger2.log({
|
|
526
|
+
level: "info",
|
|
527
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
528
|
+
});
|
|
529
|
+
}).catch((err) => {
|
|
530
|
+
logger2.log({
|
|
531
|
+
level: "error",
|
|
532
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
async function add(value, session) {
|
|
537
|
+
try {
|
|
538
|
+
value = modelDivision(value);
|
|
539
|
+
const res = await collection.insertOne(value, { session });
|
|
540
|
+
delCachedData();
|
|
541
|
+
return res.insertedId;
|
|
542
|
+
} catch (error) {
|
|
543
|
+
logger2.log({
|
|
544
|
+
level: "error",
|
|
545
|
+
message: error.message
|
|
546
|
+
});
|
|
547
|
+
if (error instanceof AppError2) {
|
|
548
|
+
throw error;
|
|
549
|
+
} else {
|
|
550
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
551
|
+
if (isDuplicated) {
|
|
552
|
+
throw new BadRequestError5("Division already exists.");
|
|
553
|
+
}
|
|
554
|
+
throw new Error("Failed to create division.");
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
async function getAll({
|
|
559
|
+
search = "",
|
|
560
|
+
page = 1,
|
|
561
|
+
limit = 10,
|
|
562
|
+
sort = {},
|
|
563
|
+
status = "active"
|
|
564
|
+
} = {}) {
|
|
565
|
+
page = page > 0 ? page - 1 : 0;
|
|
566
|
+
const query = {
|
|
567
|
+
deletedAt: { $in: ["", null] },
|
|
568
|
+
status
|
|
569
|
+
};
|
|
570
|
+
sort = Object.keys(sort).length > 0 ? sort : { _id: 1 };
|
|
571
|
+
const cacheKeyOptions = {
|
|
572
|
+
status,
|
|
573
|
+
page,
|
|
574
|
+
limit,
|
|
575
|
+
sort: JSON.stringify(sort)
|
|
576
|
+
};
|
|
577
|
+
if (search) {
|
|
578
|
+
query.$text = { $search: search };
|
|
579
|
+
cacheKeyOptions.search = search;
|
|
580
|
+
}
|
|
581
|
+
const cacheKey = makeCacheKey2(namespace_collection, cacheKeyOptions);
|
|
582
|
+
logger2.log({
|
|
583
|
+
level: "info",
|
|
584
|
+
message: `Cache key for getAll divisions: ${cacheKey}`
|
|
585
|
+
});
|
|
586
|
+
try {
|
|
587
|
+
const cached = await getCache(cacheKey);
|
|
588
|
+
if (cached) {
|
|
589
|
+
logger2.log({
|
|
590
|
+
level: "info",
|
|
591
|
+
message: `Cache hit for getAll divisions: ${cacheKey}`
|
|
592
|
+
});
|
|
593
|
+
return cached;
|
|
594
|
+
}
|
|
595
|
+
const items = await collection.aggregate([
|
|
596
|
+
{ $match: query },
|
|
597
|
+
{ $sort: sort },
|
|
598
|
+
{ $skip: page * limit },
|
|
599
|
+
{ $limit: limit }
|
|
600
|
+
]).toArray();
|
|
601
|
+
const length = await collection.countDocuments(query);
|
|
602
|
+
const data = paginate2(items, page, limit, length);
|
|
603
|
+
setCache(cacheKey, data, 600).then(() => {
|
|
604
|
+
logger2.log({
|
|
605
|
+
level: "info",
|
|
606
|
+
message: `Cache set for getAll divisions: ${cacheKey}`
|
|
607
|
+
});
|
|
608
|
+
}).catch((err) => {
|
|
609
|
+
logger2.log({
|
|
610
|
+
level: "error",
|
|
611
|
+
message: `Failed to set cache for getAll divisions: ${err.message}`
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
return data;
|
|
615
|
+
} catch (error) {
|
|
616
|
+
logger2.log({ level: "error", message: `${error}` });
|
|
617
|
+
throw error;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
async function getById(_id) {
|
|
621
|
+
try {
|
|
622
|
+
_id = new ObjectId4(_id);
|
|
623
|
+
} catch (error) {
|
|
624
|
+
throw new BadRequestError5("Invalid ID.");
|
|
625
|
+
}
|
|
626
|
+
const cacheKey = makeCacheKey2(namespace_collection, { _id: String(_id) });
|
|
627
|
+
try {
|
|
628
|
+
const cached = await getCache(cacheKey);
|
|
629
|
+
if (cached) {
|
|
630
|
+
logger2.log({
|
|
631
|
+
level: "info",
|
|
632
|
+
message: `Cache hit for getById division: ${cacheKey}`
|
|
633
|
+
});
|
|
634
|
+
return cached;
|
|
635
|
+
}
|
|
636
|
+
const result = await collection.findOne({
|
|
637
|
+
_id,
|
|
638
|
+
deletedAt: { $in: ["", null] }
|
|
639
|
+
});
|
|
640
|
+
if (!result) {
|
|
641
|
+
throw new BadRequestError5("Division not found.");
|
|
642
|
+
}
|
|
643
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
644
|
+
logger2.log({
|
|
645
|
+
level: "info",
|
|
646
|
+
message: `Cache set for division by id: ${cacheKey}`
|
|
647
|
+
});
|
|
648
|
+
}).catch((err) => {
|
|
649
|
+
logger2.log({
|
|
650
|
+
level: "error",
|
|
651
|
+
message: `Failed to set cache for division by id: ${err.message}`
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
return result;
|
|
655
|
+
} catch (error) {
|
|
656
|
+
if (error instanceof AppError2) {
|
|
657
|
+
throw error;
|
|
658
|
+
} else {
|
|
659
|
+
throw new InternalServerError2("Failed to get division.");
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
async function getByName(name) {
|
|
664
|
+
const cacheKey = makeCacheKey2(namespace_collection, { name });
|
|
665
|
+
try {
|
|
666
|
+
const cached = await getCache(cacheKey);
|
|
667
|
+
if (cached) {
|
|
668
|
+
logger2.log({
|
|
669
|
+
level: "info",
|
|
670
|
+
message: `Cache hit for getByName division: ${cacheKey}`
|
|
671
|
+
});
|
|
672
|
+
return cached;
|
|
673
|
+
}
|
|
674
|
+
const result = await collection.findOne({
|
|
675
|
+
name,
|
|
676
|
+
deletedAt: { $in: ["", null] }
|
|
677
|
+
});
|
|
678
|
+
if (!result) {
|
|
679
|
+
throw new BadRequestError5("Division not found.");
|
|
680
|
+
}
|
|
681
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
682
|
+
logger2.log({
|
|
683
|
+
level: "info",
|
|
684
|
+
message: `Cache set for division by name: ${cacheKey}`
|
|
685
|
+
});
|
|
686
|
+
}).catch((err) => {
|
|
687
|
+
logger2.log({
|
|
688
|
+
level: "error",
|
|
689
|
+
message: `Failed to set cache for division by name: ${err.message}`
|
|
690
|
+
});
|
|
691
|
+
});
|
|
692
|
+
return result;
|
|
693
|
+
} catch (error) {
|
|
694
|
+
if (error instanceof AppError2) {
|
|
695
|
+
throw error;
|
|
696
|
+
} else {
|
|
697
|
+
throw new InternalServerError2("Failed to get division.");
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
702
|
+
const allowedFields = ["name"];
|
|
703
|
+
if (!allowedFields.includes(field)) {
|
|
704
|
+
throw new BadRequestError5(
|
|
705
|
+
`Field "${field}" is not allowed to be updated.`
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
_id = new ObjectId4(_id);
|
|
710
|
+
} catch (error) {
|
|
711
|
+
throw new BadRequestError5("Invalid ID.");
|
|
712
|
+
}
|
|
713
|
+
try {
|
|
714
|
+
await collection.updateOne(
|
|
715
|
+
{ _id, deletedAt: { $in: ["", null] } },
|
|
716
|
+
{ $set: { [field]: value, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
|
|
717
|
+
{ session }
|
|
718
|
+
);
|
|
719
|
+
delCachedData();
|
|
720
|
+
return `Successfully updated division ${field}.`;
|
|
721
|
+
} catch (error) {
|
|
722
|
+
throw new InternalServerError2(`Failed to update division ${field}.`);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
async function deleteById(_id) {
|
|
726
|
+
try {
|
|
727
|
+
_id = new ObjectId4(_id);
|
|
728
|
+
} catch (error) {
|
|
729
|
+
throw new BadRequestError5("Invalid ID.");
|
|
730
|
+
}
|
|
731
|
+
try {
|
|
732
|
+
await collection.updateOne(
|
|
733
|
+
{ _id },
|
|
734
|
+
{ $set: { deletedAt: (/* @__PURE__ */ new Date()).toISOString() } }
|
|
735
|
+
);
|
|
736
|
+
delCachedData();
|
|
737
|
+
return "Successfully deleted division.";
|
|
738
|
+
} catch (error) {
|
|
739
|
+
throw new InternalServerError2("Failed to delete division.");
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return {
|
|
743
|
+
createIndexes,
|
|
744
|
+
add,
|
|
745
|
+
getAll,
|
|
746
|
+
getById,
|
|
747
|
+
updateFieldById,
|
|
748
|
+
deleteById,
|
|
749
|
+
getByName
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// src/controllers/division.controller.ts
|
|
754
|
+
import { BadRequestError as BadRequestError6 } from "@eeplatform/nodejs-utils";
|
|
755
|
+
import Joi4 from "joi";
|
|
756
|
+
function useDivisionController() {
|
|
757
|
+
const {
|
|
758
|
+
add: _add,
|
|
759
|
+
getAll: _getAll,
|
|
760
|
+
getById: _getById,
|
|
761
|
+
getByName: _getByName,
|
|
762
|
+
updateFieldById: _updateFieldById,
|
|
763
|
+
deleteById: _deleteById
|
|
764
|
+
} = useDivisionRepo();
|
|
765
|
+
async function add(req, res, next) {
|
|
766
|
+
const value = req.body;
|
|
767
|
+
const { error } = schemaDivision.validate(value);
|
|
768
|
+
if (error) {
|
|
769
|
+
next(new BadRequestError6(error.message));
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
try {
|
|
773
|
+
const data = await _add(value);
|
|
774
|
+
res.json({
|
|
775
|
+
message: "Successfully created division.",
|
|
776
|
+
data
|
|
777
|
+
});
|
|
778
|
+
return;
|
|
779
|
+
} catch (error2) {
|
|
780
|
+
next(error2);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
async function getAll(req, res, next) {
|
|
784
|
+
const query = req.query;
|
|
785
|
+
const validation = Joi4.object({
|
|
786
|
+
page: Joi4.number().min(1).optional().allow("", null),
|
|
787
|
+
limit: Joi4.number().min(1).optional().allow("", null),
|
|
788
|
+
search: Joi4.string().optional().allow("", null),
|
|
789
|
+
status: Joi4.string().optional().allow("", null)
|
|
790
|
+
});
|
|
791
|
+
const { error } = validation.validate(query);
|
|
792
|
+
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
793
|
+
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
794
|
+
const search = req.query.search ?? "";
|
|
795
|
+
const status = req.query.status ?? "active";
|
|
796
|
+
const isPageNumber = isFinite(page);
|
|
797
|
+
if (!isPageNumber) {
|
|
798
|
+
next(new BadRequestError6("Invalid page number."));
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
const isLimitNumber = isFinite(limit);
|
|
802
|
+
if (!isLimitNumber) {
|
|
803
|
+
next(new BadRequestError6("Invalid limit number."));
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
if (error) {
|
|
807
|
+
next(new BadRequestError6(error.message));
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
try {
|
|
811
|
+
const data = await _getAll({ page, limit, search, status });
|
|
812
|
+
res.json(data);
|
|
813
|
+
return;
|
|
814
|
+
} catch (error2) {
|
|
815
|
+
next(error2);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
async function getById(req, res, next) {
|
|
819
|
+
const id = req.params.id;
|
|
820
|
+
const validation = Joi4.object({
|
|
821
|
+
id: Joi4.string().hex().required()
|
|
822
|
+
});
|
|
823
|
+
const { error } = validation.validate({ id });
|
|
824
|
+
if (error) {
|
|
825
|
+
next(new BadRequestError6(error.message));
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
try {
|
|
829
|
+
const data = await _getById(id);
|
|
830
|
+
res.json({
|
|
831
|
+
message: "Successfully retrieved division.",
|
|
832
|
+
data
|
|
833
|
+
});
|
|
834
|
+
return;
|
|
835
|
+
} catch (error2) {
|
|
836
|
+
next(error2);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
async function getByName(req, res, next) {
|
|
840
|
+
const name = req.params.name;
|
|
841
|
+
const validation = Joi4.object({
|
|
842
|
+
name: Joi4.string().required()
|
|
843
|
+
});
|
|
844
|
+
const { error } = validation.validate({ name });
|
|
845
|
+
if (error) {
|
|
846
|
+
next(new BadRequestError6(error.message));
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
try {
|
|
850
|
+
const data = await _getByName(name);
|
|
851
|
+
res.json({
|
|
852
|
+
message: "Successfully retrieved division.",
|
|
853
|
+
data
|
|
854
|
+
});
|
|
855
|
+
return;
|
|
856
|
+
} catch (error2) {
|
|
857
|
+
next(error2);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
async function updateField(req, res, next) {
|
|
861
|
+
const _id = req.params.id;
|
|
862
|
+
const { field, value } = req.body;
|
|
863
|
+
const validation = Joi4.object({
|
|
864
|
+
_id: Joi4.string().hex().required(),
|
|
865
|
+
field: Joi4.string().valid("name", "director", "directorName").required(),
|
|
866
|
+
value: Joi4.string().required()
|
|
867
|
+
});
|
|
868
|
+
const { error } = validation.validate({ _id, field, value });
|
|
869
|
+
if (error) {
|
|
870
|
+
next(new BadRequestError6(error.message));
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
try {
|
|
874
|
+
const message = await _updateFieldById({ _id, field, value });
|
|
875
|
+
res.json({ message });
|
|
876
|
+
return;
|
|
877
|
+
} catch (error2) {
|
|
878
|
+
next(error2);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
async function deleteById(req, res, next) {
|
|
882
|
+
const _id = req.params.id;
|
|
883
|
+
const validation = Joi4.object({
|
|
884
|
+
_id: Joi4.string().hex().required()
|
|
885
|
+
});
|
|
886
|
+
const { error } = validation.validate({ _id });
|
|
887
|
+
if (error) {
|
|
888
|
+
next(new BadRequestError6(error.message));
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
try {
|
|
892
|
+
const message = await _deleteById(_id);
|
|
893
|
+
res.json({ message });
|
|
894
|
+
return;
|
|
895
|
+
} catch (error2) {
|
|
896
|
+
next(error2);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return {
|
|
900
|
+
add,
|
|
901
|
+
getAll,
|
|
902
|
+
getById,
|
|
903
|
+
getByName,
|
|
904
|
+
updateField,
|
|
905
|
+
deleteById
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
// src/models/school.model.ts
|
|
910
|
+
import { BadRequestError as BadRequestError7 } from "@eeplatform/nodejs-utils";
|
|
911
|
+
import Joi5 from "joi";
|
|
912
|
+
import { ObjectId as ObjectId5 } from "mongodb";
|
|
913
|
+
var schemaSchool = Joi5.object({
|
|
914
|
+
_id: Joi5.string().hex().optional().allow(null, ""),
|
|
915
|
+
name: Joi5.string().min(1).max(100).required(),
|
|
916
|
+
region: Joi5.string().hex().optional().allow(null, ""),
|
|
917
|
+
regionName: Joi5.string().min(1).max(100).optional().allow(null, ""),
|
|
918
|
+
division: Joi5.string().hex().optional().allow(null, ""),
|
|
919
|
+
divisionName: Joi5.string().min(1).max(100).optional().allow(null, ""),
|
|
920
|
+
principal: Joi5.string().hex().optional().allow(null, ""),
|
|
921
|
+
principalName: Joi5.string().min(1).max(100).optional().allow(null, ""),
|
|
922
|
+
createdAt: Joi5.string().isoDate().optional(),
|
|
923
|
+
updatedAt: Joi5.string().isoDate().optional(),
|
|
924
|
+
deletedAt: Joi5.string().isoDate().optional().allow(null, "")
|
|
925
|
+
});
|
|
926
|
+
function modelSchool(value) {
|
|
927
|
+
const { error } = schemaSchool.validate(value);
|
|
928
|
+
if (error) {
|
|
929
|
+
throw new BadRequestError7(`Invalid sdo data: ${error.message}`);
|
|
930
|
+
}
|
|
931
|
+
if (value._id && typeof value._id === "string") {
|
|
932
|
+
try {
|
|
933
|
+
value._id = ObjectId5.createFromTime(value._id);
|
|
934
|
+
} catch (error2) {
|
|
935
|
+
throw new Error("Invalid _id.");
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return {
|
|
939
|
+
_id: value._id,
|
|
940
|
+
name: value.name,
|
|
941
|
+
region: value.region,
|
|
942
|
+
regionName: value.regionName ?? "",
|
|
943
|
+
division: value.division,
|
|
944
|
+
divisionName: value.divisionName ?? "",
|
|
945
|
+
principal: value.principal,
|
|
946
|
+
principalName: value.principalName ?? "",
|
|
947
|
+
createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
948
|
+
updatedAt: value.updatedAt ?? "",
|
|
949
|
+
deletedAt: value.deletedAt ?? ""
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// src/repositories/school.repository.ts
|
|
954
|
+
import {
|
|
955
|
+
AppError as AppError3,
|
|
956
|
+
BadRequestError as BadRequestError8,
|
|
957
|
+
InternalServerError as InternalServerError3,
|
|
958
|
+
logger as logger3,
|
|
959
|
+
makeCacheKey as makeCacheKey3,
|
|
960
|
+
paginate as paginate3,
|
|
961
|
+
useAtlas as useAtlas3,
|
|
962
|
+
useCache as useCache3
|
|
963
|
+
} from "@eeplatform/nodejs-utils";
|
|
964
|
+
import { ObjectId as ObjectId6 } from "mongodb";
|
|
965
|
+
function useSchoolRepo() {
|
|
966
|
+
const db = useAtlas3.getDb();
|
|
967
|
+
if (!db) {
|
|
968
|
+
throw new Error("Unable to connect to server.");
|
|
969
|
+
}
|
|
970
|
+
const namespace_collection = "deped.schools";
|
|
971
|
+
const collection = db.collection(namespace_collection);
|
|
972
|
+
const { getCache, setCache, delNamespace } = useCache3(namespace_collection);
|
|
973
|
+
async function createIndexes() {
|
|
974
|
+
try {
|
|
975
|
+
await collection.createIndexes([
|
|
976
|
+
{ key: { name: 1 } },
|
|
977
|
+
{ key: { createdAt: 1 } },
|
|
978
|
+
{ key: { name: "text" } },
|
|
979
|
+
{ key: { name: 1 }, unique: true, name: "unique_name" }
|
|
980
|
+
]);
|
|
981
|
+
} catch (error) {
|
|
982
|
+
throw new Error("Failed to create index on schools.");
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
function delCachedData() {
|
|
986
|
+
delNamespace().then(() => {
|
|
987
|
+
logger3.log({
|
|
988
|
+
level: "info",
|
|
989
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
990
|
+
});
|
|
991
|
+
}).catch((err) => {
|
|
992
|
+
logger3.log({
|
|
993
|
+
level: "error",
|
|
994
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
995
|
+
});
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
async function add(value, session) {
|
|
999
|
+
try {
|
|
1000
|
+
value = modelSchool(value);
|
|
1001
|
+
const res = await collection.insertOne(value, { session });
|
|
1002
|
+
delCachedData();
|
|
1003
|
+
return res.insertedId;
|
|
1004
|
+
} catch (error) {
|
|
1005
|
+
logger3.log({
|
|
1006
|
+
level: "error",
|
|
1007
|
+
message: error.message
|
|
1008
|
+
});
|
|
1009
|
+
if (error instanceof AppError3) {
|
|
1010
|
+
throw error;
|
|
1011
|
+
} else {
|
|
1012
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
1013
|
+
if (isDuplicated) {
|
|
1014
|
+
throw new BadRequestError8("School already exists.");
|
|
1015
|
+
}
|
|
1016
|
+
throw new Error("Failed to create school.");
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
async function getAll({
|
|
1021
|
+
search = "",
|
|
1022
|
+
page = 1,
|
|
1023
|
+
limit = 10,
|
|
1024
|
+
sort = {},
|
|
1025
|
+
status = "active"
|
|
1026
|
+
} = {}) {
|
|
1027
|
+
page = page > 0 ? page - 1 : 0;
|
|
1028
|
+
const query = {
|
|
1029
|
+
deletedAt: { $in: ["", null] },
|
|
1030
|
+
status
|
|
1031
|
+
};
|
|
1032
|
+
sort = Object.keys(sort).length > 0 ? sort : { _id: 1 };
|
|
1033
|
+
const cacheKeyOptions = {
|
|
1034
|
+
status,
|
|
1035
|
+
page,
|
|
1036
|
+
limit,
|
|
1037
|
+
sort: JSON.stringify(sort)
|
|
1038
|
+
};
|
|
1039
|
+
if (search) {
|
|
1040
|
+
query.$text = { $search: search };
|
|
1041
|
+
cacheKeyOptions.search = search;
|
|
1042
|
+
}
|
|
1043
|
+
const cacheKey = makeCacheKey3(namespace_collection, cacheKeyOptions);
|
|
1044
|
+
logger3.log({
|
|
1045
|
+
level: "info",
|
|
1046
|
+
message: `Cache key for getAll schools: ${cacheKey}`
|
|
1047
|
+
});
|
|
1048
|
+
try {
|
|
1049
|
+
const cached = await getCache(cacheKey);
|
|
1050
|
+
if (cached) {
|
|
1051
|
+
logger3.log({
|
|
1052
|
+
level: "info",
|
|
1053
|
+
message: `Cache hit for getAll schools: ${cacheKey}`
|
|
1054
|
+
});
|
|
1055
|
+
return cached;
|
|
1056
|
+
}
|
|
1057
|
+
const items = await collection.aggregate([
|
|
1058
|
+
{ $match: query },
|
|
1059
|
+
{ $sort: sort },
|
|
1060
|
+
{ $skip: page * limit },
|
|
1061
|
+
{ $limit: limit }
|
|
1062
|
+
]).toArray();
|
|
1063
|
+
const length = await collection.countDocuments(query);
|
|
1064
|
+
const data = paginate3(items, page, limit, length);
|
|
1065
|
+
setCache(cacheKey, data, 600).then(() => {
|
|
1066
|
+
logger3.log({
|
|
1067
|
+
level: "info",
|
|
1068
|
+
message: `Cache set for getAll schools: ${cacheKey}`
|
|
1069
|
+
});
|
|
1070
|
+
}).catch((err) => {
|
|
1071
|
+
logger3.log({
|
|
1072
|
+
level: "error",
|
|
1073
|
+
message: `Failed to set cache for getAll schools: ${err.message}`
|
|
1074
|
+
});
|
|
1075
|
+
});
|
|
1076
|
+
return data;
|
|
1077
|
+
} catch (error) {
|
|
1078
|
+
logger3.log({ level: "error", message: `${error}` });
|
|
1079
|
+
throw error;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
async function getById(_id) {
|
|
1083
|
+
try {
|
|
1084
|
+
_id = new ObjectId6(_id);
|
|
1085
|
+
} catch (error) {
|
|
1086
|
+
throw new BadRequestError8("Invalid ID.");
|
|
1087
|
+
}
|
|
1088
|
+
const cacheKey = makeCacheKey3(namespace_collection, { _id: String(_id) });
|
|
1089
|
+
try {
|
|
1090
|
+
const cached = await getCache(cacheKey);
|
|
1091
|
+
if (cached) {
|
|
1092
|
+
logger3.log({
|
|
1093
|
+
level: "info",
|
|
1094
|
+
message: `Cache hit for getById school: ${cacheKey}`
|
|
1095
|
+
});
|
|
1096
|
+
return cached;
|
|
1097
|
+
}
|
|
1098
|
+
const result = await collection.findOne({
|
|
1099
|
+
_id,
|
|
1100
|
+
deletedAt: { $in: ["", null] }
|
|
1101
|
+
});
|
|
1102
|
+
if (!result) {
|
|
1103
|
+
throw new BadRequestError8("School not found.");
|
|
1104
|
+
}
|
|
1105
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
1106
|
+
logger3.log({
|
|
1107
|
+
level: "info",
|
|
1108
|
+
message: `Cache set for school by id: ${cacheKey}`
|
|
1109
|
+
});
|
|
1110
|
+
}).catch((err) => {
|
|
1111
|
+
logger3.log({
|
|
1112
|
+
level: "error",
|
|
1113
|
+
message: `Failed to set cache for school by id: ${err.message}`
|
|
1114
|
+
});
|
|
1115
|
+
});
|
|
1116
|
+
return result;
|
|
1117
|
+
} catch (error) {
|
|
1118
|
+
if (error instanceof AppError3) {
|
|
1119
|
+
throw error;
|
|
1120
|
+
} else {
|
|
1121
|
+
throw new InternalServerError3("Failed to get school.");
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
async function getByName(name) {
|
|
1126
|
+
const cacheKey = makeCacheKey3(namespace_collection, { name });
|
|
1127
|
+
try {
|
|
1128
|
+
const cached = await getCache(cacheKey);
|
|
1129
|
+
if (cached) {
|
|
1130
|
+
logger3.log({
|
|
1131
|
+
level: "info",
|
|
1132
|
+
message: `Cache hit for getByName school: ${cacheKey}`
|
|
1133
|
+
});
|
|
1134
|
+
return cached;
|
|
1135
|
+
}
|
|
1136
|
+
const result = await collection.findOne({
|
|
1137
|
+
name,
|
|
1138
|
+
deletedAt: { $in: ["", null] }
|
|
1139
|
+
});
|
|
1140
|
+
if (!result) {
|
|
1141
|
+
throw new BadRequestError8("School not found.");
|
|
1142
|
+
}
|
|
1143
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
1144
|
+
logger3.log({
|
|
1145
|
+
level: "info",
|
|
1146
|
+
message: `Cache set for school by name: ${cacheKey}`
|
|
1147
|
+
});
|
|
1148
|
+
}).catch((err) => {
|
|
1149
|
+
logger3.log({
|
|
1150
|
+
level: "error",
|
|
1151
|
+
message: `Failed to set cache for school by name: ${err.message}`
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
return result;
|
|
1155
|
+
} catch (error) {
|
|
1156
|
+
if (error instanceof AppError3) {
|
|
1157
|
+
throw error;
|
|
1158
|
+
} else {
|
|
1159
|
+
throw new InternalServerError3("Failed to get school.");
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
1164
|
+
const allowedFields = ["name"];
|
|
1165
|
+
if (!allowedFields.includes(field)) {
|
|
1166
|
+
throw new BadRequestError8(
|
|
1167
|
+
`Field "${field}" is not allowed to be updated.`
|
|
1168
|
+
);
|
|
1169
|
+
}
|
|
1170
|
+
try {
|
|
1171
|
+
_id = new ObjectId6(_id);
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
throw new BadRequestError8("Invalid ID.");
|
|
1174
|
+
}
|
|
1175
|
+
try {
|
|
1176
|
+
await collection.updateOne(
|
|
1177
|
+
{ _id, deletedAt: { $in: ["", null] } },
|
|
1178
|
+
{ $set: { [field]: value, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
|
|
1179
|
+
{ session }
|
|
1180
|
+
);
|
|
1181
|
+
delCachedData();
|
|
1182
|
+
return `Successfully updated school ${field}.`;
|
|
1183
|
+
} catch (error) {
|
|
1184
|
+
throw new InternalServerError3(`Failed to update school ${field}.`);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
async function deleteById(_id) {
|
|
1188
|
+
try {
|
|
1189
|
+
_id = new ObjectId6(_id);
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
throw new BadRequestError8("Invalid ID.");
|
|
1192
|
+
}
|
|
1193
|
+
try {
|
|
1194
|
+
await collection.updateOne(
|
|
1195
|
+
{ _id },
|
|
1196
|
+
{ $set: { deletedAt: (/* @__PURE__ */ new Date()).toISOString() } }
|
|
1197
|
+
);
|
|
1198
|
+
delCachedData();
|
|
1199
|
+
return "Successfully deleted school.";
|
|
1200
|
+
} catch (error) {
|
|
1201
|
+
throw new InternalServerError3("Failed to delete school.");
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
return {
|
|
1205
|
+
createIndexes,
|
|
1206
|
+
add,
|
|
1207
|
+
getAll,
|
|
1208
|
+
getById,
|
|
1209
|
+
updateFieldById,
|
|
1210
|
+
deleteById,
|
|
1211
|
+
getByName
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
// src/controllers/school.controller.ts
|
|
1216
|
+
import { BadRequestError as BadRequestError9 } from "@eeplatform/nodejs-utils";
|
|
1217
|
+
import Joi6 from "joi";
|
|
1218
|
+
function useSchoolController() {
|
|
1219
|
+
const {
|
|
1220
|
+
add: _add,
|
|
1221
|
+
getAll: _getAll,
|
|
1222
|
+
getById: _getById,
|
|
1223
|
+
getByName: _getByName,
|
|
1224
|
+
updateFieldById: _updateFieldById,
|
|
1225
|
+
deleteById: _deleteById
|
|
1226
|
+
} = useSchoolRepo();
|
|
1227
|
+
async function add(req, res, next) {
|
|
1228
|
+
const value = req.body;
|
|
1229
|
+
const { error } = schemaSchool.validate(value);
|
|
1230
|
+
if (error) {
|
|
1231
|
+
next(new BadRequestError9(error.message));
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
try {
|
|
1235
|
+
const data = await _add(value);
|
|
1236
|
+
res.json({
|
|
1237
|
+
message: "Successfully created school.",
|
|
1238
|
+
data
|
|
1239
|
+
});
|
|
1240
|
+
return;
|
|
1241
|
+
} catch (error2) {
|
|
1242
|
+
next(error2);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
async function getAll(req, res, next) {
|
|
1246
|
+
const query = req.query;
|
|
1247
|
+
const validation = Joi6.object({
|
|
1248
|
+
page: Joi6.number().min(1).optional().allow("", null),
|
|
1249
|
+
limit: Joi6.number().min(1).optional().allow("", null),
|
|
1250
|
+
search: Joi6.string().optional().allow("", null),
|
|
1251
|
+
status: Joi6.string().optional().allow("", null)
|
|
1252
|
+
});
|
|
1253
|
+
const { error } = validation.validate(query);
|
|
1254
|
+
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
1255
|
+
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
1256
|
+
const search = req.query.search ?? "";
|
|
1257
|
+
const status = req.query.status ?? "active";
|
|
1258
|
+
const isPageNumber = isFinite(page);
|
|
1259
|
+
if (!isPageNumber) {
|
|
1260
|
+
next(new BadRequestError9("Invalid page number."));
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
const isLimitNumber = isFinite(limit);
|
|
1264
|
+
if (!isLimitNumber) {
|
|
1265
|
+
next(new BadRequestError9("Invalid limit number."));
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
if (error) {
|
|
1269
|
+
next(new BadRequestError9(error.message));
|
|
1270
|
+
return;
|
|
1271
|
+
}
|
|
1272
|
+
try {
|
|
1273
|
+
const data = await _getAll({ page, limit, search, status });
|
|
1274
|
+
res.json(data);
|
|
1275
|
+
return;
|
|
1276
|
+
} catch (error2) {
|
|
1277
|
+
next(error2);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
async function getById(req, res, next) {
|
|
1281
|
+
const id = req.params.id;
|
|
1282
|
+
const validation = Joi6.object({
|
|
1283
|
+
id: Joi6.string().hex().required()
|
|
1284
|
+
});
|
|
1285
|
+
const { error } = validation.validate({ id });
|
|
1286
|
+
if (error) {
|
|
1287
|
+
next(new BadRequestError9(error.message));
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
try {
|
|
1291
|
+
const data = await _getById(id);
|
|
1292
|
+
res.json({
|
|
1293
|
+
message: "Successfully retrieved school.",
|
|
1294
|
+
data
|
|
1295
|
+
});
|
|
1296
|
+
return;
|
|
1297
|
+
} catch (error2) {
|
|
1298
|
+
next(error2);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
async function getByName(req, res, next) {
|
|
1302
|
+
const name = req.params.name;
|
|
1303
|
+
const validation = Joi6.object({
|
|
1304
|
+
name: Joi6.string().required()
|
|
1305
|
+
});
|
|
1306
|
+
const { error } = validation.validate({ name });
|
|
1307
|
+
if (error) {
|
|
1308
|
+
next(new BadRequestError9(error.message));
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
try {
|
|
1312
|
+
const data = await _getByName(name);
|
|
1313
|
+
res.json({
|
|
1314
|
+
message: "Successfully retrieved school.",
|
|
1315
|
+
data
|
|
1316
|
+
});
|
|
1317
|
+
return;
|
|
1318
|
+
} catch (error2) {
|
|
1319
|
+
next(error2);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
async function updateField(req, res, next) {
|
|
1323
|
+
const _id = req.params.id;
|
|
1324
|
+
const { field, value } = req.body;
|
|
1325
|
+
const validation = Joi6.object({
|
|
1326
|
+
_id: Joi6.string().hex().required(),
|
|
1327
|
+
field: Joi6.string().valid("name", "director", "directorName").required(),
|
|
1328
|
+
value: Joi6.string().required()
|
|
1329
|
+
});
|
|
1330
|
+
const { error } = validation.validate({ _id, field, value });
|
|
1331
|
+
if (error) {
|
|
1332
|
+
next(new BadRequestError9(error.message));
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
try {
|
|
1336
|
+
const message = await _updateFieldById({ _id, field, value });
|
|
1337
|
+
res.json({ message });
|
|
1338
|
+
return;
|
|
1339
|
+
} catch (error2) {
|
|
1340
|
+
next(error2);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
async function deleteById(req, res, next) {
|
|
1344
|
+
const _id = req.params.id;
|
|
1345
|
+
const validation = Joi6.object({
|
|
1346
|
+
_id: Joi6.string().hex().required()
|
|
1347
|
+
});
|
|
1348
|
+
const { error } = validation.validate({ _id });
|
|
1349
|
+
if (error) {
|
|
1350
|
+
next(new BadRequestError9(error.message));
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
try {
|
|
1354
|
+
const message = await _deleteById(_id);
|
|
1355
|
+
res.json({ message });
|
|
1356
|
+
return;
|
|
1357
|
+
} catch (error2) {
|
|
1358
|
+
next(error2);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
return {
|
|
1362
|
+
add,
|
|
1363
|
+
getAll,
|
|
1364
|
+
getById,
|
|
1365
|
+
getByName,
|
|
1366
|
+
updateField,
|
|
1367
|
+
deleteById
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
export {
|
|
1371
|
+
modelDivision,
|
|
1372
|
+
modelRegion,
|
|
1373
|
+
modelSchool,
|
|
1374
|
+
schemaDivision,
|
|
1375
|
+
schemaRegion,
|
|
1376
|
+
schemaSchool,
|
|
1377
|
+
useDivisionController,
|
|
1378
|
+
useDivisionRepo,
|
|
1379
|
+
useRegionController,
|
|
1380
|
+
useRegionRepo,
|
|
1381
|
+
useSchoolController,
|
|
1382
|
+
useSchoolRepo
|
|
1383
|
+
};
|
|
1384
|
+
//# sourceMappingURL=index.mjs.map
|