Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
4.4 kB
2
Indexable
Never
const Joi = require('joi');
const catchAsync = require('../utils/catchAsync');
const AppError = require('../utils/appError');


const schema = Joi.object({
    firstName: Joi.string()
        .alphanum()
        .min(3)
        .max(30),
    lastName: Joi.string()
        .alphanum()
        .min(3)
        .max(30),
    email: Joi.string()
        .email(),
    phone: Joi.string().min(10).max(13),
     //  .pattern(/[6-9]{1}[0-9]{9}/),
    gender: Joi.string()
       .valid('Male', 'Female'),
    address: {
        city: Joi.string().min(3).max(50),
        address: Joi.string().min(3).max(60),
        zipCode: Joi.string().max(7),
    }
})


const signupShema = Joi.object({
    name: Joi.string()
        .min(3)
        .max(30).required(),
    email: Joi.string()
        .email(),
    phone: Joi.string().min(10).max(13),
      // .pattern(/[6-9]{1}[0-9]{9}/).required(),
    password: Joi.string().min(8),
    passwordConfirm: Joi.ref('password'),
    gender: Joi.string()
       .valid('Male', 'Female'),
    role: Joi.string()
    .valid('client', 'retailer').required(),
})


const updatePasswordShema = Joi.object({
    currPassword: Joi.string().required(),
    newPassword: Joi.string().min(8).required(),
    newPasswordConfirm: Joi.any().valid(Joi.ref('newPassword')).required(),

})

const RetailerShema = Joi.object({
    company_name: Joi.string().alphanum().min(3).max(50).required(),
    province: Joi.string().alphanum().min(3).max(50).required(),
    ice: Joi.string().alphanum().min(3).max(50).required(),
    id_fiscal: Joi.string().alphanum().min(3).max(50).required(),
    register_commerce: Joi.string().alphanum().min(3).max(50).required(),
    taxe: Joi.string().alphanum().min(3).max(50).required(),
    cnss: Joi.string().alphanum().min(3).max(50).required(),
    rib: Joi.string().alphanum().min(3).max(50).required(),

})


const RetailerCreateShema = Joi.object({
    name: Joi.string()
        .min(3)
        .max(30).required(),
    email: Joi.string()
        .email().required(),
    image: Joi.string(),
    phone: Joi.string().min(10).max(13).required(),
      // .pattern(/[6-9]{1}[0-9]{9}/).required(),
    password: Joi.string().min(8).required(),
    passwordConfirm: Joi.ref('password'),
    address: {
        city: Joi.string().min(3).max(50),
        address: Joi.string().min(3).max(60),
        zipCode: Joi.string().max(7),
    },
    company_name: Joi.string().min(3).max(50).required(),
    province: Joi.string().alphanum().min(3).max(50).required(),
    ice: Joi.string().alphanum().min(3).max(50).required(),
    id_fiscal: Joi.string().alphanum().min(3).max(50).required(),
    register_commerce: Joi.string().alphanum().min(3).max(50).required(),
    taxe: Joi.string().alphanum().min(3).max(50).required(),
    cnss: Joi.string().alphanum().min(3).max(50).required(),
    rib: Joi.string().alphanum().min(3).max(50).required(),
    pricingType : Joi.number().min(1).max(3).required(),
    request_retailer_id: Joi.string()
})

exports.validateRequest = catchAsync(async (req, res, next) => {

        const { error, value } = schema.validate(req.body);
        if(error){
            return next(new AppError(error.details[0].message, 401));
        }
        next()
   
});

exports.validateRequestCreateRetailer = catchAsync(async (req, res, next) => {

    const { error, value } = RetailerCreateShema.validate(req.body);
    if(error){
        return next(new AppError(error.details[0].message, 401));
    }

    next()

});


exports.validateSignup = catchAsync(async (req, res, next) => {
    const { error, value } = signupShema.validate(req.body,{allowUnknown : true});
    if(error){
        return next(new AppError(error.details[0].message, 401));
    }
    if(value.role == "retailer"){
        const { error, value } = RetailerShema.validate(req.body,{allowUnknown : true});
        if(error){
            return next(new AppError(error.details[0].message, 401));
        } 
    }
    next()
});


exports.validateUpdatePassword = catchAsync(async (req, res, next) => {
    const { error, value } = updatePasswordShema.validate(req.body);
    if(error){
        return next(new AppError(error.details[0].message, 401));
    }
    next()
});
Leave a Comment