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.
73 lines
2.3 KiB
73 lines
2.3 KiB
'use strict';
|
|
|
|
import { SiteError } from '../../../lib/site-lib.js';
|
|
|
|
export function populateClientId (controller) {
|
|
return async function (req, res, next, clientId) {
|
|
const { client: clientService } = controller.dtp.services;
|
|
try {
|
|
res.locals.client = await clientService.getClientById(clientId);
|
|
if (!res.locals.client) {
|
|
throw new SiteError(404, 'Client not found');
|
|
}
|
|
if (!res.locals.client.user._id.equals(req.user._id)) {
|
|
throw new SiteError(401, 'This is not your client');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
controller.log.error('failed to populate client', { error });
|
|
return next(error);
|
|
}
|
|
};
|
|
}
|
|
|
|
export function populateProjectId (controller) {
|
|
return async function (req, res, next, projectId) {
|
|
const { client: clientService } = controller.dtp.services;
|
|
try {
|
|
res.locals.project = await clientService.getProjectById(projectId);
|
|
if (!res.locals.project) {
|
|
throw new SiteError(404, 'Project not found');
|
|
}
|
|
if (!res.locals.project.user._id.equals(req.user._id)) {
|
|
throw new SiteError(401, 'This is not your project');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
controller.log.error('failed to populate project', { error });
|
|
return next(error);
|
|
}
|
|
};
|
|
}
|
|
|
|
export function populateTaskId (controller) {
|
|
return async function (req, res, next, taskId) {
|
|
const { task: taskService } = controller.dtp.services;
|
|
try {
|
|
res.locals.task = await taskService.getTaskById(taskId);
|
|
if (!res.locals.task) {
|
|
throw new SiteError(404, 'Task not found');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
controller.log.error('failed to populate taskId', { taskId, error });
|
|
return next(error);
|
|
}
|
|
};
|
|
}
|
|
|
|
export function populateSessionId (controller) {
|
|
return async function (req, res, next, sessionId) {
|
|
const { task: taskService } = controller.dtp.services;
|
|
try {
|
|
res.locals.session = await taskService.getTaskSessionById(sessionId);
|
|
if (!res.locals.session) {
|
|
throw new SiteError(404, 'Task session not found');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
controller.log.error('failed to populate sessionId', { sessionId, error });
|
|
return next(error);
|
|
}
|
|
};
|
|
}
|