diff --git a/src/services/api/daily-checklist/daily-checklist.ts b/src/services/api/daily-checklist/daily-checklist.ts new file mode 100644 index 00000000..48b789a8 --- /dev/null +++ b/src/services/api/daily-checklist/daily-checklist.ts @@ -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 + >(getOneDailyChecklistPath); + + return getOneDailyChecklistRes; + } catch (error) { + if (axios.isAxiosError>(error)) { + return error.response?.data; + } + return undefined; + } + } + + async setDailyChecklistPhase(id: string, phaseIds: string[]) { + try { + const setDailyChecklistPhasePath = `${this.basePath}/phase/${id}`; + const setDailyChecklistPhaseRes = await httpClient( + setDailyChecklistPhasePath, + { + method: 'POST', + body: { phase_ids: phaseIds.join(',') }, + } + ); + + return setDailyChecklistPhaseRes; + } catch (error) { + if (axios.isAxiosError(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( + removeEmployeeAssignmentPath, + { + method: 'DELETE', + } + ); + + return removeEmployeeAssignmentRes; + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + } + + async setDailyChecklistEmployees(checklistId: string, employeeIds: string[]) { + try { + const setDailyChecklistPhasePath = `${this.basePath}/assignment/${checklistId}`; + const setDailyChecklistPhaseRes = await httpClient( + setDailyChecklistPhasePath, + { + method: 'POST', + body: { employee_ids: employeeIds.join(',') }, + } + ); + + return setDailyChecklistPhaseRes; + } catch (error) { + if (axios.isAxiosError(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( + getTasksByChecklistIdPath + ); + + return getTasksByChecklistIdRes; + } catch (error) { + if (axios.isAxiosError(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( + checkOrUncheckAssignmentPath, + { + method: 'POST', + body: payload, + } + ); + + return checkOrUncheckAssignmentRes; + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + } + + async submit(id: string) { + try { + const submitPath = `${this.basePath}/${id}`; + const submitRes = await httpClient(submitPath, { + method: 'PATCH', + body: { + status: 'SUBMITTED', + reject_reason: '', + }, + }); + + return submitRes; + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + } + + async approve(id: string) { + try { + const approvePath = `${this.basePath}/${id}`; + const approveRes = await httpClient(approvePath, { + method: 'PATCH', + body: { + status: 'APPROVED', + reject_reason: '', + }, + }); + + return approveRes; + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + } + + async reject(id: string, rejectReason: string) { + try { + const rejectPath = `${this.basePath}/${id}`; + const rejectRes = await httpClient(rejectPath, { + method: 'PATCH', + body: { + status: 'REJECTED', + reject_reason: rejectReason, + }, + }); + + return rejectRes; + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + } +} + +export const DailyChecklistApi = new DailyChecklistApiService( + '/daily-checklists' +);