All files / api user.js

91.3% Statements 21/23
80% Branches 12/15
100% Functions 2/2
91.3% Lines 21/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    1x 1x   1x 1x                                                               3x 1x     2x 1x     1x 1x 1x 1x 1x   1x                         4x 1x 3x 1x     2x 1x         1x        
'use strict';
 
const { SdkError } = require('general-mq');
const { DataTypes } = require('general-mq/lib/constants');
 
const { ErrorCode, HttpMethod } = require('./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`.
 *
 * @async
 * @param {Client} client
 * @param {function} callback
 * @returns {Promise<GetResData>}
 * @throws {Error} Wrong arguments.
 * @throws {SdkError}
 */
async function get(client) {
  if (!(client instanceof Client)) {
    throw Error('`client` is not a Client');
  }
 
  const res = await client.request(HttpMethod.Get, '/api/v1/user');
  Iif (res.status !== 200) {
    throw SdkError({ code: ErrorCode.Rsc, message: JSON.stringify(res.body) });
  }
  const data = res.body.data;
  data.createdAt = new Date(data.createdAt);
  data.modifiedAt = new Date(data.modifiedAt);
  Eif (data.verifiedAt) {
    data.verifiedAt = new Date(data.verifiedAt);
  }
  return data;
}
 
/**
 * `PATCH /coremgr/api/v1/user`
 *
 * @async
 * @param {Client} client
 * @param {PatchReqData} data
 * @throws {Error} Wrong arguments.
 * @throws {SdkError}
 */
async function update(client, data) {
  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');
  }
 
  const res = await client.request(HttpMethod.Patch, '/api/v1/user', { data });
  Iif (res.status !== 204) {
    return { code: ErrorCode.Rsc, message: JSON.stringify(body) };
  }
}
 
module.exports = {
  get,
  update,
};