All files / api user.js

96.42% Statements 27/28
86.95% Branches 20/23
100% Functions 4/4
96.42% Lines 27/28

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    1x   1x                                                             4x 1x 3x 1x     2x 2x 1x 1x     1x 1x 1x 1x 1x   1x                           5x 1x 4x 1x 3x 1x     2x 2x 1x   1x       1x        
'use strict';
 
const { DataTypes } = require('general-mq/lib/constants');
 
const { Client } = require('./http');
 
/**
 * @typedef {Object} GetResData
 * @property {string} [userId]
 * @property {string} account
 * @property {Date} createdAt
 * @property {Date} modifiedAt
 * @property {Date} [verifiedAt]
 * @property {Object} roles  <string, boolean> pairs.
 * @property {string} name
 * @property {Object} info
 */
 
/**
 * @typedef {Object} PatchReqData
 * @property {string} [password]
 * @property {string} [name]
 * @property {Object} [info]
 */
 
/**
 * `GET /coremgr/api/v1/user`.
 *
 * @param {Client} client
 * @param {function} callback
 *   @param {?Error} callback.err
 *   @param {GetResData} callback.data
 * @throws {Error} Wrong arguments.
 */
function get(client, callback) {
  if (!(client instanceof Client)) {
    throw Error('`client` is not a Client');
  } else if (typeof callback !== DataTypes.Function) {
    throw Error('`callback` is not a function');
  }
 
  client.request('GET', '/api/v1/user', (err, status, body) => {
    if (err) {
      return void callback(err);
    } else Iif (status !== 200) {
      return void callback(Error({ code: 'err_rsc', message: JSON.stringify(body) }));
    }
    const data = body.data;
    data.createdAt = new Date(data.createdAt);
    data.modifiedAt = new Date(data.modifiedAt);
    Eif (data.verifiedAt) {
      data.verifiedAt = new Date(data.verifiedAt);
    }
    callback(null, data);
  });
}
 
/**
 * `PATCH /coremgr/api/v1/user`
 *
 * @param {Client} client
 * @param {PatchReqData} data
 * @param {function} callback
 *   @param {?Error} callback.err
 * @throws {Error} Wrong arguments.
 */
function update(client, data, callback) {
  if (!(client instanceof Client)) {
    throw Error('`client` is not a Client');
  } else if (!data || typeof data !== DataTypes.Object || Array.isArray(data)) {
    throw Error('`data` is not an object');
  } else if (typeof callback !== DataTypes.Function) {
    throw Error('`callback` is not a function');
  }
 
  client.request('PATCH', '/api/v1/user', { data }, (err, status, body) => {
    if (err) {
      return void callback(err);
    }
    callback(status === 204 ? null : Error({ code: 'err_rsc', message: JSON.stringify(body) }));
  });
}
 
module.exports = {
  get,
  update,
};