New-Thanasoft/thanas
2025-11-05 17:09:12 +03:00

371 lines
8.6 KiB
Plaintext

import { defineStore } from "pinia";
import { ref, computed } from "vue";
import EmployeeService from "@/services/employee";
import type {
Employee,
CreateEmployeePayload,
UpdateEmployeePayload,
EmployeeListResponse,
} from "@/services/employee";
export const useEmployeeStore = defineStore("employee", () => {
// State
const employees = ref<Employee[]>([]);
const currentEmployee = ref<Employee | null>(null);
const loading = ref(false);
const error = ref<string | null>(null);
const searchResults = ref<Employee[]>([]);
// Pagination state
const pagination = ref({
current_page: 1,
last_page: 1,
per_page: 10,
total: 0,
from: 0,
to: 0,
});
// Getters
const allEmployees = computed(() => employees.value);
const activeEmployees = computed(() =>
employees.value.filter((employee) => employee.is_active)
);
const inactiveEmployees = computed(() =>
employees.value.filter((employee) => !employee.is_active)
);
const isLoading = computed(() => loading.value);
const hasError = computed(() => error.value !== null);
const getError = computed(() => error.value);
const getEmployeeById = computed(
() => (id: number) => employees.value.find((employee) => employee.id === id)
);
const getPagination = computed(() => pagination.value);
// Actions
const setLoading = (isLoading: boolean) => {
loading.value = isLoading;
};
const setError = (err: string | null) => {
error.value = err;
};
const clearError = () => {
error.value = null;
};
const setEmployees = (newEmployees: Employee[]) => {
employees.value = newEmployees;
};
const setCurrentEmployee = (employee: Employee | null) => {
currentEmployee.value = employee;
};
const setSearchEmployee = (searchEmployee: Employee[]) => {
searchResults.value = searchEmployee;
};
const setPagination = (meta: any) => {
if (meta) {
pagination.value = {
current_page: meta.current_page || 1,
last_page: meta.last_page || 1,
per_page: meta.per_page || 10,
total: meta.total || 0,
from: meta.from || 0,
to: meta.to || 0,
};
}
};
/**
* Fetch all employees with optional pagination and filters
*/
const fetchEmployees = async (params?: {
page?: number;
per_page?: number;
search?: string;
active?: boolean;
sort_by?: string;
sort_direction?: string;
}) => {
setLoading(true);
setError(null);
try {
const response = await EmployeeService.getAllEmployees(params);
setEmployees(response.data);
setPagination(response.pagination);
return response;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to fetch employees";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Fetch a single employee by ID
*/
const fetchEmployee = async (id: number) => {
setLoading(true);
setError(null);
try {
const response = await EmployeeService.getEmployee(id);
setCurrentEmployee(response.data);
return response.data;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to fetch employee";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Create a new employee
*/
const createEmployee = async (payload: CreateEmployeePayload) => {
setLoading(true);
setError(null);
try {
const response = await EmployeeService.createEmployee(payload);
// Add the new employee to the list
employees.value.push(response.data);
setCurrentEmployee(response.data);
return response.data;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to create employee";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Update an existing employee
*/
const updateEmployee = async (payload: UpdateEmployeePayload) => {
setLoading(true);
setError(null);
try {
console.log(payload);
const response = await EmployeeService.updateEmployee(payload);
const updatedEmployee = response.data;
// Update in the employees list
const index = employees.value.findIndex(
(employee) => employee.id === updatedEmployee.id
);
if (index !== -1) {
employees.value[index] = updatedEmployee;
}
// Update current employee if it's the one being edited
if (
currentEmployee.value &&
currentEmployee.value.id === updatedEmployee.id
) {
setCurrentEmployee(updatedEmployee);
}
return updatedEmployee;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to update employee";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Delete an employee
*/
const deleteEmployee = async (id: number) => {
setLoading(true);
setError(null);
try {
const response = await EmployeeService.deleteEmployee(id);
// Remove from the employees list
employees.value = employees.value.filter(
(employee) => employee.id !== id
);
// Clear current employee if it's the one being deleted
if (currentEmployee.value && currentEmployee.value.id === id) {
setCurrentEmployee(null);
}
return response;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to delete employee";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Search employees
*/
const searchEmployees = async (query: string) => {
setLoading(true);
error.value = null;
try {
const results = await EmployeeService.searchEmployees(query);
setSearchEmployee(results);
return results;
} catch (err) {
error.value = "Erreur lors de la recherche des employés";
console.error("Error searching employees:", err);
setSearchEmployee([]);
throw err;
} finally {
setLoading(false);
}
};
/**
* Toggle employee active status
*/
const toggleEmployeeStatus = async (id: number, isActive: boolean) => {
setLoading(true);
setError(null);
try {
const response = await EmployeeService.toggleEmployeeStatus(id, isActive);
const updatedEmployee = response.data;
// Update in the employees list
const index = employees.value.findIndex((employee) => employee.id === id);
if (index !== -1) {
employees.value[index] = updatedEmployee;
}
// Update current employee if it's the one being toggled
if (currentEmployee.value && currentEmployee.value.id === id) {
setCurrentEmployee(updatedEmployee);
}
return updatedEmployee;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to toggle employee status";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Get employee statistics
*/
const fetchStatistics = async () => {
setLoading(true);
setError(null);
try {
const response = await EmployeeService.getStatistics();
return response.data;
} catch (err: any) {
const errorMessage =
err.response?.data?.message ||
err.message ||
"Failed to fetch statistics";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Clear current employee
*/
const clearCurrentEmployee = () => {
setCurrentEmployee(null);
};
/**
* Clear all state
*/
const clearStore = () => {
employees.value = [];
currentEmployee.value = null;
error.value = null;
pagination.value = {
current_page: 1,
last_page: 1,
per_page: 10,
total: 0,
from: 0,
to: 0,
};
};
return {
// State
employees,
currentEmployee,
loading,
error,
searchResults,
// Getters
allEmployees,
activeEmployees,
inactiveEmployees,
isLoading,
hasError,
getError,
getEmployeeById,
getPagination,
// Actions
fetchEmployees,
fetchEmployee,
createEmployee,
updateEmployee,
deleteEmployee,
searchEmployees,
toggleEmployeeStatus,
fetchStatistics,
clearCurrentEmployee,
clearStore,
clearError,
};
});