Merge branch 'main' of https://github.com/Danakcorp/asan-service
This commit is contained in:
@@ -5,6 +5,7 @@ const { res404, res500, checkValidations } = require('../plugins/controllersHelp
|
||||
const { notifyAllUsers } = require('../WebSocket/controllers/user_EventsController')
|
||||
const { notifyAllAgents } = require('../WebSocket/controllers/agent_EventsController')
|
||||
const _faSr = _sr.fa
|
||||
const maxAttachmentSize = 2048
|
||||
|
||||
module.exports.create = [
|
||||
[
|
||||
@@ -31,10 +32,25 @@ module.exports.create = [
|
||||
.withMessage(_faSr.format.boolean)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
(req, res) => {
|
||||
async (req, res) => {
|
||||
const { title, caption, expireDate, isForAgents } = req.body
|
||||
const { user_id } = req
|
||||
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)
|
||||
announcement.save(err => {
|
||||
if (err) return res500(res, err)
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
function image(img) {
|
||||
if (img) return '/uploads/images/announcement/' + img
|
||||
}
|
||||
|
||||
const AnnouncementSchema = mongoose.Schema({
|
||||
title: String,
|
||||
caption: String,
|
||||
@@ -7,6 +11,7 @@ const AnnouncementSchema = mongoose.Schema({
|
||||
expireDate: Number,
|
||||
expired: { type: Boolean, default: false },
|
||||
isForAgents: { type: Boolean, default: false },
|
||||
image: { type: String, get: image },
|
||||
_creator: { type: mongoose.ObjectId, ref: 'User' }
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user