forked from StellarEdu/StellarEduPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschoolContext.js
More file actions
52 lines (44 loc) · 1.51 KB
/
Copy pathschoolContext.js
File metadata and controls
52 lines (44 loc) · 1.51 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
'use strict';
const School = require('../models/schoolModel');
/**
* resolveSchool — middleware that identifies the current school from the request.
*
* Lookup strategy (in order of precedence):
* 1. X-School-ID header — opaque schoolId string (e.g. "SCH-3F2A")
* 2. X-School-Slug header — human slug (e.g. "lincoln-high")
*
* On success: attaches req.school (lean School doc) and req.schoolId (string).
* On failure: 400 if no header provided, 404 if school not found or inactive.
*
* Usage:
* router.use(resolveSchool); // apply to all routes in a router
* router.get('/students', resolveSchool, handler); // apply to one route
*/
async function resolveSchool(req, res, next) {
try {
const schoolId = req.headers['x-school-id'];
const schoolSlug = req.headers['x-school-slug'];
if (!schoolId && !schoolSlug) {
return res.status(400).json({
error: 'School context is required. Provide X-School-ID or X-School-Slug header.',
code: 'MISSING_SCHOOL_CONTEXT',
});
}
const query = schoolId
? { schoolId, isActive: true }
: { slug: schoolSlug.toLowerCase().trim(), isActive: true };
const school = await School.findOne(query).lean();
if (!school) {
return res.status(404).json({
error: 'School not found or inactive.',
code: 'SCHOOL_NOT_FOUND',
});
}
req.school = school;
req.schoolId = school.schoolId;
next();
} catch (err) {
next(err);
}
}
module.exports = { resolveSchool };