All files / platform/modules/booking/src/Controllers/Http BookingNotificationController.js

100% Statements 25/25
100% Branches 4/4
100% Functions 5/5
100% Lines 23/23

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157    1x 1x 1x       6x       6x                                                                     4x 4x 4x 4x 4x               4x     4x 4x 4x             4x       4x                                                             1x       1x   1x                                                               1x         1x   1x       1x  
'use strict';
 
const Config = use('Config');
const configs = Config.get('modules.booking.general');
const namespace = configs.namespace;
 
class BookingNotificationController {
  static get inject() {
    return [`${namespace}/Services/NotificationService`];
  }
 
  constructor(notificationService) {
    this._notificationService = notificationService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/bookings/notifications:
   *   get:
   *     tags:
   *       - bookings
   *     operationId: 'getBookingNotification'
   *     summary: 'getBookingNotification'
   *     description: 'get booking notifications'
   *     parameters:
   *       - $ref: '#/components/parameters/PageParam'
   *       - $ref: '#/components/parameters/LimitParam'
   *       - $ref: '#/components/parameters/IsReadParam'
   *       - $ref: '#/components/parameters/FromCreatedAtParam'
   *       - $ref: '#/components/parameters/XFieldsHeader'
   *     responses:
   *       200:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: "#/components/schemas/NotificationPaginationResponse"
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       404:
   *         $ref: '#/components/responses/NotFound'
   *     deprecated: false
   *     security:
   *      - BearerAuth: []
   */
  async index({ request, response, transform }) {
    const isRead = request.input('isRead');
    const page = request.input('page', 1);
    const limit = request.input('limit', 20);
    const fromCreatedAt = request.input('fromCreatedAt');
    const includes = transform.getPartialIncludes([
      'booking',
      'booking.orders',
      'booking.orders.staff',
      'booking.orders.staff.connections',
      'booking.notes',
    ]);
 
    const conditions = {
      userId: request.user.userId,
    };
    if (isRead !== undefined) conditions.isRead = isRead;
    if (fromCreatedAt !== undefined) conditions.fromCreatedAt = fromCreatedAt;
    const notifications = await this._notificationService.getWithPage(
      conditions,
      page,
      limit,
      includes,
    );
 
    const data = await transform
      .include(includes)
      .paginate(notifications, 'NotificationTransformer');
 
    return response.success(data);
  }
 
  /**
   * @swagger
   *
   * /api/v1/bookings/notifications/read:
   *   post:
   *     tags:
   *       - bookings
   *     operationId: 'readAllBookingNotification'
   *     summary: 'readAllBookingNotification'
   *     description: 'read all booking notifications'
   *     parameters: []
   *     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 readAll({ request, response }) {
    const conditions = {
      userId: request.user.userId,
    };
 
    await this._notificationService.readBy(conditions);
 
    return response.success();
  }
 
  /**
   * @swagger
   *
   * /api/v1/bookings/notifications/{notificationId}/read:
   *   post:
   *     tags:
   *       - bookings
   *     operationId: 'readBookingNotification'
   *     summary: 'readBookingNotification'
   *     description: 'read booking notification'
   *     parameters:
   *       - $ref: '#/components/parameters/NotificationIdParam'
   *     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 read({ request, params, response }) {
    const conditions = {
      userId: request.user.userId,
      id: Number(params.notificationId),
    };
 
    await this._notificationService.readBy(conditions);
 
    return response.success();
  }
}
 
module.exports = BookingNotificationController;