feat: create Daily Checklist API Service

This commit is contained in:
ValdiANS
2026-01-09 15:30:37 +07:00
parent 6dc93b1065
commit f60a07bc5b
@@ -0,0 +1,200 @@
import axios from 'axios';
import { BaseApiService } from '@/services/api/base';
import { httpClient } from '@/services/http/client';
import { BaseApiResponse } from '@/types/api/api-general';
import {
CreateDailyChecklistPayload,
DailyChecklist,
DetailDailyChecklist,
} from '@/types/api/daily-checklist/daily-checklist';
export class DailyChecklistApiService extends BaseApiService<
DailyChecklist,
CreateDailyChecklistPayload,
unknown
> {
constructor(basePath: string = '/daily-checklists') {
super(basePath);
}
async getOneDailyChecklist(id: string) {
try {
const getOneDailyChecklistPath = `${this.basePath}/relation/${id}`;
const getOneDailyChecklistRes = await httpClient<
BaseApiResponse<DetailDailyChecklist>
>(getOneDailyChecklistPath);
return getOneDailyChecklistRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse<DetailDailyChecklist>>(error)) {
return error.response?.data;
}
return undefined;
}
}
async setDailyChecklistPhase(id: string, phaseIds: string[]) {
try {
const setDailyChecklistPhasePath = `${this.basePath}/phase/${id}`;
const setDailyChecklistPhaseRes = await httpClient<BaseApiResponse>(
setDailyChecklistPhasePath,
{
method: 'POST',
body: { phase_ids: phaseIds.join(',') },
}
);
return setDailyChecklistPhaseRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async removeEmployeeAssignment(id: string, employeeId: string) {
try {
const removeEmployeeAssignmentPath = `${this.basePath}/${id}/assignments/${employeeId}`;
const removeEmployeeAssignmentRes = await httpClient<BaseApiResponse>(
removeEmployeeAssignmentPath,
{
method: 'DELETE',
}
);
return removeEmployeeAssignmentRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async setDailyChecklistEmployees(checklistId: string, employeeIds: string[]) {
try {
const setDailyChecklistPhasePath = `${this.basePath}/assignment/${checklistId}`;
const setDailyChecklistPhaseRes = await httpClient<BaseApiResponse>(
setDailyChecklistPhasePath,
{
method: 'POST',
body: { employee_ids: employeeIds.join(',') },
}
);
return setDailyChecklistPhaseRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async getTasksByChecklistId(id: string) {
try {
const getTasksByChecklistIdPath = `${this.basePath}/tasks?checklist_id=${id}&page=1&limit=100`;
const getTasksByChecklistIdRes = await httpClient<BaseApiResponse>(
getTasksByChecklistIdPath
);
return getTasksByChecklistIdRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async checkOrUncheckAssignment(payload: {
task_id: number;
employee_id: number;
checked: boolean;
note: string | null;
}) {
try {
const checkOrUncheckAssignmentPath = `${this.basePath}/assignment`;
const checkOrUncheckAssignmentRes = await httpClient<BaseApiResponse>(
checkOrUncheckAssignmentPath,
{
method: 'POST',
body: payload,
}
);
return checkOrUncheckAssignmentRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async submit(id: string) {
try {
const submitPath = `${this.basePath}/${id}`;
const submitRes = await httpClient<BaseApiResponse>(submitPath, {
method: 'PATCH',
body: {
status: 'SUBMITTED',
reject_reason: '',
},
});
return submitRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async approve(id: string) {
try {
const approvePath = `${this.basePath}/${id}`;
const approveRes = await httpClient<BaseApiResponse>(approvePath, {
method: 'PATCH',
body: {
status: 'APPROVED',
reject_reason: '',
},
});
return approveRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
async reject(id: string, rejectReason: string) {
try {
const rejectPath = `${this.basePath}/${id}`;
const rejectRes = await httpClient<BaseApiResponse>(rejectPath, {
method: 'PATCH',
body: {
status: 'REJECTED',
reject_reason: rejectReason,
},
});
return rejectRes;
} catch (error) {
if (axios.isAxiosError<BaseApiResponse>(error)) {
return error.response?.data;
}
return undefined;
}
}
}
export const DailyChecklistApi = new DailyChecklistApiService(
'/daily-checklists'
);