Files
ostandari/server/controllers/contactUsController.js
T
2024-10-21 10:22:26 +03:30

140 lines
3.5 KiB
JavaScript

const ContactUs = require('../models/ContactUs');
const {body, validationResult , checkSchema} = require('express-validator');
const {_sr} = require('../plugins/serverResponses');
const {res404, res500, hasWhiteSpaces, isLatinCharactersWithSymbol, generateRandomDigits} = require('../plugins/controllersHelperFunctions');
/////////////////////////////////////////////////////////////////////// admin
module.exports.updateContacUs = [
checkSchema({
description : {
notEmpty : true,
errorMessage : _sr['fa'].required.description
},
email : {
isEmail : true,
errorMessage : _sr['fa'].format.email,
},
phone : {
isNumeric : true,
errorMessage : `شماره تلفن ${_sr['fa'].optional.number}`
},
address : {
isString : true,
errorMessage : `آدرس ${_sr['fa'].optional.string}`
},
'location.type' : {
isString : true,
},
'location.coordinates' : {
isArray : true
}
}),
async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({
validation: errors.mapped()
});
try {
/**
* get data from input
*/
var {
description,
email,
phone,
address,
location,
} = req.body;
/**
* get id
*/
// var contactUsId = req.params.id;
var contactUs = await ContactUs.findOne();
if(!contactUs){
return res404(res , `صفحه ارتباط با ما وجود ندارد`);
}
/**
* make update object
*/
contactUs.title = contactUs.title.trim();
contactUs.description = description.trim();
contactUs.email = email.trim();
contactUs.phone = phone.trim();
contactUs.address = address.trim();
contactUs.location = {
type : location.type,
coordinates : location.coordinates
};
contactUs.main = contactUs.main;
contactUs.showInMenu = contactUs.showInMenu;
contactUs._creator = req.user?._id || null;
contactUs._updatedBy = req.user?._id || null;
contactUs.siteMap = contactUs.siteMap;
contactUs.showInFooter = contactUs.showInFooter;
await contactUs.save();
/**
* show result
*/
return res.json(contactUs);
} catch (error) {
return res500(res , error.message);
}
}
]
module.exports.getOneContacUs = [
async (req, res) => {
try {
// var add = new ContactUs({
// description: "توضیحات صفحه تماس با ما",
// showInMenu: true,
// main: true,
// email: 'test@gmail.com',
// phone: '08632222222',
// address: 'میدان فاطمیه',
// _creator: null,
// _updatedBy: null,
// location: {
// type: 'Point',
// coordinates: [-118.2870407961309, 33.93508571994455]
// },
// siteMap: true,
// showInFooter: true
// });
// await add.save();
/**
* get ContacUs
*/
const getContacUs = await ContactUs.find({});
/**
* if not exist
*/
if(!getContacUs.length){
return res404(res , `صفحه ارتباط با ما وجود ندارد`);
}
/**
* show result
*/
return res.json(getContacUs[0]);
} catch (error) {
/**
* if we got unkown error show to client
*/
res500(res , error);
}
}
]