All files / platform/modules/booking/src/UseCases CreateOrder.js

100% Statements 17/17
100% Branches 2/2
100% Functions 3/3
100% Lines 17/17

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    1x   1x 1x 1x 1x 1x       133x       133x       297x 297x   297x   293x   293x   4x 4x 4x         1x  
'use strict';
 
const assert = require('assert');
 
const Event = use('Event');
const Config = use('Config');
const Logger = use('Logger');
const CE = use('C2C/Exceptions');
const { ORDER_CREATED_FAILED } = Config.get('modules.booking.constants');
 
class CreateOrder {
  static get inject() {
    return ['C2C/Repositories/OrderRepo'];
  }
 
  constructor(orderRepo) {
    this.orderRepo = orderRepo;
  }
 
  async execute(payload) {
    try {
      const { bookingId, staffId } = payload;
 
      assert(bookingId && staffId, 'Required fields');
 
      const order = await this.orderRepo.create(payload);
 
      return order;
    } catch (error) {
      Event.fire(ORDER_CREATED_FAILED, payload);
      Logger.info(`Event emmited: ${ORDER_CREATED_FAILED}, payload: ${JSON.stringify(payload)}`);
      throw CE.BadRequestException.raise('errors.serviceOrStaffNotAvailable');
    }
  }
}
 
module.exports = CreateOrder;