All files / platform/modules/third-party/src/Controllers/Http ThirdPartyStaffController.js

100% Statements 26/26
100% Branches 4/4
100% Functions 6/6
100% Lines 24/24

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137    1x   1x 1x 1x       5x       5x                                                             1x   1x                   1x 1x                                                                       4x 4x                 4x 4x 4x 1x     3x       3x 1x     2x         2x   2x           2x       1x  
'use strict';
 
const _ = require('lodash');
 
const Config = use('Config');
const namespace = Config.get('modules.thirdParty.general.namespace');
const CE = use('C2C/Exceptions');
 
class ThirdPartyStaffController {
  static get inject() {
    return [`${namespace}/Services/ThirdPartyService`];
  }
 
  constructor(thirdPartyService) {
    this.thirdPartyService = thirdPartyService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/staff/third-parties/{thirdPartyCode}/staff:
   *   get:
   *     tags:
   *       - third-parties
   *     operationId: 'getThirdPartySourceStaffList'
   *     summary: 'getThirdPartySourceStaffList'
   *     parameters:
   *       - $ref: '#/components/parameters/ThirdPartyCodeParam'
   *       - $ref: '#/components/parameters/SortParam'
   *     responses:
   *       200:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ThirdPartyStaffPaginationResponse'
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       404:
   *         $ref: '#/components/responses/NotFound'
   *     deprecated: false
   *     security:
   *      - BearerAuth: []
   */
  async index({ request, response, thirdParty, salon, transform }) {
    const sort = request.input('sort', 'createdAt desc');
 
    const staffs = await this.thirdPartyService.getListSourceStaff(
      {
        thirdPartyId: thirdParty.id,
        salonId: salon.id,
      },
      {
        sort,
      },
    );
 
    const transformed = await transform.collection(staffs, 'ThirdPartyStaffTransformer');
    return response.success(transformed, 200);
  }
 
  /**
   * @swagger
   *
   * /api/v1/salons/current/third-parties/{thirdPartyCode}/staff:
   *   put:
   *     tags:
   *       - third-parties
   *     operationId: 'salonPutThirdPartySourceStaff'
   *     summary: 'salonPutThirdPartySourceStaff'
   *     parameters:
   *       - $ref: '#/components/parameters/ThirdPartyCodeParam'
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/ThirdPartyStaffBody'
   *     responses:
   *       200:
   *         description: |-
   *           OK
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       404:
   *         $ref: '#/components/responses/NotFound'
   *     deprecated: false
   *     security:
   *      - BearerAuth: []
   */
  async update({ request, response, thirdParty, salon }) {
    let thirdPartyStaffs = request.input('thirdPartyStaffs', []);
    thirdPartyStaffs = thirdPartyStaffs.map(({ sourceId, name, kana, email }) => ({
      thirdPartyId: thirdParty.id,
      salonId: salon.id,
      sourceId,
      name,
      kana,
      email,
    }));
    // check unique by sourceId in input
    const sourceIds = thirdPartyStaffs.map(({ sourceId }) => sourceId);
    const uniqSourceIds = _.uniq(sourceIds);
    if (sourceIds.length !== uniqSourceIds.length) {
      throw CE.BadRequestException.raise('errors.duplicateSourceIdValues');
    }
    // check unique by sourceId in database
    const existsBySourceIds = await this.thirdPartyService.getListSourceStaff({
      salonId: ['<>', salon.id],
      sourceId: ['IN', sourceIds],
    });
    if (existsBySourceIds.size() > 0) {
      throw CE.BadRequestException.raise('errors.duplicateSourceIdValues');
    }
    // delete old source staff
    await this.thirdPartyService.deleteAllSourceStaff({
      thirdPartyId: thirdParty.id,
      salonId: salon.id,
    });
    // add new source staff
    await this.thirdPartyService.createManySourceStaff(thirdPartyStaffs);
    // delete connection by source ids
    await this.thirdPartyService.deleteStaffThirdPartyConnectionBy({
      salonId: salon.id,
      thirdPartyId: thirdParty.id,
      sourceId: ['NOT_IN', sourceIds],
    });
 
    return response.success();
  }
}
 
module.exports = ThirdPartyStaffController;