full app
This commit is contained in:
91
controllers/application/autoDirect/autoDirectController.js
Normal file
91
controllers/application/autoDirect/autoDirectController.js
Normal file
@@ -0,0 +1,91 @@
|
||||
const AutoDirectRuleModel = require('../../../models/AutoDirectRuleModel')
|
||||
|
||||
function ruleError (res, status, message) {
|
||||
return res.status(status).json({ error: true, message })
|
||||
}
|
||||
|
||||
async function findOwnedRule (ruleId, userId) {
|
||||
const rule = await AutoDirectRuleModel.findById(ruleId)
|
||||
if (!rule) return { error: 'NOT_FOUND' }
|
||||
if (String(rule.user_id) !== String(userId)) return { error: 'FORBIDDEN' }
|
||||
return { rule }
|
||||
}
|
||||
|
||||
const getMyAutoDirectRules = async (req, res, next) => {
|
||||
try {
|
||||
const rules = await AutoDirectRuleModel.find({ user_id: req.user._id }).sort({ createdAt: -1 })
|
||||
return res.json({ rules })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const createAutoDirectRule = async (req, res, next) => {
|
||||
try {
|
||||
const { trigger_text, direct_message, reply_caption } = req.body
|
||||
if (!trigger_text?.trim() || !direct_message?.trim() || !reply_caption?.trim()) {
|
||||
return ruleError(res, 422, 'همهی فیلدها الزامی است')
|
||||
}
|
||||
|
||||
const rule = await AutoDirectRuleModel.create({
|
||||
user_id: req.user._id,
|
||||
trigger_text: trigger_text.trim(),
|
||||
direct_message: direct_message.trim(),
|
||||
reply_caption: reply_caption.trim()
|
||||
})
|
||||
|
||||
return res.status(201).json({ message: 'دایرکت اتوماتیک ثبت شد', rule })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const updateAutoDirectRule = async (req, res, next) => {
|
||||
try {
|
||||
const { rule, error } = await findOwnedRule(req.params.id, req.user._id)
|
||||
if (error === 'NOT_FOUND') return ruleError(res, 404, 'دایرکت اتوماتیک یافت نشد')
|
||||
if (error === 'FORBIDDEN') return ruleError(res, 403, 'شما مالک این قانون نیستید')
|
||||
|
||||
const { trigger_text, direct_message, reply_caption, is_active } = req.body
|
||||
if (trigger_text !== undefined) {
|
||||
if (!trigger_text?.trim()) return ruleError(res, 422, 'متن کامنت الزامی است')
|
||||
rule.trigger_text = trigger_text.trim()
|
||||
}
|
||||
if (direct_message !== undefined) {
|
||||
if (!direct_message?.trim()) return ruleError(res, 422, 'متن دایرکت الزامی است')
|
||||
rule.direct_message = direct_message.trim()
|
||||
}
|
||||
if (reply_caption !== undefined) {
|
||||
if (!reply_caption?.trim()) return ruleError(res, 422, 'متن پاسخ کامنت الزامی است')
|
||||
rule.reply_caption = reply_caption.trim()
|
||||
}
|
||||
if (is_active !== undefined) {
|
||||
rule.is_active = Boolean(is_active)
|
||||
}
|
||||
|
||||
await rule.save()
|
||||
return res.json({ message: 'دایرکت اتوماتیک بهروزرسانی شد', rule })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const deleteAutoDirectRule = async (req, res, next) => {
|
||||
try {
|
||||
const { rule, error } = await findOwnedRule(req.params.id, req.user._id)
|
||||
if (error === 'NOT_FOUND') return ruleError(res, 404, 'دایرکت اتوماتیک یافت نشد')
|
||||
if (error === 'FORBIDDEN') return ruleError(res, 403, 'شما مالک این قانون نیستید')
|
||||
|
||||
await rule.deleteOne()
|
||||
return res.json({ message: 'دایرکت اتوماتیک حذف شد' })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMyAutoDirectRules,
|
||||
createAutoDirectRule,
|
||||
updateAutoDirectRule,
|
||||
deleteAutoDirectRule
|
||||
}
|
||||
Reference in New Issue
Block a user