update
All checks were successful
Deploy to Production / deploy (push) Successful in 21s

This commit is contained in:
silverpro89
2026-01-28 11:21:21 +07:00
parent 57c45d27a3
commit 3791b7cae1
23 changed files with 1033 additions and 317 deletions

View File

@@ -1,6 +1,5 @@
const { School } = require('../models');
const { cacheUtils } = require('../config/redis');
const { addDatabaseWriteJob } = require('../config/bullmq');
/**
* School Controller - Quản lý thông tin trường học
@@ -108,12 +107,11 @@ class SchoolController {
}
/**
* Create new school - Push to BullMQ
* Create new school
*/
async createSchool(req, res, next) {
try {
const schoolData = req.body;
const userId = req.user?.id; // From auth middleware
// Validate required fields (you can use Joi for this)
if (!schoolData.school_code || !schoolData.school_name || !schoolData.school_type) {
@@ -123,22 +121,14 @@ class SchoolController {
});
}
// Add job to BullMQ queue
const job = await addDatabaseWriteJob(
'create',
'School',
schoolData,
{ userId, priority: 3 }
);
const newSchool = await School.create(schoolData);
res.status(202).json({
await cacheUtils.deletePattern('schools:list:*');
res.status(201).json({
success: true,
message: 'School creation queued',
jobId: job.id,
data: {
school_code: schoolData.school_code,
school_name: schoolData.school_name,
},
message: 'School created successfully',
data: newSchool,
});
} catch (error) {
next(error);
@@ -146,44 +136,31 @@ class SchoolController {
}
/**
* Update school - Push to BullMQ
* Update school
*/
async updateSchool(req, res, next) {
try {
const { id } = req.params;
const updateData = req.body;
const userId = req.user?.id;
// Check if school exists (from cache or DB)
const cacheKey = `school:${id}`;
let school = await cacheUtils.get(cacheKey);
const school = await School.findByPk(id);
if (!school) {
school = await School.findByPk(id);
if (!school) {
return res.status(404).json({
success: false,
message: 'School not found',
});
}
return res.status(404).json({
success: false,
message: 'School not found',
});
}
// Add update job to BullMQ
const job = await addDatabaseWriteJob(
'update',
'School',
{ id, ...updateData },
{ userId, priority: 3 }
);
await school.update(updateData);
// Invalidate cache
await cacheUtils.delete(cacheKey);
await cacheUtils.delete(`school:${id}`);
await cacheUtils.deletePattern('schools:list:*');
res.json({
res.status(200).json({
success: true,
message: 'School update queued',
jobId: job.id,
message: 'School updated successfully',
data: school,
});
} catch (error) {
next(error);
@@ -191,12 +168,11 @@ class SchoolController {
}
/**
* Delete school - Push to BullMQ
* Delete school (soft delete)
*/
async deleteSchool(req, res, next) {
try {
const { id } = req.params;
const userId = req.user?.id;
// Check if school exists
const school = await School.findByPk(id);
@@ -208,21 +184,16 @@ class SchoolController {
}
// Soft delete - just mark as inactive
const job = await addDatabaseWriteJob(
'update',
'School',
{ id, is_active: false },
{ userId, priority: 3 }
);
await school.update({ is_active: false });
// Invalidate cache
await cacheUtils.delete(`school:${id}`);
await cacheUtils.deletePattern('schools:list:*');
res.json({
res.status(200).json({
success: true,
message: 'School deletion queued',
jobId: job.id,
message: 'School deactivated successfully',
data: school,
});
} catch (error) {
next(error);