All files / src/models organisation.js

79.31% Statements 23/29
62.5% Branches 5/8
100% Functions 2/2
79.31% Lines 23/29

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 30193x 193x 193x 193x 193x 2x     2x 2x     2x 2x 2x 193x 2x 2x 2x 2x 2x 1x 1x 1x     1x 1x 2x  
import { getSummary } from '@clevercloud/client/esm/api/v2/user.js';
import _ from 'lodash';
import { sendToApi } from './send-to-api.js';
 
export async function getId(orgaIdOrName) {
  if (orgaIdOrName == null) {
    return null;
  }
 
  if (orgaIdOrName.orga_id != null) {
    return orgaIdOrName.orga_id;
  }
 
  return getByName(orgaIdOrName.orga_name).then((orga) => orga.id);
}
 
async function getByName(name) {
  const fullSummary = await getSummary({}).then(sendToApi);
  const filteredOrgs = _.filter(fullSummary.organisations, { name });
 
  if (filteredOrgs.length === 0) {
    throw new Error('Organisation not found');
  }
  if (filteredOrgs.length > 1) {
    throw new Error('Ambiguous organisation name');
  }
 
  return filteredOrgs[0];
}