Files
Caramel-CRM/resources/js/services/LicenseService.ts
T

126 lines
4.2 KiB
TypeScript

import axios, { AxiosError } from 'axios';
import { License } from '@/types';
import { toast } from 'vue-sonner';
const API_URL = '/api/licenses';
export default {
/**
* Fetches all licenses
* @returns Promise<License[] | null>
*/
async getLicenses(): Promise<License[] | null> {
try {
const response = await axios.get(API_URL);
return response.data;
} catch (error) {
toast.error('Fehler beim Abrufen der Lizenzen', { description: (error as AxiosError).message });
console.error(error);
return null;
}
},
/**
* Fetches a single license by ID
* @param id - The ID of the license to fetch
* @returns Promise<License | null>
*/
async getLicense(id: number): Promise<License | null> {
try {
const response = await axios.get(`${API_URL}/${id}`);
return response.data;
} catch (error) {
toast.error('Fehler beim Abrufen der Lizenz', { description: (error as AxiosError).message });
console.error(error);
return null;
}
},
/**
* Creates a new license
* @param license - The license data to create
* @returns Promise<License | null>
*/
async createLicense(license: License): Promise<License | null> {
try {
const response = await axios.post(API_URL, license);
return response.data;
} catch (error) {
toast.error('Fehler beim Erstellen der Lizenz', { description: (error as AxiosError).message });
console.error(error);
return null;
}
},
/**
* Updates an existing license
* @param license - The license data to update
* @returns Promise<License | null>
*/
async updateLicense(license: License): Promise<License | null> {
try {
const response = await axios.put(`${API_URL}/${license.id}`, license);
return response.data;
} catch (error) {
toast.error('Fehler beim Aktualisieren der Lizenz', { description: (error as AxiosError).message });
console.error(error);
return null;
}
},
/**
* Deletes a license by ID
* @param id - The ID of the license to delete
* @returns Promise<boolean>
*/
async deleteLicense(id: number): Promise<boolean> {
try {
await axios.delete(`${API_URL}/${id}`);
return true;
} catch (error) {
toast.error('Fehler beim Löschen der Lizenz', { description: (error as AxiosError).message });
console.error(error);
return false;
}
},
/**
* Downloads a license as a JSON file
* @param id - The ID of the license to download
* @returns Promise<void>
*/
async downloadLicense(id: number): Promise<void> {
try {
const response = await axios.get(`/licenses/download/${id}`, {
responseType: 'blob',
});
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `license_${id}.json`);
document.body.appendChild(link);
link.click();
link.remove();
} catch (error) {
toast.error('Fehler beim Herunterladen der Lizenz', { description: (error as AxiosError).message });
console.error(error);
}
},
/**
* Validates a license
* @param licenseId - The ID of the license to validate
* @returns Promise<{ is_valid: boolean, is_signature_valid: boolean, is_expired: boolean } | null>
*/
async validateLicense(licenseId: number): Promise<{ is_valid: boolean, is_signature_valid: boolean, is_expired: boolean } | null> {
try {
const response = await axios.post('/api/licenses/validate', { license_id: licenseId });
return response.data;
} catch (error) {
toast.error('Fehler beim Validieren der Lizenz', { description: (error as AxiosError).message });
console.error(error);
return null;
}
}
};