Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
eclipsefdn.membership.js 2.56 KiB
/*
 * Copyright (c) 2023 Eclipse Foundation, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * Contributors:
 *   Olivier Goulet <olivier.goulet@eclipse-foundation.org>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import 'isomorphic-fetch';
import { convertToQueryString } from '../utils/utils';

const apiBasePath = 'https://membership.eclipse.org/api';

export const organizationMapper = organization => {
  const { organization_id: organizationId, ...otherOrganizationFields } = organization;

  return {
    organizationId,
    ...otherOrganizationFields,
  };
};

export const getOrganizationById = async organizationId => {
  try {
    const response = await fetch(`${apiBasePath}/organizations/${organizationId}`);
    if (!response.ok) throw new Error(`Could not fetch organization by id ${organizationId}`);

    const data = await response.json();
    const organization = organizationMapper(data);

    return [organization, null];
  } catch (error) {
    console.error(error);
    return [null, error];
  }
};

export const getOrganizations = async (params = {}) => {
  try {
    const queryString = convertToQueryString(params); 
    const response = await fetch(`${apiBasePath}/organizations?${queryString}`);
    if (!response.ok) {
      throw new Error(`Could not fetch organizations`);
    }

    const data = await response.json();
    const organizations = data
      .map(organizationMapper)
    
    return [organizations, null];
  } catch (error) {
    console.error(error);
    return [null, 'An unexpected error has occurred when fetching organizations'];
  }
}

export const getProjectParticipatingOrganizations = async (projectId, params = {}) => {
  try {
    if (!projectId) {
        throw new Error('No project id provided for fetching project participating organizations');
    }

    const queryString = convertToQueryString(params);
    const response = await fetch(`${apiBasePath}/projects/${projectId}/organizations?${queryString}`);
    if (!response.ok) { 
        throw new Error(`Could not fetch project organizations for project id "${projectId}"`);
    }

    const data = await response.json();
    const organizations = data
        .map(organizationMapper)
        .sort((a, b) => a.name.localeCompare(b.name));

    return [organizations, null];
  } catch (error) {
    console.error(error);
    return [
      null,
      'An unexpected error has occurred when fetching participating organizations for project',
    ];
  }
};