DTP Base provides a scalable and secure Node.js application development harness ready for production service.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

57 lines
1.7 KiB

'use strict';
import mongoose from 'mongoose';
import { SiteError } from '../../../lib/site-lib.js';
export function populateImageId (controller) {
const { image: imageService } = controller.dtp.services;
return async function (req, res, next, imageId) {
try {
res.locals.imageId = mongoose.Types.ObjectId.createFromHexString(imageId);
res.locals.image = await imageService.getImageById(res.locals.imageId);
if (!res.locals.image) {
throw new SiteError(404, 'Image not found');
}
return next();
} catch (error) {
controller.log.error('failed to populate imageId', { imageId, error });
return next(error);
}
};
}
export function populateUserId (controller) {
const { user: userService } = controller.dtp.services;
return async function (req, res, next, userId) {
if (!mongoose.Types.ObjectId.isValid(userId)) {
return next(new SiteError(406, 'Invalid User'));
}
try {
res.locals.userProfile = await userService.getUserAccount(userId);
if (!res.locals.userProfile) {
throw new SiteError(404, 'User not found');
}
return next();
} catch (error) {
controller.log.error('failed to populate userId', { userId, error });
return next(error);
}
};
}
export function populateVideoId (controller) {
const { video: videoService } = controller.dtp.services;
return async function (req, res, next, videoId) {
try {
res.locals.video = await videoService.getVideoById(videoId);
if (!res.locals.video) {
throw new SiteError(404, 'Video not found');
}
return next();
} catch (error) {
controller.log.error('failed to populate video', { videoId, error });
return next(error);
}
};
}