const { body, validationResult } = require('express-validator') const Complaint = require('../models/Complaint') const Transaction = require('../models/Transaction') const { _sr } = require('../plugins/serverResponses') const { checkValidations } = require('../plugins/controllersHelperFunctions') const _faSr = _sr.fa module.exports.create = [ [ body('fullName').notEmpty().withMessage(_faSr.required.name), body('national').notEmpty().withMessage(_faSr.required.national_code), body('phoneNumber').notEmpty().isMobilePhone('ir-IR').withMessage(_faSr.required.phone_number), body('email').notEmpty().isEmail().withMessage(_faSr.required.email), body('receptionCode').notEmpty().withMessage(_faSr.required.field).custom(async(value, { req }) => { const check = await Transaction.findOne({ItemReceptionTransNumber:value}) if(!check){ return Promise.reject(new Error("کد پذیرش معتبر نیست")) }else{ return Promise.resolve() } }), body('caption').notEmpty().withMessage(_faSr.required.caption), body('callType').notEmpty().isBoolean().withMessage(_faSr.required.field), body('brand').notEmpty().isBoolean().withMessage(_faSr.required.field), ], checkValidations(validationResult), async(req, res) => { const { fullName, national, phoneNumber, deviceNumber, email, receptionCode, caption, callType, brand } = req.body const complaint = await Complaint.create({ fullName, national, type:0, phoneNumber, deviceNumber, callType, email, receptionCode, caption, brand }) res.status(201).json(complaint) } ] module.exports.createSurvey = [ [ body('fullName').notEmpty().withMessage(_faSr.required.name), body('national').notEmpty().withMessage(_faSr.required.national_code), body('phoneNumber').notEmpty().isMobilePhone().withMessage(_faSr.required.phone_number), body('email').notEmpty().isEmail().withMessage(_faSr.required.email), body('caption').notEmpty().withMessage(_faSr.required.caption), ], checkValidations(validationResult), async(req, res) => { const { fullName, national, phoneNumber, email, receptionCode, caption } = req.body const complaint = await Complaint.create({ fullName, national, type:1, phoneNumber, email, receptionCode, caption }) res.status(201).json(complaint) } ] module.exports.getsComplaint = [ async(req, res)=>{ const complaints = await Complaint.find({type:0}).populate('user', 'first_name last_name personalCode') res.status(200).json(complaints) } ] module.exports.get = [ async(req, res)=>{ const complaint = await Complaint.findByIdAndUpdate( req.params.id, {read: true} ).populate('user', 'first_name last_name personalCode') res.status(200).json(complaint) } ] module.exports.put = [ async(req, res)=>{ const { complete, desc } = req.body const complaint = await Complaint.findByIdAndUpdate( req.params.id, { complete, user:req.user_id, desc }, {new:true} ) res.status(200).json(complaint) } ]