Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
This commit is contained in:
34
backend/controllers/pushNotificationController.js
Normal file
34
backend/controllers/pushNotificationController.js
Normal file
@@ -0,0 +1,34 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user