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

100% Statements 17/17
100% Branches 4/4
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 39 40 41 42 43 44 45 46 47 48 49 50    1x 1x       133x       133x 133x       3x   3x   3x       3x 1x         2x         2x   2x   1x 1x     1x         1x  
'use strict';
 
const assert = require('assert');
const CE = use('C2C/Exceptions');
 
class HandleOrderCreatedError {
  static get inject() {
    return ['C2C/Repositories/BookingRepo', 'C2C/Repositories/OrderRepo'];
  }
 
  constructor(bookingRepo, orderRepo) {
    this._bookingRepo = bookingRepo;
    this._orderRepo = orderRepo;
  }
 
  async execute(payload) {
    const { bookingId } = payload;
 
    assert(bookingId, 'HandleOrderCreatedError.execute: Required fields');
 
    const booking = await this._bookingRepo.findOne({
      id: bookingId,
    });
 
    if (!booking) {
      throw CE.NotFoundException.raise(
        `HandleOrderCreatedError.execute - Booking not found with id: ${bookingId}`,
      );
    }
 
    const deletedOrders = await this._orderRepo.findOnlyTrashed({
      bookingId: booking.id,
    });
 
    // Remove new orders from booking
    await this._orderRepo.deleteMany({ bookingId: booking.id }, { force: true });
 
    if (deletedOrders.size() === 0) {
      // Booking created, so dissociate customer and remove booking
      await this._bookingRepo.dissociateCustomer(booking);
      await this._bookingRepo.delete(booking);
    } else {
      // Booking updating, so just restore deleted orders
      await this._orderRepo.restoreTrashed({ bookingId: booking.id });
    }
  }
}
 
module.exports = HandleOrderCreatedError;