|
| 1 | +"use strict"; |
| 2 | +/** |
| 3 | + * account.controller.ts |
| 4 | + */ |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.AccountController = void 0; |
| 7 | +const base_controller_1 = require("./base.controller"); |
| 8 | +const account_model_1 = require("../models/account.model"); |
| 9 | +class AccountController extends base_controller_1.BaseController { |
| 10 | + service; |
| 11 | + constructor(service) { |
| 12 | + super(); |
| 13 | + this.service = service; |
| 14 | + } |
| 15 | + register = async (req, res, next) => { |
| 16 | + try { |
| 17 | + const result = await this.service.register(req.body); |
| 18 | + this.sendResponse(res, 201 /* HttpStatus.CREATED */, 'Account created succesfully', result); |
| 19 | + } |
| 20 | + catch (error) { |
| 21 | + next(error); |
| 22 | + } |
| 23 | + }; |
| 24 | + login = async (req, res, next) => { |
| 25 | + try { |
| 26 | + const { email, password } = req.body; |
| 27 | + const result = await this.service.login(email, password); |
| 28 | + this.sendResponse(res, 200 /* HttpStatus.OK */, 'Login successful', result); |
| 29 | + } |
| 30 | + catch (error) { |
| 31 | + next(error); |
| 32 | + } |
| 33 | + }; |
| 34 | + getProfile = async (req, res, next) => { |
| 35 | + try { |
| 36 | + const userId = req.user.userId; |
| 37 | + const result = await this.service.getById(userId); |
| 38 | + this.sendResponse(res, 200 /* HttpStatus.OK */, 'Account retrieved successfully', result); |
| 39 | + } |
| 40 | + catch (error) { |
| 41 | + next(error); |
| 42 | + } |
| 43 | + }; |
| 44 | + getAccountById = async (req, res, next) => { |
| 45 | + try { |
| 46 | + const id = parseInt(req.params.id); |
| 47 | + const result = await this.service.getById(id); |
| 48 | + this.sendResponse(res, 200 /* HttpStatus.OK */, 'Account retrieved successfully', result); |
| 49 | + } |
| 50 | + catch (error) { |
| 51 | + next(error); |
| 52 | + } |
| 53 | + }; |
| 54 | + getAllAccounts = async (req, res, next) => { |
| 55 | + try { |
| 56 | + if (req.user.role !== account_model_1.Role.Admin) { |
| 57 | + this.sendResponse(res, 403 /* HttpStatus.FORBIDDEN */, 'Permission denied'); |
| 58 | + return; |
| 59 | + } |
| 60 | + const { role } = req.query; |
| 61 | + if (role !== undefined) { |
| 62 | + if (typeof role !== 'string' || !Object.values(account_model_1.Role).includes(role)) { |
| 63 | + this.sendResponse(res, 400 /* HttpStatus.BAD_REQUEST */, 'Invalid role filter'); |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + const result = await this.service.getAll(role); |
| 68 | + this.sendResponse(res, 200 /* HttpStatus.OK */, 'Accounts retrieved successfully', result); |
| 69 | + } |
| 70 | + catch (error) { |
| 71 | + next(error); |
| 72 | + } |
| 73 | + }; |
| 74 | + updateAccount = async (req, res, next) => { |
| 75 | + try { |
| 76 | + const id = parseInt(req.params.id); |
| 77 | + const userId = req.user?.userId; |
| 78 | + if (!userId) { |
| 79 | + res.status(401 /* HttpStatus.UNAUTHORIZED */).json({ message: 'User not authenticated' }); |
| 80 | + return; |
| 81 | + } |
| 82 | + const result = await this.service.update(id, req.body, userId); |
| 83 | + this.sendResponse(res, 200 /* HttpStatus.OK */, 'Account updated successfully', result); |
| 84 | + } |
| 85 | + catch (error) { |
| 86 | + next(error); |
| 87 | + } |
| 88 | + }; |
| 89 | + deleteAccount = async (req, res, next) => { |
| 90 | + try { |
| 91 | + const id = parseInt(req.params.id); |
| 92 | + const userId = req.user?.userId; |
| 93 | + if (!userId) { |
| 94 | + res.status(401 /* HttpStatus.UNAUTHORIZED */).json({ message: 'User not authenticated' }); |
| 95 | + return; |
| 96 | + } |
| 97 | + await this.service.delete(id, userId); |
| 98 | + this.sendResponse(res, 200 /* HttpStatus.OK */, 'Account deleted successfully'); |
| 99 | + } |
| 100 | + catch (error) { |
| 101 | + next(error); |
| 102 | + } |
| 103 | + }; |
| 104 | +} |
| 105 | +exports.AccountController = AccountController; |
0 commit comments