88 lines
3.1 KiB
TypeScript
88 lines
3.1 KiB
TypeScript
// File: lib/services/project.service.ts
|
|
import apiClient from "@/lib/api/client";
|
|
import {
|
|
CreateProjectDto,
|
|
UpdateProjectDto,
|
|
SearchProjectDto
|
|
} from "@/types/dto/project/project.dto";
|
|
|
|
export const projectService = {
|
|
// --- Basic CRUD ---
|
|
|
|
/**
|
|
* ดึงรายการโครงการทั้งหมด (รองรับ Search & Pagination)
|
|
* (เดิมคือ getAllProjects แต่ปรับให้รับ params ได้)
|
|
*/
|
|
getAll: async (params?: SearchProjectDto) => {
|
|
// GET /projects
|
|
const response = await apiClient.get("/projects", { params });
|
|
// Handle paginated response
|
|
if (response.data && Array.isArray(response.data.data)) {
|
|
return response.data.data;
|
|
}
|
|
return response.data;
|
|
},
|
|
|
|
/** ดึงรายละเอียดโครงการตาม ID */
|
|
getById: async (id: string | number) => {
|
|
const response = await apiClient.get(`/projects/${id}`);
|
|
return response.data;
|
|
},
|
|
|
|
/** สร้างโครงการใหม่ (Admin) */
|
|
create: async (data: CreateProjectDto) => {
|
|
const response = await apiClient.post("/projects", data);
|
|
return response.data;
|
|
},
|
|
|
|
/** แก้ไขโครงการ */
|
|
update: async (id: string | number, data: UpdateProjectDto) => {
|
|
const response = await apiClient.put(`/projects/${id}`, data);
|
|
return response.data;
|
|
},
|
|
|
|
/** ลบโครงการ (Soft Delete) */
|
|
delete: async (id: string | number) => {
|
|
const response = await apiClient.delete(`/projects/${id}`);
|
|
return response.data;
|
|
},
|
|
|
|
// --- Related Data / Dropdown Helpers ---
|
|
|
|
/** * ดึงรายชื่อองค์กรในโครงการ (สำหรับ Dropdown 'To/From')
|
|
* GET /projects/:id/organizations
|
|
*/
|
|
getOrganizations: async (projectId: string | number) => {
|
|
const response = await apiClient.get(`/projects/${projectId}/organizations`);
|
|
// Unwrap the response data if it's wrapped in a 'data' property by the interceptor
|
|
return response.data.data || response.data;
|
|
},
|
|
|
|
/** * ดึงรายชื่อสัญญาในโครงการ
|
|
* GET /projects/:id/contracts
|
|
*/
|
|
/** * ดึงรายชื่อสัญญาในโครงการ (Legacy/Specific)
|
|
* GET /projects/:id/contracts
|
|
*/
|
|
getContracts: async (projectId: string | number) => {
|
|
// Note: If backend doesn't have /projects/:id/contracts, use /contracts?projectId=:id
|
|
const response = await apiClient.get(`/contracts`, { params: { projectId } });
|
|
// Handle paginated response
|
|
if (response.data && Array.isArray(response.data.data)) {
|
|
return response.data.data;
|
|
}
|
|
return response.data.data || response.data;
|
|
},
|
|
|
|
/**
|
|
* ดึงรายการสัญญาเรื้งหมด (Global Search)
|
|
*/
|
|
getAllContracts: async (params?: any) => {
|
|
const response = await apiClient.get("/contracts", { params });
|
|
if (response.data && Array.isArray(response.data.data)) {
|
|
return response.data.data;
|
|
}
|
|
return response.data.data || response.data;
|
|
}
|
|
};
|