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

100% Statements 29/29
100% Branches 2/2
100% Functions 4/4
100% Lines 27/27

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    1x   1x 1x 1x 1x 1x 1x       6x       6x 6x                                                                                                                                                           6x 6x 6x   6x 5x 2x     3x 5x           3x   1x 1x 1x 7x 7x 7x     1x               1x  
'use strict';
 
const moment = require('moment-timezone');
 
const Config = use('Config');
const Event = use('Event');
const CE = use('C2C/Exceptions');
const namespace = Config.get('modules.salon.general.namespace');
const defaultDateObject = Config.get('core.shared.general.defaultDateObject');
const { SALON_SCHEDULE_UPDATED } = Config.get('modules.salon.constants');
 
class ScheduleController {
  static get inject() {
    return [`${namespace}/Services/SalonService`, `C2C/Services/SalonService`];
  }
 
  constructor(salonService, sharedSalonService) {
    this.salonService = salonService;
    this.sharedSalonService = sharedSalonService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/salons/{salonId}/schedule:
   *    put:
   *     tags:
   *       - salons
   *     description: update salon schedule
   *     security:
   *       - BearerAuth: []
   *     parameters:
   *       - name: salonId
   *         in: path
   *         required: true
   *         schema:
   *           type: string
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/ScheduleBody'
   *     responses:
   *       200:
   *         description: 'An object containing the salon information'
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       403:
   *         description: Can not accesss to this salon
   *         content:
   *           application/json:
   *             schema:
   *               type: object
   *               properties:
   *                 statusCode:
   *                   type: number
   *                   example: 403
   *                 error:
   *                   type: string
   *                   example: ForbiddenException
   *                 message:
   *                   type: string
   *                   example: You can not accesss to this salon
   *                 data:
   *                   type: array
   *                   example: []
   *                   items:
   *                     type: object
   *       404:
   *         description: The salon you requested does not exist
   *         content:
   *           application/json:
   *             schema:
   *               type: object
   *               properties:
   *                 statusCode:
   *                   type: number
   *                   example: 404
   *                 error:
   *                   type: string
   *                   example: NotFoundException
   *                 message:
   *                   type: string
   *                   example: The salon you requested does not exist
   *                 data:
   *                   type: array
   *                   example: []
   *                   items:
   *                     type: object
   *       500:
   *         description:  system error
   */
 
  async update({ request, response, transform }) {
    const { schedules } = request.all();
    const salonId = request.params.id;
    const user = request.user;
 
    const salon = await this.salonService.findOrFail(salonId, user);
    if (salon.lockSchedule) {
      throw CE.ForbiddenException.raise('errors.scheduleLocked');
    }
 
    const timezone = salon.timezone;
    const schedulesData = schedules.map((schedule) => ({
      salonId,
      dayOfWeek: schedule.dayOfWeek,
      startAt: moment(schedule.startAt).tz(timezone).set(defaultDateObject),
      endAt: moment(schedule.endAt).tz(timezone).set(defaultDateObject),
    }));
    const newSchedules = await this.salonService.updateSchedules({ salonId }, schedulesData);
 
    response.success({});
    const data = [];
    for (let i = 0; i < newSchedules.length; i++) {
      const item = newSchedules[i];
      await item.reload();
      data.push(item.toJSON());
    }
 
    Event.fire(SALON_SCHEDULE_UPDATED, {
      salonId,
      timezone,
      schedules: data,
    });
  }
}
 
module.exports = ScheduleController;