📦 EqualifyEverything / equalify-dashboard

📄 reports.ts · 264 lines
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264import { del, get, post, put } from 'aws-amplify/api';

interface ApiResponse<T> {
  status: string;
  result: T;
  total?: number;
}

interface Report {
  id: string;
  name: string;
  activeIssues: number;
  mostCommonIssue: string;
  filters: any[];
}

const API_NAME = 'auth';

/**
 * Fetch all reports
 * @returns {Promise<Report[]>} List of reports
 * @throws Will throw an error if the fetch fails
 */
export const getReports = async (): Promise<Report[]> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: '/get/reports',
    }).response;

    const { body } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<Report[]>;
    return result;
  } catch (error) {
    console.error('Error fetching reports', error);
    throw error;
  }
};

/**
 * Fetch report by ID
 * @param {string} reportId - The ID of the report to fetch
 * @returns {Promise<Report>} The fetched report
 * @throws Will throw an error if the fetch fails
 */
export const getReportById = async (reportId: string): Promise<Report> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: '/get/reports',
      options: {
        queryParams: { reportId },
      },
    }).response;

    const { body } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<Report[]>;
    return result[0];
  } catch (error) {
    console.error('Error fetching report by ID', error);
    throw error;
  }
};
/**
 * Fetch report details
 * @param {string} reportId - The ID of the report to fetch details for
 * @returns {Promise<any>} The fetched report details
 * @throws Will throw an error if the fetch fails
 */
export const getReportDetails = async (reportId: string): Promise<any> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: `/get/results/all`,
      options: {
        queryParams: { reportId },
      },
    }).response;

    const { body } = response;
    const details = await body.json();
    return details;
  } catch (error) {
    console.error('Error fetching report details', error);
    throw error;
  }
};

/**
 * Fetch page details
 * @param {string} reportId - The ID of the report to fetch details for
 * @param {string} urlId - The ID of the report to fetch details for
 * @returns {Promise<any>} The fetched report details
 * @throws Will throw an error if the fetch fails
 */
export const getPageDetails = async (
  reportId: string,
  urlId: string,
): Promise<any> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: `/get/results/urls`,
      options: {
        queryParams: { reportId, urlId },
      },
    }).response;

    const { body } = response;
    const details = await body.json();
    return details;
  } catch (error) {
    console.error('Error fetching page details', error);
    throw error;
  }
};

/**
 * Fetch message details
 * @param {string} reportId - The ID of the report to fetch details for
 * @param {string} messageId - The ID of the report to fetch details for
 * @returns {Promise<any>} The fetched report details
 * @throws Will throw an error if the fetch fails
 */
export const getMessageDetails = async (
  reportId: string,
  messageId: string,
): Promise<any> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: `/get/results/messages`,
      options: {
        queryParams: { reportId, messageId },
      },
    }).response;

    const { body } = response;
    const details = await body.json();
    return details;
  } catch (error) {
    console.error('Error fetching message details', error);
    throw error;
  }
};

/**
 * Fetch tag details
 * @param {string} reportId - The ID of the report to fetch details for
 * @param {string} tagId - The ID of the report to fetch details for
 * @returns {Promise<any>} The fetched report details
 * @throws Will throw an error if the fetch fails
 */
export const getTagDetails = async (
  reportId: string,
  tagId: string,
): Promise<any> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: `/get/results/tags`,
      options: {
        queryParams: { reportId, tagId },
      },
    }).response;

    const { body } = response;
    const details = await body.json();
    return details;
  } catch (error) {
    console.error('Error fetching tag details', error);
    throw error;
  }
};

/**
 * Add a new report
 * @param {string} reportName - The name of the report
 * @param {any[]} filters - The filters for the report
 * @returns {Promise<{ result: Report; status: string }>} The added report and status
 * @throws Will throw an error if the addition fails
 */
export const addReport = async (
  reportName: string,
  filters: any[],
): Promise<{ result: Report; status: string }> => {
  try {
    const response = await post({
      apiName: API_NAME,
      path: '/add/reports',
      options: {
        body: { reportName, filters },
      },
    }).response;

    const { body, statusCode } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<Report>;
    return { result, status: statusCode === 200 ? 'success' : 'error' };
  } catch (error) {
    console.error('Error adding report', error);
    throw error;
  }
};

/**
 * Update a report
 * @param {string} reportId - The ID of the report to update
 * @param {string} reportName - The new name of the report
 * @param {any[]} filters - The new filters for the report
 * @returns {Promise<{ result: Report; status: string }>} The updated report and status
 * @throws Will throw an error if the update fails
 */
export const updateReport = async (
  reportId: string,
  reportName?: string,
  filters?: any[],
): Promise<{ result: Report; status: string }> => {
  try {
    const response = await put({
      apiName: API_NAME,
      path: '/update/reports',
      options: {
        body: {
          reportId,
          reportName,
          reportFilters: filters,
        },
      },
    }).response;
    const { body, statusCode } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<Report>;
    return { result, status: statusCode === 200 ? 'success' : 'error' };
  } catch (error) {
    console.error('Error updating report', error);
    throw error;
  }
};

/**
 * Delete a report
 * @param {string} reportId - The ID of the report to delete
 * @returns {Promise<{ status: string }>} The status of the deletion
 * @throws Will throw an error if the deletion fails
 */
export const deleteReport = async (
  reportId: string,
): Promise<{ status: string }> => {
  try {
    const response = await del({
      apiName: API_NAME,
      path: '/delete/reports',
      options: {
        queryParams: { reportId },
      },
    }).response;

    const { statusCode } = response;
    return { status: statusCode === 200 ? 'success' : 'error' };
  } catch (error) {
    console.error('Error deleting report', error);
    throw error;
  }
};