targo-frontend/src/modules/timesheets/composables/api/use-timesheet-api.ts

58 lines
1.8 KiB
TypeScript

import { useAuthStore } from "src/stores/auth-store";
import { useTimesheetStore } from "src/stores/timesheet-store"
/* eslint-disable */
export const useTimesheetApi = () => {
const timesheet_store = useTimesheetStore();
const auth_store = useAuthStore();
const getTimesheetsByDate = async (date_string: string) => {
const success = await timesheet_store.getPayPeriodByDate(date_string);
if (success) {
await timesheet_store.getTimesheetsByPayPeriodAndEmail(auth_store.user.email)
}
}
const fetchPayPeriod = async (direction: number) => {
const current_pay_period = timesheet_store.current_pay_period;
let new_pay_period_no = current_pay_period.pay_period_no + direction;
let new_pay_year = current_pay_period.pay_year;
if (new_pay_period_no > 26) {
new_pay_period_no = 1;
new_pay_year += 1;
}
if (new_pay_period_no < 1) {
new_pay_period_no = 26;
new_pay_year -= 1;
}
const success = await timesheet_store.getPayPeriodByYearAndPeriodNumber(new_pay_year, new_pay_period_no);
if (success) {
await timesheet_store.getTimesheetsByPayPeriodAndEmail(auth_store.user.email);
}
};
const getNextPayPeriod = async () => fetchPayPeriod(1);
const getPreviousPayPeriod = async () => fetchPayPeriod(-1);
const getPreviousPeriodForUser = async (_employee_email: string) => {
await getPreviousPayPeriod();
};
const getNextPeriodForUser = async (_employee_email: string) => {
await getNextPayPeriod();
};
return {
getTimesheetsByDate,
fetchPayPeriod,
// getCurrentPayPeriod,
getNextPayPeriod,
getPreviousPayPeriod,
getPreviousPeriodForUser,
getNextPeriodForUser,
};
};