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