All files / platform/modules/staff/src/Controllers/Http StaffConnectionController.js

100% Statements 20/20
100% Branches 0/0
100% Functions 3/3
100% Lines 20/20

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    1x   1x 1x 1x 1x 1x 1x       2x       2x 2x                                                                               2x                 2x 2x   2x               2x           1x 1x                 1x         1x       1x  
'use strict';
 
const _ = require('lodash');
 
const Config = use('Config');
const configs = Config.get('modules.staff.general');
const Event = use('Event');
const namespace = configs.namespace;
const connectionStatuses = configs.connectionStatuses;
const { STAFF_CONNECTION_UPDATED } = Config.get('modules.staff.constants');
 
class StaffConnectionController {
  static get inject() {
    return [`${namespace}/Services/StaffService`, `${namespace}/Services/SystemService`];
  }
 
  constructor(staffService, systemService) {
    this.staffService = staffService;
    this.systemService = systemService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/apps/{appId}/staff/{staffId}/connections:
   *   put:
   *     tags:
   *       - staff
   *     operationId: 'updateStaffConnection'
   *     summary: 'updateStaffConnection'
   *     parameters:
   *       - $ref: '#/components/parameters/AppIdParam'
   *       - $ref: '#/components/parameters/StaffIdParam'
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/SystemStaffBody'
   *     responses:
   *       200:
   *         description: ''
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       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, params, response }) {
    let body = request.only([
      'avatarLink',
      'name',
      'birthday',
      'gender',
      'phonetic',
      'username',
      'extraInfo',
    ]);
    const systemId = params.appId;
    const staffId = params.id;
 
    const [system, staff] = await Promise.all([
      this.systemService.findOrFail(systemId),
      this.staffService.findOneOrFail({
        id: staffId,
        userId: request.user.userId,
      }),
    ]);
 
    const connection = await this.staffService.findConnectionOrFail({
      systemId: system.id,
      staffId: staff.id,
      status: connectionStatuses.approved,
    });
 
    body = _.merge({}, connection.data, body);
    await this.staffService.updateConnection(
      {
        id: connection.id,
      },
      {
        data: JSON.stringify(body),
      },
    );
 
    Event.fire(STAFF_CONNECTION_UPDATED, {
      staffId: staff.id,
      systemId: system.id,
      data: { ...body },
    });
    return response.success({});
  }
}
 
module.exports = StaffConnectionController;