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

100% Statements 27/27
100% Branches 0/0
100% Functions 7/7
100% Lines 26/26

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    1x   1x 1x 1x       8x       8x 8x                                                             4x 4x 4x   4x       1x   1x       1x                                                                                       4x 4x 4x 4x   4x       2x         2x 2x   2x         1x             2x       1x  
'use strict';
 
const _ = require('lodash');
 
const Config = use('Config');
const namespace = Config.get('modules.salon.general.namespace');
const { SALON_SURVEY_RESULT_UPDATED } = Config.get('modules.salon.constants');
 
class SalonSurveyResultController {
  static get inject() {
    return [`${namespace}/Services/SalonService`, `${namespace}/Services/SurveyService`];
  }
 
  constructor(salonService, surveyService) {
    this.salonService = salonService;
    this.surveyService = surveyService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/salons/{salonId}/surveys/{surveyId}/results:
   *    get:
   *     tags:
   *       - salons
   *     operationId: 'getSalonSurveyResultBySurveyId'
   *     summary: 'getSalonSurveyResultBySurveyId'
   *     description: 'get salon survey results'
   *     security:
   *       - BearerAuth: []
   *     parameters:
   *       - $ref: '#/components/parameters/SalonIdParam'
   *       - $ref: '#/components/parameters/SurveyIdParam'
   *     responses:
   *       200:
   *         description: ok
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/SalonSurveyResultResponse'
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       500:
   *         description:  system error
   */
  async index({ request, response, transform }) {
    const surveyId = request.params.surveyId;
    const salonId = request.params.id;
    const user = request.user;
 
    const [salon, survey] = await Promise.all([
      this.salonService.findOrFail(salonId, user),
      this.surveyService.findOrFail(surveyId),
    ]);
    const surveyResults = await this.salonService.getSurveyResults(salon);
 
    response.success({
      id: survey.id,
      type: survey.type,
      title: survey.title,
      answers: surveyResults.toJSON().map((surveyResult) => ({
        questionId: _.get(surveyResult, 'question.id'),
        questionText: _.get(surveyResult, 'question.text'),
        answer: _.get(surveyResult, 'answer'),
      })),
    });
  }
 
  /**
   * @swagger
   *
   * /api/v1/salons/{salonId}/surveys/{surveyId}/results:
   *    put:
   *     tags:
   *       - salons
   *     operationId: 'updateSalonSurveyResult'
   *     summary: 'updateSalonSurveyResult'
   *     description: 'update salon survey results'
   *     security:
   *       - BearerAuth: []
   *     parameters:
   *       - $ref: '#/components/parameters/SalonIdParam'
   *       - $ref: '#/components/parameters/SurveyIdParam'
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/SalonSurveyResultBody'
   *     responses:
   *       200:
   *         description: 'OK'
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       404:
   *         $ref: '#/components/responses/NotFound'
   *       500:
   *         description:  system error
   */
 
  async update({ request, response, transform }) {
    let questions = request.input('questions', []);
    const surveyId = request.params.surveyId;
    const salonId = request.params.id;
    const user = request.user;
 
    const [salon, survey] = await Promise.all([
      this.salonService.findOrFail(salonId, user),
      this.surveyService.findOrFail(surveyId),
    ]);
    questions = questions.map((question) => ({
      questionId: question.id,
      answer: question.answer,
    }));
 
    await this.salonService.updateSurveyResult(salon, questions);
    const surveyResults = await this.salonService.getSurveyResults(salon);
 
    use('Event').fire(SALON_SURVEY_RESULT_UPDATED, {
      salonId: salon.id,
      surveyResults: {
        type: survey.type,
        title: survey.title,
        answers: surveyResults.toJSON().map((surveyResult) => ({
          name: _.get(surveyResult, 'question.text'),
          answer: !!_.get(surveyResult, 'answer'),
        })),
      },
    });
 
    response.success({});
  }
}
 
module.exports = SalonSurveyResultController;