35 lines
1.6 KiB
JavaScript
35 lines
1.6 KiB
JavaScript
import Joi from 'joi';
|
|
import * as pushNotificationService from '../services/pushNotificationService.js';
|
|
|
|
const tokenSchema = Joi.object({ token: Joi.string().trim().min(20).max(4096).required(), enabled: Joi.boolean().default(true) });
|
|
const settingsSchema = Joi.object({
|
|
enabled: Joi.boolean(),
|
|
chatEnabled: Joi.boolean(),
|
|
friendLoginEnabled: Joi.boolean(),
|
|
falukantEnabled: Joi.boolean(),
|
|
vocabReminderEnabled: Joi.boolean(),
|
|
}).min(1);
|
|
|
|
const pushNotificationController = {
|
|
async registerDevice(req, res) {
|
|
const { error, value } = tokenSchema.validate(req.body || {});
|
|
if (error) return res.status(400).json({ error: error.details[0].message });
|
|
return res.status(200).json(await pushNotificationService.registerDevice(req.headers.userid, value));
|
|
},
|
|
async disableDevice(req, res) {
|
|
const { error, value } = tokenSchema.validate({ token: req.params.token, enabled: false });
|
|
if (error) return res.status(400).json({ error: error.details[0].message });
|
|
return res.status(200).json(await pushNotificationService.disableDevice(req.headers.userid, value.token));
|
|
},
|
|
async getSettings(req, res) {
|
|
return res.status(200).json(await pushNotificationService.loadSettings(req.headers.userid));
|
|
},
|
|
async updateSettings(req, res) {
|
|
const { error, value } = settingsSchema.validate(req.body || {});
|
|
if (error) return res.status(400).json({ error: error.details[0].message });
|
|
return res.status(200).json(await pushNotificationService.updateSettings(req.headers.userid, value));
|
|
},
|
|
};
|
|
|
|
export default pushNotificationController;
|