All files / src/api/admin departments.js

81.82% Statements 18/22
100% Branches 15/15
80% Functions 8/10
81.82% Lines 18/22

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    2x                                                                                                                       5x                                                     2x                               1x                 5x 3x   3x     2x   2x       2x 2x   2x       1x 1x   1x                               1x   1x     9x                  
// @flow
 
import { prune } from '../../utils/objects';
 
import { type Caller } from '.';
 
/**
 * A description of a department.
 */
export type AdminDepartment = {
  created: Date,
  displayName: string,
  enabled: boolean,
  id: number,
  level: number,
  name: string,
  parent: ?{ id: number },
  settings: {
    projectOnly: boolean,
  },
  status: string,
  uuid: ?string,
};
 
/**
 * Values for updating a department.
 */
export type AdminDepartmentUpdateValues = {
  displayName?: string,
  enabled?: boolean,
  settings?: {
    projectOnly?: boolean,
  },
};
 
/**
 * Values for creating a department.
 */
export type AdminDepartmentCreateValues = {
  ...AdminDepartmentUpdateValues,
  name: string,
  parent: ?{
    uuid: string,
  },
};
 
/**
 * Manage departments.
 */
export interface AdminDepartmentsAPI {
  list(id?: string): Promise<AdminDepartment | Array<AdminDepartment>>;
  create(values: AdminDepartmentCreateValues): Promise<AdminDepartment>;
  update(
    id: string,
    values: AdminDepartmentUpdateValues,
  ): Promise<AdminDepartment>;
  enable(id: string): Promise<AdminDepartment>;
  disable(id: string): Promise<AdminDepartment>;
  delete(id: string): Promise<any>;
}
 
function into(department: any): AdminDepartment {
  return {
    created: new Date(department.date_created),
    displayName: department.display_name,
    enabled: department.enabled,
    id: department.id,
    level: department.level,
    name: department.name,
    parent: department.ancestor
      ? {
          id: department.ancestor.id,
        }
      : null,
    settings: {
      projectOnly: department.disable_personal_accounts,
    },
    status: department.enabled ? 'enabled' : 'disabled',
    uuid: department.cs_domain_id || null,
  };
}
 
function intoCreateValues({
  name,
  parent,
  displayName = name,
  enabled = true,
  settings: { projectOnly = false } = {},
}: AdminDepartmentCreateValues): mixed {
  return prune({
    enabled,
    name,
    cs_domain_name: name,
    disable_personal_accounts: projectOnly,
    display_name: displayName,
    domain: name,
    parent_domain_id: parent ? parent.uuid : undefined,
  });
}
 
function intoUpdateValues({
  displayName,
  enabled,
  settings: { projectOnly } = {},
}: AdminDepartmentUpdateValues): mixed {
  return prune({
    enabled,
    disable_personal_accounts: projectOnly,
    display_name: displayName,
  });
}
 
export default function departments(call: Caller): AdminDepartmentsAPI {
  async function list(id) {
    if (id) {
      const department = await call('GET', `reseller/${id}`);
 
      return into(department);
    }
 
    const { objects } = await call('GET', 'reseller');
 
    return objects.map(into);
  }
 
  async function create(values) {
    const data = intoCreateValues(values);
    const { id } = await call('POST', `reseller`, { data });
 
    return list(id);
  }
 
  async function update(id, values) {
    const data = intoUpdateValues(values);
    await call('PUT', `reseller/${id}`, { data });
 
    return list(id);
  }
 
  async function enable(id) {
    await call('POST', `reseller/${id}/enable`);
 
    return list(id);
  }
 
  async function disable(id) {
    await call('POST', `reseller/${id}/disable`);
 
    return list(id);
  }
 
  async function remove(id) {
    await call('DELETE', `reseller/${id}`);
 
    return id;
  }
 
  return {
    list,
    create,
    update,
    enable,
    disable,
    delete: remove,
  };
}