CoreID/app/controllers/api/v1/System.controller.js

110 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2020-08-13 02:27:51 +00:00
const { Controller } = require('libflitter')
class ReflectController extends Controller {
static get services() {
return [...super.services, 'routers', 'models', 'activity', 'Vue']
2020-08-13 02:27:51 +00:00
}
async get_announcements(req, res, next) {
const Announcement = this.models.get('system:Announcement')
const announcements = await Announcement.find()
const data = []
for ( const announcement of announcements ) {
data.push(await announcement.to_api())
}
return res.api(data)
}
async get_announcement(req, res, next) {
const Announcement = this.models.get('system:Announcement')
const announcement = await Announcement.findById(req.params.id)
if ( !announcement )
return res.status(404)
.message(req.T('api.announcement_not_found'))
.api()
return res.api(await announcement.to_api())
}
async create_announcement(req, res, next) {
const Announcement = this.models.get('system:Announcement')
const required_fields = ['title', 'message', 'user_ids', 'group_ids', 'type']
for ( const field of required_fields ) {
if ( !req.body[field] )
return res.status(400)
.message(`${req.T('api.missing_field')} ${field}`)
.api()
}
if ( !Array.isArray(req.body.user_ids) )
return res.status(400)
.message(`${req.T('api.improper_field')} user_ids`)
.api()
if ( !Array.isArray(req.body.group_ids) )
return res.status(400)
.message(`${req.T('api.improper_field')} group_ids`)
.api()
if ( !['email', 'login', 'banner'].includes(req.body.type) )
return res.status(400)
.message(`${req.T('api.improper_field')} type`)
.api()
const announcement = new Announcement({
title: req.body.title,
message: req.body.message,
user_ids: req.body.user_ids,
group_ids: req.body.group_ids,
type: req.body.type,
})
await announcement.save()
2020-08-13 02:49:02 +00:00
await announcement.log_populate()
2020-08-13 02:27:51 +00:00
return res.api(await announcement.to_api())
}
async delete_announcement(req, res, next) {
const Announcement = this.models.get('system:Announcement')
const announcement = await Announcement.findById(req.params.id)
if ( !announcement )
return res.status(404)
.message(req.T('api.announcement_not_found'))
.api()
await announcement.delete()
return res.api()
}
async show_login_message(req, res) {
const LoginMessage = this.models.get('LoginMessage')
const messages = await LoginMessage.for_user(req.user)
const message = messages[0]
return this.Vue.auth_message(res, {
message: `<h3>${message.title}</h3>${message.message}`,
next_destination: '/auth/login-message/dismiss',
})
}
async dismiss_login_message(req, res) {
const LoginMessage = this.models.get('LoginMessage')
const messages = await LoginMessage.for_user(req.user)
const message = messages[0]
await message.mark_seen()
if ( req.trap.has_trap('login_message') )
await req.trap.end()
return res.redirect(req.session?.auth?.flow || '/dash')
}
2020-08-13 02:27:51 +00:00
}
module.exports = exports = ReflectController