All files / platform/modules/salon/src/Controllers/Http SalonBankController.js

95.65% Statements 22/23
75% Branches 3/4
100% Functions 3/3
95.65% Lines 22/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120    1x 1x 1x 1x 1x 1x       3x               3x 3x                                                                                                               3x 3x 3x   3x 3x 1x     2x   2x       2x                                     1x   1x   1x       1x  
'use strict';
 
const Event = use('Event');
const namespace = use('Config').get('modules.salon.general.namespace');
const bankNamespace = use('Config').get('modules.bank.general.namespace');
const { NotFoundException } = use('C2C/Exceptions');
const Config = use('Config');
const { SALON_BANK_UPDATED } = Config.get('modules.salon.constants');
 
class SalonController {
  static get inject() {
    return [
      'AuthService',
      `${namespace}/Services/SalonService`,
      `${bankNamespace}/Services/BankService`,
    ];
  }
 
  constructor(AuthService, salonService, bankService) {
    this.salonService = salonService;
    this.bankService = bankService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/salons/{salonId}/banks:
   *   patch:
   *     tags:
   *       - salons
   *     description: 'Update salon bank information'
   *     operationId: 'updateBankInfo'
   *     summary: 'updateBankInfo'
   *     security:
   *       - BearerAuth: []
   *     parameters:
   *       - name: salonId
   *         in: path
   *         required: true
   *         schema:
   *           type: string
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             properties:
   *               bankId:
   *                 type: string
   *               branchId:
   *                 type: string
   *               accountType:
   *                 type: string
   *               accountNumber:
   *                 type: number
   *               accountName:
   *                 type: string
   *               photo:
   *                 type: string
   *     responses:
   *       200:
   *         description: 'A object containing salon information'
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: "#/components/schemas/SalonResponse"
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       404:
   *         $ref: '#/components/responses/NotFound'
   *       422:
   *         $ref: '#/components/responses/BadData'
   *       500:
   *         description:  system error
   */
  async update({ request, response, transform }) {
    const { bankId, branchId, accountType, accountNumber, accountName, photo } = request.all();
    const salonId = request.params.id;
    const user = request.user;
 
    const bank = await this.bankService.getBank(bankId);
    if (!bank) {
      throw new NotFoundException('the bank you requested not found');
    }
 
    const branch = await this.bankService.getBranch(branchId, bank.id);
 
    Iif (!branch) {
      throw new NotFoundException('the bank branch you requested not found');
    }
 
    const salon = await this.salonService.update(
      { id: salonId },
      {
        bankInfo: JSON.stringify({
          bankId,
          branchId,
          bankName: bank.bankName,
          bankCode: bank.bankCode,
          branchName: branch.branchName,
          branchCode: branch.branchCode,
          accountType,
          accountNumber,
          accountName,
          photo,
        }),
      },
      user,
    );
 
    Event.fire(SALON_BANK_UPDATED, salon.toJSON());
 
    const transformed = await transform.item(salon, 'SalonTransformer');
 
    response.success(transformed);
  }
}
 
module.exports = SalonController;