Initial commit: teaching institution management API.
Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// /components/professors/professorController.js
|
||||
|
||||
const catchAsync = require('../../utils/catchAsync');
|
||||
const professorService = require('./professorService');
|
||||
const { successResponse, listResponse } = require('../../utils/apiResponse');
|
||||
|
||||
exports.create = catchAsync(async (req, res, next) => {
|
||||
const professor = await professorService.createProfessor(req.body);
|
||||
return successResponse(res, 201, 'Professor created successfully', professor);
|
||||
});
|
||||
|
||||
exports.getOne = catchAsync(async (req, res, next) => {
|
||||
const professor = await professorService.getProfessorById(req.params.id);
|
||||
return successResponse(res, 200, 'Professor retrieved successfully', professor);
|
||||
});
|
||||
|
||||
exports.getAll = catchAsync(async (req, res, next) => {
|
||||
const { data, meta } = await professorService.getAllProfessors(req.query);
|
||||
return listResponse(res, 200, data, meta);
|
||||
});
|
||||
|
||||
exports.update = catchAsync(async (req, res, next) => {
|
||||
const professor = await professorService.updateProfessor(req.params.id, req.body);
|
||||
return successResponse(res, 200, 'Professor updated successfully', professor);
|
||||
});
|
||||
|
||||
exports.delete = catchAsync(async (req, res, next) => {
|
||||
await professorService.deleteProfessor(req.params.id);
|
||||
return successResponse(res, 200, 'Professor deleted successfully');
|
||||
});
|
||||
|
||||
exports.search = catchAsync(async (req, res, next) => {
|
||||
const { data, meta } = await professorService.searchProfessors(req.query);
|
||||
return listResponse(res, 200, data, meta);
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
// /components/professors/professorModel.js
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const professorSchema = new mongoose.Schema({
|
||||
nationalIdCode: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true,
|
||||
index: true
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
surname: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
phoneNumber: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true,
|
||||
index: true
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
trim: true,
|
||||
lowercase: true
|
||||
},
|
||||
expertise: [{
|
||||
type: String,
|
||||
trim: true
|
||||
}],
|
||||
courses: [{
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Course'
|
||||
}],
|
||||
isActive: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
module.exports = mongoose.model('Professor', professorSchema);
|
||||
@@ -0,0 +1,21 @@
|
||||
// /components/professors/professorRoutes.js
|
||||
|
||||
const express = require('express');
|
||||
const professorController = require('./professorController');
|
||||
const { validateCreateProfessor, validateUpdateProfessor } = require('./professorValidator');
|
||||
const authMiddleware = require('../../middlewares/authMiddleware');
|
||||
const perm = require('../../middlewares/permissionMiddleware');
|
||||
const { PERMISSIONS } = require('../../constants/permissions');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use(authMiddleware);
|
||||
|
||||
router.post('/admin/create', perm.requires(PERMISSIONS.PROFESSORS_CREATE), validateCreateProfessor, professorController.create);
|
||||
router.get('/admin/get-all', perm.requires(PERMISSIONS.PROFESSORS_READ), professorController.getAll);
|
||||
router.get('/admin/search', perm.requires(PERMISSIONS.PROFESSORS_SEARCH), professorController.search);
|
||||
router.get('/admin/get-one/:id', perm.requires(PERMISSIONS.PROFESSORS_READ), professorController.getOne);
|
||||
router.put('/admin/update/:id', perm.requires(PERMISSIONS.PROFESSORS_UPDATE), validateUpdateProfessor, professorController.update);
|
||||
router.delete('/admin/delete/:id', perm.requires(PERMISSIONS.PROFESSORS_DELETE), professorController.delete);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,79 @@
|
||||
// /components/professors/professorService.js
|
||||
|
||||
const Professor = require('./professorModel');
|
||||
const AppError = require('../../utils/AppError');
|
||||
const eventEmitter = require('../../events/eventEmitter');
|
||||
const EVENT_NAMES = require('../../constants/eventNames');
|
||||
const { parsePaginationAndSort, buildFilterQuery, calculateMeta } = require('../../utils/pagination');
|
||||
|
||||
const createProfessor = async (data) => {
|
||||
const existing = await Professor.findOne({
|
||||
$or: [
|
||||
{ nationalIdCode: data.nationalIdCode },
|
||||
{ phoneNumber: data.phoneNumber },
|
||||
...(data.email ? [{ email: data.email }] : [])
|
||||
]
|
||||
});
|
||||
if (existing) {
|
||||
throw new AppError('PROFESSOR_ALREADY_EXISTS');
|
||||
}
|
||||
|
||||
const professor = await Professor.create(data);
|
||||
eventEmitter.emit(EVENT_NAMES.PROFESSOR_CREATED, { professorId: professor._id, name: `${professor.name} ${professor.surname}` });
|
||||
return professor;
|
||||
};
|
||||
|
||||
const getProfessorById = async (id) => {
|
||||
const professor = await Professor.findById(id).populate('courses', 'title type price');
|
||||
if (!professor) {
|
||||
throw new AppError('PROFESSOR_NOT_FOUND');
|
||||
}
|
||||
return professor;
|
||||
};
|
||||
|
||||
const getAllProfessors = async (queryParams) => {
|
||||
const { page, limit, skip, sort } = parsePaginationAndSort(queryParams);
|
||||
const filter = buildFilterQuery(queryParams, ['name', 'surname', 'nationalIdCode', 'phoneNumber', 'email', 'expertise']);
|
||||
|
||||
const [professors, totalCount] = await Promise.all([
|
||||
Professor.find(filter).populate('courses', 'title').sort(sort).skip(skip).limit(limit),
|
||||
Professor.countDocuments(filter)
|
||||
]);
|
||||
|
||||
const meta = calculateMeta(totalCount, page, limit);
|
||||
return { data: professors, meta };
|
||||
};
|
||||
|
||||
const updateProfessor = async (id, updateData) => {
|
||||
const professor = await Professor.findById(id);
|
||||
if (!professor) {
|
||||
throw new AppError('PROFESSOR_NOT_FOUND');
|
||||
}
|
||||
|
||||
Object.assign(professor, updateData);
|
||||
await professor.save();
|
||||
return professor;
|
||||
};
|
||||
|
||||
const deleteProfessor = async (id) => {
|
||||
const professor = await Professor.findById(id);
|
||||
if (!professor) {
|
||||
throw new AppError('PROFESSOR_NOT_FOUND');
|
||||
}
|
||||
|
||||
await Professor.findByIdAndDelete(id);
|
||||
return null;
|
||||
};
|
||||
|
||||
const searchProfessors = async (queryParams) => {
|
||||
return getAllProfessors(queryParams);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createProfessor,
|
||||
getProfessorById,
|
||||
getAllProfessors,
|
||||
updateProfessor,
|
||||
deleteProfessor,
|
||||
searchProfessors
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
// Stub validator — pass-through middleware (no validation yet)
|
||||
const passThrough = (req, res, next) => next();
|
||||
|
||||
module.exports = new Proxy({}, {
|
||||
get: () => passThrough
|
||||
});
|
||||
Reference in New Issue
Block a user