59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
import { Router } from 'express';
|
|
|
|
import { getLookup, listLookups } from '../services/lookupService.js';
|
|
import { asyncHandler } from '../utils/asyncHandler.js';
|
|
import { validateParam } from '../middleware/validateParams.js';
|
|
import { lookupCache } from '../services/cacheService.js';
|
|
|
|
const router = Router();
|
|
|
|
router.get(
|
|
'/',
|
|
asyncHandler(async (_req, res) => {
|
|
/*
|
|
* The bulk lookup endpoint is convenient for client startup because dropdown
|
|
* lists are small and reused across many dynamic fields. Fetching them in one
|
|
* call keeps the frontend startup sequence short.
|
|
*/
|
|
const cached = lookupCache.get('all-lookups');
|
|
|
|
if (cached) {
|
|
return res.json(cached);
|
|
}
|
|
|
|
const lookups = await listLookups();
|
|
const payload = { items: lookups };
|
|
lookupCache.set('all-lookups', payload);
|
|
return res.json(payload);
|
|
})
|
|
);
|
|
|
|
router.get(
|
|
'/:lookupCode',
|
|
validateParam('lookupCode'),
|
|
asyncHandler(async (req, res) => {
|
|
/*
|
|
* The single-lookup endpoint is still useful for debugging and for possible
|
|
* future optimization if the number of lookup sets grows and startup payloads
|
|
* need to become more selective.
|
|
*/
|
|
const cacheKey = `lookup-${req.params.lookupCode}`;
|
|
const cached = lookupCache.get(cacheKey);
|
|
|
|
if (cached) {
|
|
return res.json(cached);
|
|
}
|
|
|
|
const lookup = await getLookup(req.params.lookupCode);
|
|
|
|
if (!lookup) {
|
|
return res.status(404).json({ message: 'Lookup not found.' });
|
|
}
|
|
|
|
lookupCache.set(cacheKey, lookup);
|
|
return res.json(lookup);
|
|
})
|
|
);
|
|
|
|
export default router;
|