All files / src/api/account profile.js

96.43% Statements 27/28
89.47% Branches 17/19
100% Functions 10/10
96.3% Lines 26/27

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                                                                              39x 5x     34x 11x     23x       39x                   27x 26x             27x                                         3x   2x       1x 1x   1x       1x   1x     2x                   11x   10x       2x 2x   2x 1x     2x   2x     10x          
// @flow
 
import { type Caller } from '.';
 
/**
 * A description of your profile.
 */
export type AccountProfile = {
  created: Date,
  department: { id: number, name: string, uuid: ?string },
  departmentAdmin: Array<{ id: number, uuid: ?string }>,
  email: string,
  id: number,
  name: string,
  projectOnly: boolean,
  projects: Array<{ id: number, name: string }>,
  status: string,
  username: string,
  uuid: string,
  verified: boolean,
};
 
/**
 * Parameters passed in for updating your profile.
 */
export type AccountProfileUpdateValues = {
  email?: string,
  name?: string,
};
 
/**
 * Manage your user’s profile.
 */
export interface AccountProfileAPI {
  get(): Promise<AccountProfile>;
  update(values: AccountProfileUpdateValues): Promise<AccountProfile>;
}
 
function fullName(firstName, lastName) {
  if (!firstName) {
    return lastName;
  }
 
  if (!lastName) {
    return firstName;
  }
 
  return `${firstName} ${lastName}`;
}
 
export function into(user: any): AccountProfile {
  return {
    created: new Date(user.date_joined),
    department: {
      id: user.reseller_id,
      name: user.reseller_name,
      uuid: user.cs_domain_id || null,
    },
    departmentAdmin:
      (user.reseller_rights &&
        user.reseller_rights.reduce((reduced, role) => {
          if (reduced.every(({ id }) => id !== role.id)) {
            reduced.push({
              id: role.id,
              name: role.name,
              uuid: role.domain_id || null,
            });
          }
 
          return reduced;
        }, [])) ||
      [],
    email: user.email,
    enabled: user.status.toLowerCase() === 'enabled',
    id: user.id,
    isSuperAdmin: user.is_superuser,
    name: fullName(user.first_name, user.last_name).trim() || user.username,
    projectOnly: user.disable_personal_account === true,
    projects: user.projects,
    status: user.status.toLowerCase(),
    username: user.username,
    uuid: user.uuid,
    verified: user.is_verified === true,
  };
}
 
export function gqConsoleProfile(call: Caller): AccountProfileAPI {
  async function get() {
    const {
      objects: [user],
    } = await call('GET', 'account');
 
    return into(user);
  }
 
  async function update({ email, name }) {
    const { id, name: oldName } = await get();
    const data = { email, full_name: name };
 
    Iif (typeof data.full_name === 'undefined') {
      data.full_name = oldName;
    }
 
    await call('PUT', `account/${id}`, { data });
 
    return get();
  }
 
  return {
    get,
    update,
  };
}
 
export function profile(call: Caller): AccountProfileAPI {
  async function get() {
    const {
      objects: [user],
    } = await call('GET', 'account');
 
    return into(user);
  }
 
  async function update({ email, name }) {
    const { id, name: oldName } = await get();
    const data = { email, full_name: name };
 
    if (typeof data.full_name === 'undefined') {
      data.full_name = oldName;
    }
 
    await call('PUT', `accounts/${id}`, { data });
 
    return get();
  }
 
  return {
    get,
    update,
  };
}