ReportService.ts 2.02 KB
Newer Older
Maksym Tishchenko's avatar
Maksym Tishchenko committed
1
2
3
4
import * as UrlTemplate from 'url-template';
import { AxiosInstance, AxiosRequestConfig } from 'axios';
import RepositoryFile from "@gringlobal-ce/client/model/repository/RepositoryFile";

Matija Obreza's avatar
Matija Obreza committed
5
6
7
const API_PREFIX = '/api/v1/'
const URL_GENERATE_REPORT = UrlTemplate.parse(API_PREFIX + '{endpoint}/report/generate/{reportTemplate}');
const URL_GET_REPORT_LIST = UrlTemplate.parse(API_PREFIX + '{endpoint}/report/list');
Maksym Tishchenko's avatar
Maksym Tishchenko committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21

/**
 * Repository service
 *
 * GRIN-Global CE API
 */
class RepositoryService {
  private _axios: AxiosInstance;

  public constructor(axios: AxiosInstance) {
    this._axios = axios;
  }

  /**
Matija Obreza's avatar
Matija Obreza committed
22
   * generateReport at /api/v1/{endpoint}/report/generate/{reportTemplate}
Maksym Tishchenko's avatar
Maksym Tishchenko committed
23
   *
Matija Obreza's avatar
Matija Obreza committed
24
   * @param endpoint the API endpoint base (e.g. "order", "a")
Maksym Tishchenko's avatar
Maksym Tishchenko committed
25
26
27
28
   * @param reportTemplate undefined
   * @param entityIds undefined
   * @param xhrConfig additional xhr config
   */
Matija Obreza's avatar
Matija Obreza committed
29
  public generateReport = (endpoint: string, reportTemplate: string, entityIds: number[], xhrConfig?: AxiosRequestConfig): Promise<unknown> => {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
30

Matija Obreza's avatar
Matija Obreza committed
31
    const apiUrl = URL_GENERATE_REPORT.expand({ endpoint, reportTemplate });
Maksym Tishchenko's avatar
Maksym Tishchenko committed
32
    // console.log(`Fetching from ${apiUrl}`);
Matija Obreza's avatar
Matija Obreza committed
33
34
35
    const content = {
      data: entityIds
    };
Maksym Tishchenko's avatar
Maksym Tishchenko committed
36
37
38
39
40
41
42

    return this._axios.request({
      ...xhrConfig,
      url: apiUrl,
      method: 'POST',
      responseType: 'blob',
      headers: {
Matija Obreza's avatar
Matija Obreza committed
43
        'Accept': 'application/pdf, */*'
Maksym Tishchenko's avatar
Maksym Tishchenko committed
44
45
      },
      ...content,
Matija Obreza's avatar
Matija Obreza committed
46
    }).then(({ data }) => data as unknown);
Maksym Tishchenko's avatar
Maksym Tishchenko committed
47
48
49
  }

  /**
Matija Obreza's avatar
Matija Obreza committed
50
   * getReportList at /api/v1/{endpoint}/report/list
Maksym Tishchenko's avatar
Maksym Tishchenko committed
51
   *
Matija Obreza's avatar
Matija Obreza committed
52
   * @param endpoint
Maksym Tishchenko's avatar
Maksym Tishchenko committed
53
54
   * @param xhrConfig additional xhr config
   */
Matija Obreza's avatar
Matija Obreza committed
55
  public getReportList = (endpoint: string, xhrConfig?: AxiosRequestConfig): Promise<RepositoryFile[]> => {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
56

Matija Obreza's avatar
Matija Obreza committed
57
    const apiUrl = URL_GET_REPORT_LIST.expand({ endpoint });
Maksym Tishchenko's avatar
Maksym Tishchenko committed
58
59
60
61
62
63
64
65
66
67
68
69
70
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return this._axios.request({
      ...xhrConfig,
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as RepositoryFile[]);
  }
}

export default RepositoryService;