Advertisement
KoctrX

Untitled

Jun 13th, 2024
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { validationResult, body } = require('express-validator');
  2. const { roles } = require('../../config/companyTeam');
  3. const TeamCompanyAggregate = require('../../models/TeamCompanyAggregate');
  4. const isTimeFormat = (value) => {
  5.     return /^([01]\d|2[0-3]):([0-5]\d)$/.test(value);
  6. };
  7.  
  8. module.exports = {
  9.     createMeetingValidator: [
  10.         body('interviewType').notEmpty().optional({ nullable: true }),
  11.         body('duration').notEmpty().optional({ nullable: true }),
  12.         body('date').isDate().optional({ nullable: true }),
  13.         body('times').isArray().optional({ nullable: true }).withMessage('Times must be an array').custom((value, { req }) => {
  14.             for (let time of value) {
  15.                 if (!isTimeFormat(time)) return false;
  16.             }
  17.  
  18.             return true;
  19.         }).optional({ nullable: true }).withMessage('Invalid time format'),
  20.  
  21.         body('employmentType').notEmpty().optional({ nullable: true }),
  22.         body('startDate').isDate().optional({ nullable: true }),
  23.         body('payRate').isFloat().optional({ nullable: true }),
  24.         body('payPeriod').notEmpty().optional({ nullable: true }),
  25.         body('payFrequency').notEmpty().optional({ nullable: true }),
  26.         body('recruterName').notEmpty().optional({ nullable: true }),
  27.         body('offerLetter').notEmpty().optional({ nullable: true }),
  28.         body('interviewerId').notEmpty().optional({ nullable: true }).custom(async (value, { req }) => {
  29.             if (value === null) return true;
  30.  
  31.             const user = (req?.jobApplication?.job?.company?.users || []).find(u => u.userId === req.user.id);
  32.             const isRoleValid = user && user.role === roles.admin.name;
  33.             if (!isRoleValid || req.user.id === parseInt(value)) return false;
  34.  
  35.             // добавляем пользователя с тимы
  36.             const teamUser = await TeamCompanyAggregate.findOne({
  37.                 where: { userId: value, companyId: user?.companyId }
  38.             });
  39.  
  40.             return !!teamUser;
  41.         }).withMessage('Not have access'),
  42.  
  43.         (req, res, next) => {
  44.             const errors = validationResult(req).errors.map(error => ({ msg: error.msg, field: error.path }));
  45.             if (!errors.length) return next();
  46.  
  47.             return res.json({ success: false, message: 'Some fields invalid', errors });
  48.         }
  49.     ]
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement