All files / platform/modules/service/src/Controllers/Http ServiceCategoryController.js

100% Statements 27/27
100% Branches 0/0
100% Functions 6/6
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 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198    1x   1x 1x       7x       7x 7x                                                                     1x 1x 1x   1x   1x       1x     1x                                                                       1x 1x   1x   1x 1x                                                                             3x 3x   3x         2x 2x                                                                   2x 2x         1x       1x  
'use strict';
 
const _ = require('lodash');
 
const configs = use('Config').get('modules.service.general');
const namespace = configs.namespace;
 
class ServiceCategoryController {
  static get inject() {
    return [`${namespace}/Services/ServiceService`, `${namespace}/Services/SalonService`];
  }
 
  constructor(serviceService, salonService) {
    this.serviceService = serviceService;
    this.salonService = salonService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/services/categories:
   *   get:
   *     tags:
   *       - services
   *     operationId: 'getAllServiceCategories'
   *     summary: 'getAllServiceCategories'
   *     description: 'Get all service categories (include all services / category)'
   *     parameters:
   *       - $ref: '#/components/parameters/WithTrashedParam'
   *     responses:
   *       200:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ServiceCategoryPaginationResponse'
   *       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 index({ request, response, transform }) {
    const withTrashed = request.input('withTrashed');
    const user = request.user;
    const salonId = _.get(user, 'userMetadata.salonId');
    // get salon
    await this.salonService.findOrFail(salonId);
    // get all categories with service
    const serviceCategories = await this.serviceService.getAllCategories({
      serviceConditions: { userId: user.userId, withTrashed },
    });
    // transform data
    const data = await transform
      .include('services')
      .collection(serviceCategories, 'ServiceCategoryTransformer');
    return response.success(data);
  }
 
  /**
   * @swagger
   *
   * /api/v1/services/categories:
   *   post:
   *     tags:
   *       - services
   *     operationId: 'addServiceCategory'
   *     summary: 'addServiceCategory'
   *     parameters: []
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/ServiceCategoryBody'
   *     responses:
   *       201:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ServiceCategoryResponse'
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       422:
   *         $ref: '#/components/responses/BadData'
   *     deprecated: false
   *     security:
   *      - BearerAuth: []
   */
  async store({ request, response, transform }) {
    const input = request.only(['name', 'description']);
    input.userId = request.user.userId;
 
    const cate = await this.serviceService.createCategory(input);
 
    const data = await transform.item(cate, 'ServiceCategoryTransformer');
    return response.success(data, 201);
  }
 
  /**
   * @swagger
   *
   * /api/v1/services/categories/{categoryId}:
   *   put:
   *     tags:
   *       - services
   *     operationId: 'updateServiceCategory'
   *     summary: 'updateServiceCategory'
   *     parameters:
   *       - $ref: '#/components/parameters/CategoryIdParam'
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/ServiceCategoryBody'
   *     responses:
   *       200:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ServiceCategoryResponse'
   *       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, transform }) {
    const input = request.only(['name', 'description', 'order', 'active']);
    const id = request.params.categoryId;
 
    const cate = await this.serviceService.updateCategory(
      { id, userId: request.user.userId },
      { ...input },
    );
 
    const data = await transform.item(cate, 'ServiceCategoryTransformer');
    return response.success(data);
  }
 
  /**
   * @swagger
   *
   * /api/v1/services/categories/{categoryId}:
   *   delete:
   *     tags:
   *       - services
   *     operationId: 'deleteServiceCategory'
   *     summary: 'deleteServiceCategory'
   *     parameters:
   *       - $ref: '#/components/parameters/CategoryIdParam'
   *     responses:
   *       200:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/DeletedResponse'
   *       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 destroy({ request, response }) {
    const id = request.params.categoryId;
    await this.serviceService.deleteCategory({
      id,
      userId: request.user.userId,
    });
 
    return response.success({ id });
  }
}
 
module.exports = ServiceCategoryController;