Files
pp-tcms/frontend/utils/caseControl.ts
2025-05-06 10:05:37 +09:00

165 lines
4.1 KiB
TypeScript

import Config from '@/config/config';
const apiServer = Config.apiServer;
import { CaseType } from '@/types/case';
async function fetchCase(jwt: string, caseId: number) {
const url = `${apiServer}/cases/${caseId}`;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error('Error fetching data:', error.message);
}
}
async function fetchCases(jwt: string, folderId: number) {
const url = `${apiServer}/cases?folderId=${folderId}`;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error('Error fetching data:', error.message);
}
}
async function createCase(jwt: string, folderId: string, title: string, description: string) {
const newCase = {
title: title,
state: 0,
priority: 2,
type: 0,
automationStatus: 0,
description: description,
template: 0,
preConditions: '',
expectedResults: '',
};
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
body: JSON.stringify(newCase),
};
const url = `${apiServer}/cases?folderId=${folderId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error('Error creating case:', error);
throw error;
}
}
async function updateCase(jwt: string, updateCaseData: CaseType) {
const fetchOptions = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
body: JSON.stringify(updateCaseData),
};
const url = `${apiServer}/cases/${updateCaseData.id}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error('Error updating project:', error);
throw error;
}
}
async function deleteCases(jwt: string, deleteCaseIds: number[], projectId: number) {
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
body: JSON.stringify({ caseIds: deleteCaseIds }),
};
const url = `${apiServer}/cases/bulkdelete?projectId=${projectId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error: any) {
console.error('Error deleting cases:', error);
throw error;
}
}
async function csvDownload(jwt: string, folderId: number) {
const url = `${apiServer}/cases/download?folderId=${folderId}&type=csv`;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${jwt}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const blob = await response.blob();
const objectUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = objectUrl;
a.download = `folder_${folderId}.csv`;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(objectUrl);
} catch (error: any) {
console.error('Error fetching data:', error.message);
}
}
export { fetchCase, fetchCases, updateCase, createCase, deleteCases, csvDownload };