All files / platform/modules/staff/src/Controllers/Http StaffOrderController.js

100% Statements 20/20
100% Branches 6/6
100% Functions 3/3
100% Lines 20/20

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    1x 1x       6x                 6x 6x 6x 6x                                                                                 6x 6x   6x           4x 2x         2x       2x         2x       2x       2x         4x   4x       1x  
'use strict';
 
const configs = use('Config').get('modules.staff.general');
const namespace = configs.namespace;
 
class StaffOrderController {
  static get inject() {
    return [
      `${namespace}/Services/StaffService`,
      `${namespace}/Services/SalonService`,
      `C2C/Services/SystemService`,
      `LexoRank`,
    ];
  }
 
  constructor(staffService, salonService, systemService, LexoRank) {
    this.staffService = staffService;
    this.salonService = salonService;
    this.systemService = systemService;
    this.LexoRank = LexoRank;
  }
 
  /**
   * @swagger
   *
   * /api/v1/staff/{staffId}/order:
   *   put:
   *     tags:
   *       - staff
   *     operationId: 'updateStaffOrder'
   *     summary: 'updateStaffOrder'
   *     parameters:
   *       - $ref: '#/components/parameters/StaffIdParam'
   *     requestBody:
   *       description: 'A object contain the staff-id of the prev/next order'
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/StaffUpdateOrderRequest'
   *     responses:
   *       200:
   *         description: |-
   *           success
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       404:
   *         $ref: '#/components/responses/NotFound'
   *       422:
   *         $ref: '#/components/responses/BadData'
   *     deprecated: false
   *     security:
   *      - BearerAuth: []
   */
  async update({ request, response, salon, transform }) {
    const prevStaffId = request.input('prevStaffId');
    const staffId = request.params.staffId;
 
    const currentStaff = await this.staffService.findOneOrFail({
      id: staffId,
      salonId: salon.id,
    });
 
    let newOrder;
    if (prevStaffId) {
      const prevStaff = await this.staffService.findOneOrFail({
        id: prevStaffId,
        salonId: salon.id,
      });
      // find next order in DB
      const nextOrder = await this.staffService.findMinOrder({
        order: ['>', prevStaff.order],
      });
      // generate new
      newOrder = nextOrder
        ? this.LexoRank.between(prevStaff.order, nextOrder)
        : this.LexoRank.genNext(prevStaff.order);
    } else {
      // find first of salon
      const firstOrder = await this.staffService.findMinOrder({
        salonId: salon.id,
      });
      // find prev order of first
      const prevOrderOfFirst = await this.staffService.findMaxOrder({
        order: ['<', firstOrder],
      });
      // generate new
      newOrder = prevOrderOfFirst
        ? this.LexoRank.between(prevOrderOfFirst, firstOrder)
        : this.LexoRank.genPrev(firstOrder);
    }
    // update order of current staff
    await this.staffService.update({ id: currentStaff.id }, { order: newOrder });
 
    return response.success();
  }
}
 
module.exports = StaffOrderController;