|
| 1 | +import { Request, Response } from 'express'; |
| 2 | +import Joi from 'joi'; |
| 3 | +import { AccountWebPushDeviceService } from 'podverse-orm'; |
| 4 | +import { handleGenericErrorResponse } from '@api/controllers/helpers/error'; |
| 5 | +import { validateBodyObject } from '@api/lib/validation'; |
| 6 | +import { ensureAuthenticated } from '@api/lib/auth'; |
| 7 | + |
| 8 | +const createAccountWebPushDeviceSchema = Joi.object({ |
| 9 | + endpoint: Joi.string().uri().required(), |
| 10 | + p256dh: Joi.string().required(), |
| 11 | + auth: Joi.string().required() |
| 12 | +}); |
| 13 | + |
| 14 | +const updateAccountWebPushDeviceSchema = Joi.object({ |
| 15 | + endpoint: Joi.string().uri().required(), |
| 16 | + p256dh: Joi.string().required(), |
| 17 | + auth: Joi.string().required() |
| 18 | +}); |
| 19 | + |
| 20 | +const deleteAccountWebPushDeviceSchema = Joi.object({ |
| 21 | + endpoint: Joi.string().uri().required() |
| 22 | +}); |
| 23 | + |
| 24 | +const updateLocaleForAccountSchema = Joi.object({ |
| 25 | + locale: Joi.string().required() |
| 26 | +}); |
| 27 | + |
| 28 | +export class AccountWebPushDeviceController { |
| 29 | + private static accountWebPushDeviceService = new AccountWebPushDeviceService(); |
| 30 | + |
| 31 | + static async create(req: Request, res: Response): Promise<void> { |
| 32 | + ensureAuthenticated(req, res, async () => { |
| 33 | + validateBodyObject(createAccountWebPushDeviceSchema, req, res, async () => { |
| 34 | + try { |
| 35 | + const jwtUser = req.user!; |
| 36 | + const { endpoint, p256dh, auth } = req.body as { |
| 37 | + endpoint: string; |
| 38 | + p256dh: string; |
| 39 | + auth: string; |
| 40 | + }; |
| 41 | + const accountWebPushDevice = await AccountWebPushDeviceController |
| 42 | + .accountWebPushDeviceService.create(jwtUser.id, { |
| 43 | + endpoint, |
| 44 | + p256dh, |
| 45 | + auth |
| 46 | + }); |
| 47 | + res.json(accountWebPushDevice); |
| 48 | + } catch (error) { |
| 49 | + handleGenericErrorResponse(res, error); |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + static async update(req: Request, res: Response): Promise<void> { |
| 56 | + ensureAuthenticated(req, res, async () => { |
| 57 | + validateBodyObject(updateAccountWebPushDeviceSchema, req, res, async () => { |
| 58 | + try { |
| 59 | + const jwtUser = req.user!; |
| 60 | + const { endpoint, p256dh, auth } = req.body as { |
| 61 | + endpoint: string; |
| 62 | + p256dh: string; |
| 63 | + auth: string; |
| 64 | + }; |
| 65 | + const accountWebPushDevice = await AccountWebPushDeviceController |
| 66 | + .accountWebPushDeviceService.update(jwtUser.id, { |
| 67 | + endpoint, |
| 68 | + p256dh, |
| 69 | + auth |
| 70 | + }); |
| 71 | + res.json(accountWebPushDevice); |
| 72 | + } catch (error) { |
| 73 | + handleGenericErrorResponse(res, error); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + static async delete(req: Request, res: Response): Promise<void> { |
| 80 | + ensureAuthenticated(req, res, async () => { |
| 81 | + validateBodyObject(deleteAccountWebPushDeviceSchema, req, res, async () => { |
| 82 | + try { |
| 83 | + const jwtUser = req.user!; |
| 84 | + const { endpoint } = req.body as { |
| 85 | + endpoint: string; |
| 86 | + }; |
| 87 | + await AccountWebPushDeviceController |
| 88 | + .accountWebPushDeviceService.delete(jwtUser.id, { |
| 89 | + endpoint |
| 90 | + }); |
| 91 | + res.json({ message: 'WebPush device deleted successfully' }); |
| 92 | + } catch (error) { |
| 93 | + handleGenericErrorResponse(res, error); |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + static async getAllForAccount(req: Request, res: Response): Promise<void> { |
| 100 | + ensureAuthenticated(req, res, async () => { |
| 101 | + try { |
| 102 | + const jwtUser = req.user!; |
| 103 | + const devices = await AccountWebPushDeviceController.accountWebPushDeviceService.getAllForAccount(jwtUser.id); |
| 104 | + res.json(devices); |
| 105 | + } catch (error) { |
| 106 | + handleGenericErrorResponse(res, error); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + static async updateLocaleForAccount(req: Request, res: Response): Promise<void> { |
| 112 | + ensureAuthenticated(req, res, async () => { |
| 113 | + validateBodyObject(updateLocaleForAccountSchema, req, res, async () => { |
| 114 | + try { |
| 115 | + const jwtUser = req.user!; |
| 116 | + const { locale } = req.body as { locale: string }; |
| 117 | + await AccountWebPushDeviceController.accountWebPushDeviceService.updateLocaleForAccount(jwtUser.id, { locale }); |
| 118 | + res.json({ message: 'Locale updated for account devices' }); |
| 119 | + } catch (error) { |
| 120 | + handleGenericErrorResponse(res, error); |
| 121 | + } |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
0 commit comments