84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import apiClient from "@/lib/api/client";
|
|
import {
|
|
CreateOrganizationDto,
|
|
UpdateOrganizationDto,
|
|
SearchOrganizationDto,
|
|
} from "@/types/dto/organization/organization.dto";
|
|
import { Organization } from "@/types/organization";
|
|
|
|
type ApiEnvelope<T> = {
|
|
data?: ApiEnvelope<T> | T;
|
|
message?: string;
|
|
statusCode?: number;
|
|
};
|
|
|
|
function unwrapApiData<T>(payload: ApiEnvelope<T> | T): ApiEnvelope<T> | T | null {
|
|
let current: ApiEnvelope<T> | T | null = payload;
|
|
|
|
while (
|
|
current &&
|
|
typeof current === "object" &&
|
|
"data" in current &&
|
|
current.data !== undefined
|
|
) {
|
|
current = current.data;
|
|
}
|
|
|
|
return current;
|
|
}
|
|
|
|
function unwrapArrayResponse<T>(payload: ApiEnvelope<T[]> | T[]): T[] {
|
|
const unwrapped = unwrapApiData(payload);
|
|
return Array.isArray(unwrapped) ? unwrapped : [];
|
|
}
|
|
|
|
export const organizationService = {
|
|
/**
|
|
* Get all organizations (supports filtering by projectId)
|
|
* GET /organizations?projectId=1
|
|
*/
|
|
getAll: async (params?: SearchOrganizationDto): Promise<Organization[]> => {
|
|
const response = await apiClient.get<ApiEnvelope<Organization[]> | Organization[]>(
|
|
"/organizations",
|
|
{ params }
|
|
);
|
|
return unwrapArrayResponse(response.data);
|
|
},
|
|
|
|
/**
|
|
* Get organization by UUID
|
|
* GET /organizations/:uuid
|
|
*/
|
|
getByUuid: async (uuid: string) => {
|
|
const response = await apiClient.get(`/organizations/${uuid}`);
|
|
return response.data;
|
|
},
|
|
|
|
/**
|
|
* Create new organization
|
|
* POST /organizations
|
|
*/
|
|
create: async (data: CreateOrganizationDto) => {
|
|
const response = await apiClient.post("/organizations", data);
|
|
return response.data;
|
|
},
|
|
|
|
/**
|
|
* Update organization
|
|
* PATCH /organizations/:uuid
|
|
*/
|
|
update: async (uuid: string, data: UpdateOrganizationDto) => {
|
|
const response = await apiClient.patch(`/organizations/${uuid}`, data);
|
|
return response.data;
|
|
},
|
|
|
|
/**
|
|
* Delete organization
|
|
* DELETE /organizations/:uuid
|
|
*/
|
|
delete: async (uuid: string) => {
|
|
const response = await apiClient.delete(`/organizations/${uuid}`);
|
|
return response.data;
|
|
},
|
|
};
|