sbt-idp/cope2n-fe/src/request/report.ts

157 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-02-01 09:37:50 +00:00
import { notification } from 'antd';
2024-01-31 04:08:20 +00:00
import {
MakeReportParams,
MakeReportResponse,
2024-01-31 09:54:39 +00:00
OverViewDataResponse,
2024-01-31 04:08:20 +00:00
ReportDetailList,
ReportDetailListParams,
ReportListParams,
ReportListType,
2024-02-05 11:20:10 +00:00
DashboardOverviewParams,
2024-01-31 04:08:20 +00:00
} from 'models';
import { API } from './api';
export async function getReportDetailList(params: ReportDetailListParams) {
try {
const response = await API.get<ReportDetailList>(
'/ctel/report_detail_list/',
{
params: {
report_id: params.report_id,
page: params.page,
},
},
);
return response.data;
} catch (error) {
2024-02-01 09:37:50 +00:00
notification.error({
message: `${error?.message}`,
});
2024-01-31 04:08:20 +00:00
console.log(error);
}
}
export async function makeReport(params: MakeReportParams) {
const { end_date, start_date, subsidiary } = params;
try {
2024-02-07 03:37:41 +00:00
const response = await API.post<MakeReportResponse>(`/ctel/make_report/`, {
2024-02-07 04:29:27 +00:00
start_date: start_date,
end_date: end_date,
subsidiary: subsidiary,
2024-01-31 04:08:20 +00:00
});
return response.data;
} catch (error) {
2024-02-01 09:37:50 +00:00
notification.error({
message: `${error?.message}`,
});
2024-01-31 04:08:20 +00:00
console.log(error);
}
}
export async function getReportList(params?: ReportListParams) {
try {
const response = await API.get<ReportListType>('/ctel/report_list/', {
params: {
page: params?.page,
page_size: params?.page_size,
start_date: params?.start_date,
end_date: params?.end_date,
},
});
return response.data;
} catch (error) {
2024-02-01 09:37:50 +00:00
notification.error({
message: `${error?.message}`,
});
2024-01-31 04:08:20 +00:00
console.log(error);
}
}
2024-01-31 09:54:39 +00:00
2024-02-05 11:20:10 +00:00
export async function getOverViewReport(params?: DashboardOverviewParams) {
2024-01-31 09:54:39 +00:00
try {
const response = await API.get<OverViewDataResponse>('/ctel/overview/', {
params: {
2024-02-05 11:20:10 +00:00
duration: params?.duration,
2024-01-31 09:54:39 +00:00
subsidiary: params?.subsidiary,
},
});
return response.data;
} catch (error) {
2024-02-01 09:37:50 +00:00
notification.error({
message: `${error?.message}`,
});
2024-01-31 09:54:39 +00:00
console.log(error);
}
}
2024-01-31 11:57:42 +00:00
2024-02-06 03:56:57 +00:00
export async function downloadReport(report_id: string, downloadFinishedCallback?: (fileDetails: any) => void) {
2024-01-31 11:57:42 +00:00
try {
const response = await API.get(`/ctel/get_report_file/${report_id}/`, {
responseType: 'blob', // Important
});
2024-02-02 07:14:46 +00:00
let filename = "report.xlsx";
try {
let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0];
2024-02-06 03:09:02 +00:00
if (basename[0] == '_') {
basename = basename.substring(1);
}
filename = `${basename}.xlsx`
2024-02-02 07:14:46 +00:00
} catch(err) {
console.log(err);
}
2024-01-31 11:57:42 +00:00
const file = new Blob([response.data], {
type: 'application/vnd.ms-excel',
});
2024-02-06 03:56:57 +00:00
downloadFinishedCallback && downloadFinishedCallback({
file: file,
filename: filename,
});
2024-02-02 07:14:46 +00:00
return {
file: file,
filename: filename,
}
2024-01-31 11:57:42 +00:00
} catch (error) {
2024-02-06 04:08:36 +00:00
downloadFinishedCallback && downloadFinishedCallback({
file: null,
filename: null,
});
2024-02-01 09:37:50 +00:00
notification.error({
message: `${error?.message}`,
});
2024-01-31 11:57:42 +00:00
console.log(error);
}
}
2024-02-05 08:52:15 +00:00
export async function downloadDashboardReport(duration='30d', subsidiary='ALL') {
try {
2024-02-05 11:52:22 +00:00
const response = await API.get(`/ctel/overview_download_file/?duration=${duration}&subsidiary=${subsidiary}`, {
2024-02-05 08:52:15 +00:00
responseType: 'blob', // Important
});
let filename = "report.xlsx";
try {
let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0];
2024-02-06 03:09:02 +00:00
if (basename[0] == '_') {
basename = basename.substring(1);
}
filename = `${basename}.xlsx`
2024-02-05 08:52:15 +00:00
} catch(err) {
console.log(err);
}
const file = new Blob([response.data], {
type: 'application/vnd.ms-excel',
});
// const fileURL = URL.createObjectURL(file);
// window.open(fileURL);
return {
file: file,
filename: filename,
}
} catch (error) {
notification.error({
message: `${error?.message}`,
});
console.log(error);
}
}