This commit is contained in:
Swift
2023-09-10 12:50:45 +03:30
2 changed files with 22 additions and 1 deletions
+17 -1
View File
@@ -5,6 +5,7 @@ const { res404, res500, checkValidations } = require('../plugins/controllersHelp
const { notifyAllUsers } = require('../WebSocket/controllers/user_EventsController') const { notifyAllUsers } = require('../WebSocket/controllers/user_EventsController')
const { notifyAllAgents } = require('../WebSocket/controllers/agent_EventsController') const { notifyAllAgents } = require('../WebSocket/controllers/agent_EventsController')
const _faSr = _sr.fa const _faSr = _sr.fa
const maxAttachmentSize = 2048
module.exports.create = [ module.exports.create = [
[ [
@@ -31,10 +32,25 @@ module.exports.create = [
.withMessage(_faSr.format.boolean) .withMessage(_faSr.format.boolean)
], ],
checkValidations(validationResult), checkValidations(validationResult),
(req, res) => { async (req, res) => {
const { title, caption, expireDate, isForAgents } = req.body const { title, caption, expireDate, isForAgents } = req.body
const { user_id } = req const { user_id } = req
const data = { title, caption, expireDate, isForAgents, _creator: user_id } const data = { title, caption, expireDate, isForAgents, _creator: user_id }
if (req.files?.image) {
const image = req.files.image
const fileName = 'TicketAttachment_' + Date.now() + '.' + image.mimetype.split('/')[1]
const acceptableFormats = ['png', 'jpg', 'jpeg', 'webp']
if (!acceptableFormats.includes(image.mimetype.split('/')[1]))
return res.status(422).json({ validation: { image: { msg: _faSr.format.image } } })
if (image.size / 1024 > maxAttachmentSize)
return res
.status(422)
.json({ validation: { image: { msg: `حجم تصویر نباید بیشتر از ${maxAttachmentSize} KB باشد.` } } })
data.image = fileName
await image.mv(`./static/uploads/images/announcement/${fileName}`)
}
const announcement = new Announcement(data) const announcement = new Announcement(data)
announcement.save(err => { announcement.save(err => {
if (err) return res500(res, err) if (err) return res500(res, err)
+5
View File
@@ -1,5 +1,9 @@
const mongoose = require('mongoose') const mongoose = require('mongoose')
function image(img) {
if (img) return '/uploads/images/announcement/' + img
}
const AnnouncementSchema = mongoose.Schema({ const AnnouncementSchema = mongoose.Schema({
title: String, title: String,
caption: String, caption: String,
@@ -7,6 +11,7 @@ const AnnouncementSchema = mongoose.Schema({
expireDate: Number, expireDate: Number,
expired: { type: Boolean, default: false }, expired: { type: Boolean, default: false },
isForAgents: { type: Boolean, default: false }, isForAgents: { type: Boolean, default: false },
image: { type: String, get: image },
_creator: { type: mongoose.ObjectId, ref: 'User' } _creator: { type: mongoose.ObjectId, ref: 'User' }
}) })