igor-starostenko/report_factory-web

View on GitHub
src/api/projects_client.js

Summary

Maintainability
B
6 hrs
Test Coverage
import ApiClient from './api_client';

class ProjectsClient extends ApiClient {
  queryProjects(xApiKey, { lastDays, lastMonths } = {}) {
    const headers = ApiClient.formatHeaders(xApiKey);
    return this.query({
      query: `query projects($lastDays: Int, $lastMonths: Int) {
        projects {
          id
          projectName
          reports(lastDays: $lastDays, lastMonths: $lastMonths) {
            createdAt
          }
        }
      }`,
      variables: { lastDays, lastMonths },
      headers,
    });
  }

  getAllProjects(xApiKey) {
    const url = `${this.baseUrl}api/v1/projects`;
    const headers = ApiClient.formatHeaders(xApiKey);
    return fetch(new Request(url, { method: 'GET', headers }));
  }

  createProject(projectName, xApiKey) {
    const url = `${this.baseUrl}api/v1/projects`;
    const headers = ApiClient.formatHeaders(xApiKey);
    const type = { type: 'project' };
    const attributes = { project_name: projectName };
    const body = ApiClient.formatPayload(type, attributes);
    return fetch(new Request(url, { method: 'POST', body, headers }));
  }

  queryProject(xApiKey, { projectName, lastDays, lastMonths } = {}) {
    const headers = ApiClient.formatHeaders(xApiKey);
    return this.query({
      query: `query project($projectName: String!, $lastDays: Int, $lastMonths: Int) {
        project(projectName: $projectName) {
          projectName
          reportsCount
          reports(lastDays: $lastDays, lastMonths: $lastMonths) {
            status
            createdAt
            updatedAt
          }
          scenarios(lastDays: $lastDays, lastMonths: $lastMonths) {
            status
            projectName
            fullDescription
          }
        }
      }`,
      variables: { projectName, lastDays, lastMonths },
      headers,
    });
  }

  showProject(projectName, xApiKey) {
    const url = `${this.baseUrl}api/v1/projects/${projectName}`;
    const headers = ApiClient.formatHeaders(xApiKey);
    return fetch(new Request(url, { method: 'GET', headers }));
  }

  updateProject(projectName, newName, xApiKey) {
    const url = `${this.baseUrl}api/v1/projects/${projectName}`;
    const headers = ApiClient.formatHeaders(xApiKey);
    const type = { type: 'project' };
    const attributes = { project_name: newName };
    const body = ApiClient.formatPayload(type, attributes);
    return fetch(new Request(url, { method: 'PUT', body, headers }));
  }

  deleteProject(projectName, xApiKey) {
    const url = `${this.baseUrl}api/v1/projects/${projectName}`;
    const headers = ApiClient.formatHeaders(xApiKey);
    return fetch(new Request(url, { method: 'DELETE', headers }));
  }
}

export default ProjectsClient;