Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { validationResult, body } = require('express-validator');
- const { roles } = require('../../config/companyTeam');
- const TeamCompanyAggregate = require('../../models/TeamCompanyAggregate');
- const isTimeFormat = (value) => {
- return /^([01]\d|2[0-3]):([0-5]\d)$/.test(value);
- };
- module.exports = {
- createMeetingValidator: [
- body('interviewType').notEmpty().optional({ nullable: true }),
- body('duration').notEmpty().optional({ nullable: true }),
- body('date').isDate().optional({ nullable: true }),
- body('times').isArray().optional({ nullable: true }).withMessage('Times must be an array').custom((value, { req }) => {
- for (let time of value) {
- if (!isTimeFormat(time)) return false;
- }
- return true;
- }).optional({ nullable: true }).withMessage('Invalid time format'),
- body('employmentType').notEmpty().optional({ nullable: true }),
- body('startDate').isDate().optional({ nullable: true }),
- body('payRate').isFloat().optional({ nullable: true }),
- body('payPeriod').notEmpty().optional({ nullable: true }),
- body('payFrequency').notEmpty().optional({ nullable: true }),
- body('recruterName').notEmpty().optional({ nullable: true }),
- body('offerLetter').notEmpty().optional({ nullable: true }),
- body('interviewerId').notEmpty().optional({ nullable: true }).custom(async (value, { req }) => {
- if (value === null) return true;
- const user = (req?.jobApplication?.job?.company?.users || []).find(u => u.userId === req.user.id);
- const isRoleValid = user && user.role === roles.admin.name;
- if (!isRoleValid || req.user.id === parseInt(value)) return false;
- // добавляем пользователя с тимы
- const teamUser = await TeamCompanyAggregate.findOne({
- where: { userId: value, companyId: user?.companyId }
- });
- return !!teamUser;
- }).withMessage('Not have access'),
- (req, res, next) => {
- const errors = validationResult(req).errors.map(error => ({ msg: error.msg, field: error.path }));
- if (!errors.length) return next();
- return res.json({ success: false, message: 'Some fields invalid', errors });
- }
- ]
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement