Files
yourpart3/backend/controllers/pushNotificationController.js
Torsten Schulz (local) 1ab9407e79
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
2026-07-21 09:08:15 +02:00

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;