158 lines
4.0 KiB
TypeScript
158 lines
4.0 KiB
TypeScript
import { notification } from 'antd';
|
|
import {
|
|
MakeReportParams,
|
|
MakeReportResponse,
|
|
OverViewDataResponse,
|
|
ReportDetailList,
|
|
ReportDetailListParams,
|
|
ReportListParams,
|
|
ReportListType,
|
|
DashboardOverviewParams,
|
|
} 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) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function makeReport(params: MakeReportParams) {
|
|
const { end_date, start_date, subsidiary, report_type } = params;
|
|
try {
|
|
const response = await API.post<MakeReportResponse>(`/ctel/make_report/`, {
|
|
start_date: start_date,
|
|
end_date: end_date,
|
|
subsidiary: subsidiary,
|
|
report_type: report_type,
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
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) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function getOverViewReport(params?: DashboardOverviewParams) {
|
|
try {
|
|
const response = await API.get<OverViewDataResponse>('/ctel/overview/', {
|
|
params: {
|
|
duration: params?.duration,
|
|
subsidiary: params?.subsidiary,
|
|
},
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function downloadReport(report_id: string, report_expression: string, downloadFinishedCallback?: (fileDetails: any) => void) {
|
|
try {
|
|
const response = await API.get(`/ctel/get_report_file/${report_id}/?report_expression=${report_expression}`, {
|
|
responseType: 'blob', // Important
|
|
});
|
|
let filename = "report.xlsx";
|
|
try {
|
|
let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0];
|
|
if (basename[0] == '_') {
|
|
basename = basename.substring(1);
|
|
}
|
|
filename = `${basename}.xlsx`
|
|
} catch(err) {
|
|
console.log(err);
|
|
}
|
|
const file = new Blob([response.data], {
|
|
type: 'application/vnd.ms-excel',
|
|
});
|
|
downloadFinishedCallback && downloadFinishedCallback({
|
|
file: file,
|
|
filename: filename,
|
|
});
|
|
return {
|
|
file: file,
|
|
filename: filename,
|
|
}
|
|
} catch (error) {
|
|
downloadFinishedCallback && downloadFinishedCallback({
|
|
file: null,
|
|
filename: null,
|
|
});
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
|
|
export async function downloadDashboardReport(duration='30d', subsidiary='SEAO') {
|
|
try {
|
|
const response = await API.get(`/ctel/overview_download_file/?duration=${duration}&subsidiary=${subsidiary}`, {
|
|
responseType: 'blob', // Important
|
|
});
|
|
let filename = "report.xlsx";
|
|
try {
|
|
let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0];
|
|
if (basename[0] == '_') {
|
|
basename = basename.substring(1);
|
|
}
|
|
filename = `${basename}.xlsx`
|
|
} 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);
|
|
}
|
|
}
|