feat: add ci cd files dont edit those files
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
const YearBanner = require('../models/YearBanner');
|
||||
const {body, validationResult, checkSchema} = require('express-validator');
|
||||
const {_sr} = require('../plugins/serverResponses');
|
||||
const {res404, res500} = require('../plugins/controllersHelperFunctions');
|
||||
const fs = require('fs');
|
||||
const sharp = require('sharp');
|
||||
|
||||
////// variables
|
||||
const yearBannerWidth = 1920
|
||||
const yearBannerHeight = 160
|
||||
const yearBannerQuality = 100
|
||||
|
||||
|
||||
module.exports.updateYearBanner = [
|
||||
checkSchema({
|
||||
iranFlag: {
|
||||
optional: true,
|
||||
isBoolean: true,
|
||||
errorMessage: _sr['fa'].format.boolean
|
||||
}
|
||||
}),
|
||||
async (req, res) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()});
|
||||
|
||||
try {
|
||||
/**
|
||||
* get data from input
|
||||
*/
|
||||
const {
|
||||
iranFlag,
|
||||
electionBtn,
|
||||
hasEmail,
|
||||
hasGPlus,
|
||||
hasTweeter,
|
||||
hasInstagram,
|
||||
email,
|
||||
gPlus,
|
||||
tweeter,
|
||||
instagram
|
||||
} = req.body
|
||||
|
||||
const yearBanner = await YearBanner.findOne();
|
||||
|
||||
let imageName
|
||||
if (req.files?.mainCover) {
|
||||
const image = req.files.mainCover;
|
||||
if (!_sr.supportedImageFormats.includes(image.mimetype.split('/')[1])) {
|
||||
return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}});
|
||||
}
|
||||
|
||||
imageName = `mainPage_cover.jpg`;
|
||||
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(yearBannerWidth, yearBannerHeight, {fit: 'cover'})
|
||||
.toFormat('jpg')
|
||||
.jpeg({quality: yearBannerQuality})
|
||||
.toFile(`./static/uploads/images/mainCover/${imageName}`);
|
||||
}
|
||||
|
||||
if (yearBanner) {
|
||||
yearBanner.iranFlag = iranFlag;
|
||||
yearBanner.electionBtn = electionBtn;
|
||||
// footerOptions
|
||||
yearBanner.hasEmail = hasEmail;
|
||||
yearBanner.hasGPlus = hasGPlus;
|
||||
yearBanner.hasTweeter = hasTweeter;
|
||||
yearBanner.hasInstagram = hasInstagram;
|
||||
yearBanner.email = email;
|
||||
yearBanner.gPlus = gPlus;
|
||||
yearBanner.tweeter = tweeter;
|
||||
yearBanner.instagram = instagram;
|
||||
|
||||
if (req.files?.mainCover) yearBanner.mainCover = imageName;
|
||||
yearBanner._updatedBy = req.user._id;
|
||||
|
||||
await yearBanner.save();
|
||||
|
||||
return res.json({message: _sr['fa'].response.success_save});
|
||||
} else {
|
||||
const data = {
|
||||
iranFlag,
|
||||
electionBtn,
|
||||
hasEmail,
|
||||
hasGPlus,
|
||||
hasTweeter,
|
||||
hasInstagram,
|
||||
email,
|
||||
gPlus,
|
||||
tweeter,
|
||||
instagram,
|
||||
_updatedBy: req.user._id
|
||||
}
|
||||
if (req.files?.mainCover) data.mainCover = imageName;
|
||||
await YearBanner.create(data);
|
||||
return res.json({message: _sr['fa'].response.success_save});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
return res500(res, `مشکلی در اجرای درخواست پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getYearBanner = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
/**
|
||||
* get getYearBanner
|
||||
*/
|
||||
const getYearBanner = await YearBanner.findOne();
|
||||
|
||||
/**
|
||||
* if not exist
|
||||
*/
|
||||
if (!getYearBanner) return res.json({
|
||||
mainCover: '',
|
||||
iranFlag: true,
|
||||
electionBtn: true,
|
||||
hasEmail: true,
|
||||
hasGPlus: true,
|
||||
hasTweeter: true,
|
||||
hasInstagram: true,
|
||||
email: '',
|
||||
gPlus: '',
|
||||
tweeter: '',
|
||||
instagram: ''
|
||||
});
|
||||
|
||||
/**
|
||||
* show result
|
||||
*/
|
||||
return res.json(getYearBanner);
|
||||
} catch (error) {
|
||||
/**
|
||||
* if we got unkown error show to client
|
||||
*/
|
||||
return res500(res, `مشکلی در اجرای درخواست شما پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user