feat: add ci cd files dont edit those files
This commit is contained in:
@@ -0,0 +1,341 @@
|
||||
const Occasion = require('../models/Occasion');
|
||||
const {body, validationResult} = require('express-validator');
|
||||
const {_sr} = require('../plugins/serverResponses');
|
||||
const {res404, res500} = require('../plugins/controllersHelperFunctions');
|
||||
const fs = require('fs');
|
||||
const sharp = require('sharp');
|
||||
const moment = require('moment-jalaali');
|
||||
|
||||
////// variables
|
||||
const occasionWidth = 1920
|
||||
const occasionHeight = 180
|
||||
const occasionQuality = 100
|
||||
|
||||
const limit = 5;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////// admin
|
||||
module.exports.addOccasion = [
|
||||
[
|
||||
body('title')
|
||||
.notEmpty().withMessage(_sr['fa'].required.title),
|
||||
|
||||
body('publish')
|
||||
.optional().isBoolean().withMessage(_sr['fa'].format.boolean),
|
||||
|
||||
body('index')
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('startDate')
|
||||
.notEmpty().withMessage(`لطفا یک تاریخ شروع تعیین کنید`),
|
||||
|
||||
|
||||
body('endDate')
|
||||
.notEmpty().withMessage(`لطفا یک تاریخ پایان تعیین کنید`),
|
||||
],
|
||||
async (req, res) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({
|
||||
validation: errors.mapped()
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
const {title, publish, index, startDate, endDate} = req.body;
|
||||
|
||||
const occasionData = {
|
||||
title,
|
||||
publish,
|
||||
index,
|
||||
startDate,
|
||||
endDate,
|
||||
_creator: req.user._id,
|
||||
_updatedBy: req.user._id,
|
||||
}
|
||||
|
||||
const addOccasion = await Occasion.create(occasionData);
|
||||
|
||||
if (!addOccasion) return res500(res, `مشلی در اجرای درخواست شما پیش امده است`);
|
||||
|
||||
if (req?.files && req.files?.cover) {
|
||||
const image = req.files.cover;
|
||||
if (!_sr.supportedImageFormats.includes(image.mimetype.split('/')[1])) {
|
||||
return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}});
|
||||
}
|
||||
|
||||
const imageName = `occasion_${addOccasion._id}_${Date.now()}.jpg`;
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(occasionWidth, occasionHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.toFormat('jpg')
|
||||
.jpeg({
|
||||
quality: occasionQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/occasion/${imageName}`);
|
||||
|
||||
addOccasion.status = true;
|
||||
addOccasion.mainCover = imageName;
|
||||
await addOccasion.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* show result
|
||||
*/
|
||||
return res.json(addOccasion);
|
||||
} catch (error) {
|
||||
return res500(res, `مشکلی در اجرای درخواست پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.updateOccasion = [
|
||||
[
|
||||
body('title')
|
||||
.notEmpty().withMessage(_sr['fa'].required.title),
|
||||
|
||||
body('publish')
|
||||
.optional().isBoolean().withMessage(_sr['fa'].format.boolean),
|
||||
|
||||
body('index')
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('startDate')
|
||||
.notEmpty().withMessage(`لطفا یک تاریخ شروع تعیین کنید`),
|
||||
|
||||
body('endDate')
|
||||
.notEmpty().withMessage(`لطفا یک تاریخ پایان تعیین کنید`),
|
||||
],
|
||||
async (req, res) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({
|
||||
validation: errors.mapped()
|
||||
});
|
||||
|
||||
|
||||
try {
|
||||
|
||||
const {title, publish, index, startDate, endDate} = req.body;
|
||||
|
||||
const occasionId = req.params.id;
|
||||
|
||||
const targetOccasion = await Occasion.findById(occasionId);
|
||||
|
||||
if (!targetOccasion) {
|
||||
return res404(res, `مناسبت مورد نظر پیدا نشد`);
|
||||
}
|
||||
|
||||
if (req?.files && req.files?.cover) {
|
||||
const image = req.files.cover;
|
||||
if (!_sr.supportedImageFormats.includes(image.mimetype.split('/')[1])) {
|
||||
return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}});
|
||||
}
|
||||
|
||||
const imageName = `occasion_${targetOccasion._id}_${Date.now()}.jpg`;
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(occasionWidth, occasionHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.toFormat('jpg')
|
||||
.jpeg({
|
||||
quality: occasionQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/occasion/${imageName}`);
|
||||
|
||||
if (targetOccasion.mainCover) {
|
||||
var oldPic = `./static${targetOccasion.mainCover}`;
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
targetOccasion.status = true;
|
||||
targetOccasion.mainCover = imageName;
|
||||
}
|
||||
|
||||
targetOccasion.title = title;
|
||||
targetOccasion.publish = publish;
|
||||
targetOccasion.index = index;
|
||||
targetOccasion.startDate = startDate;
|
||||
targetOccasion.endDate = endDate;
|
||||
targetOccasion._updatedBy = req.user._id;
|
||||
|
||||
await targetOccasion.save();
|
||||
|
||||
return res.json(targetOccasion);
|
||||
} catch (error) {
|
||||
return res500(res, `مشکلی در اجرای درخواست پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.deleteOccasion = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
const occasionId = req.params.id;
|
||||
|
||||
const targetOccasion = await Occasion.findById(occasionId);
|
||||
|
||||
if (!targetOccasion) {
|
||||
return res404(res, `مناسبت مورد نظر پیدا نشد`);
|
||||
}
|
||||
|
||||
if (targetOccasion.mainCover) {
|
||||
var oldPic = `./static${targetOccasion.mainCover}`;
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await targetOccasion.deleteOne();
|
||||
|
||||
return res.json({message: `مناسبت مورد نظر حذف گرید`});
|
||||
} catch (error) {
|
||||
return res500(res, `مشکلی در اجرای درخواست پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getOccasionByAdmin = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
const occasionId = req.params.id;
|
||||
const getOccasion = await Occasion.findById(occasionId);
|
||||
|
||||
/**
|
||||
* if not exist
|
||||
*/
|
||||
if (!getOccasion) return res404(res, `اطلاعات مورد نظر پیدا نشد`);
|
||||
|
||||
/**
|
||||
* show result
|
||||
*/
|
||||
return res.json(getOccasion);
|
||||
} catch (error) {
|
||||
/**
|
||||
* if we got unkown error show to client
|
||||
*/
|
||||
return res500(res, `مشکلی در اجرای درخواست شما پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getAllOccasionByAdmin = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
const getAllOccasion = await Occasion.paginate({}, {
|
||||
page: req.query.page || 1,
|
||||
populate: {path: '_creator', select: 'firstName lastName'},
|
||||
limit,
|
||||
sort: {_id: -1}
|
||||
});
|
||||
|
||||
return res.json(getAllOccasion);
|
||||
} catch (error) {
|
||||
/**
|
||||
* if we got unkown error show to client
|
||||
*/
|
||||
return res500(res, `مشکلی در اجرای درخواست شما پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getAllOccasionForIndex = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
const getAllOccasion = await Occasion.find();
|
||||
|
||||
return res.json(getAllOccasion);
|
||||
} catch (error) {
|
||||
/**
|
||||
* if we got unkown error show to client
|
||||
*/
|
||||
return res500(res, `مشکلی در اجرای درخواست شما پیش آمده است`);
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.search = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
let query = {};
|
||||
let populate = [{
|
||||
path: '_creator',
|
||||
select: {firstName: 1, lastName: 1, profilePic: 1}
|
||||
}];
|
||||
|
||||
|
||||
query['$and'] = [];
|
||||
|
||||
const {
|
||||
term,
|
||||
publish,
|
||||
status,
|
||||
date
|
||||
} = req.body;
|
||||
|
||||
/** search by term */
|
||||
if (term && !_.isEmpty(term)) {
|
||||
query['$and'].push({
|
||||
$or: [{
|
||||
title: {
|
||||
$regex: '.*' + term + '.*'
|
||||
}
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
||||
/** search by publish */
|
||||
if (!_.isUndefined(publish) && _.isBoolean(publish)) {
|
||||
query['$and'].push({
|
||||
publish
|
||||
});
|
||||
}
|
||||
|
||||
/** search by status */
|
||||
if (!_.isUndefined(status) && _.isBoolean(status)) {
|
||||
query['$and'].push({
|
||||
status
|
||||
});
|
||||
}
|
||||
|
||||
/** search by date */
|
||||
if (date && !_.isEmpty(date)) {
|
||||
if (date.length === 1) {
|
||||
query['$and'].push({
|
||||
startDate:{$lte : new Date(date[0])},
|
||||
endDate: {$gte : new Date(date[0])}
|
||||
});
|
||||
} else {
|
||||
query['$and'].push({
|
||||
startDate: {$lte : new Date(date[0])},
|
||||
endDate:{$gte : new Date(date[1])}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_.isEmpty(query['$and']) && delete query['$and'];
|
||||
// _.isEmpty(input['$or']) && delete input['$or'];
|
||||
|
||||
/** get news */
|
||||
const occasion = await Occasion.paginate(query, {
|
||||
page: req.query.page || 1,
|
||||
limit,
|
||||
populate,
|
||||
sort: {_id: -1}
|
||||
});
|
||||
|
||||
/** show result */
|
||||
return res.json(occasion);
|
||||
} catch (error) {
|
||||
return res500(res, error?.message)
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user