71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import { ref } from "vue";
|
|
import { defineStore } from "pinia";
|
|
import { ShiftService } from "src/modules/timesheets/services/shift-service";
|
|
import { useTimesheetStore } from "src/stores/timesheet-store";
|
|
import { Notify } from "quasar";
|
|
|
|
export const useShiftStore = defineStore('shift_store', () => {
|
|
const timesheet_store = useTimesheetStore();
|
|
const shift_error = ref();
|
|
|
|
const deleteShiftById = async (shift_id: number): Promise<boolean> => {
|
|
try {
|
|
await ShiftService.deleteShiftById(shift_id);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('DEV ERROR || error while deleting shift: ', error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const createNewShifts = async (): Promise<boolean> => {
|
|
if (timesheet_store.timesheets === undefined) return false;
|
|
|
|
try {
|
|
const new_shifts = timesheet_store.timesheets.flatMap(week => week.days).flatMap(day => day.shifts).filter(shift => shift.shift_id < 0);
|
|
|
|
if (new_shifts?.length > 0) {
|
|
const response = await ShiftService.createNewShifts(new_shifts);
|
|
if (response.status <= 200) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
console.log('No new shifts to save');
|
|
Notify.create('no new shifts to save')
|
|
return false;
|
|
} catch (error) {
|
|
console.error('Error creating new shifts: ', error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const updateShifts = async (): Promise<boolean> => {
|
|
if (timesheet_store.timesheets === undefined) return false;
|
|
|
|
try {
|
|
const existing_shifts = timesheet_store.timesheets.flatMap(week => week.days).flatMap(day => day.shifts).filter(shift => shift.shift_id > 0);
|
|
|
|
if (existing_shifts?.length > 0) {
|
|
const response = await ShiftService.updateShifts(existing_shifts);
|
|
if (response.status <= 200) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
console.log('No shifts to update');
|
|
Notify.create('no shifts to update')
|
|
return false;
|
|
} catch (error) {
|
|
console.error('Error updating shifts: ', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return {
|
|
shift_error,
|
|
deleteShiftById,
|
|
createNewShifts,
|
|
updateShifts,
|
|
}
|
|
}) |