Files
edr-platform/apps/edr-freight-web/backoffice/src/user-management/services/api/positionService.ts
Nathnael 063a8799f5 fix(backoffice): make position type optional for unit departments
Creating a department under a unit required picking a position type,
which is not always known at that point. Sub-department creation is
unchanged and still requires one.

PositionPayload.positionTypeId becomes optional and the field is omitted
from the request body when unset rather than sent as an empty string.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 13:41:39 +00:00

166 lines
4.4 KiB
TypeScript

import { OrganizationsPositions } from "@/record-management/services/api/departmentService";
import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";
import { AxiosResponse } from "axios";
export interface PositionHeaders {
tenantKey: string;
unitId?: string;
}
export interface MovePostionPayload {
positionId: string;
newParentId: string;
}
export interface PositionPayload {
name: {
am: string;
en: string;
};
key: string;
unitId: string;
organizationId: string;
parentPositionId?: string;
projectId?: string;
positionTypeId?: string;
}
export interface PositionQueryParams {
orderBy?: string;
take?: number;
skip?: number;
order?: string;
}
export const getPositions = async (
params?: PositionQueryParams,
): Promise<AxiosResponse> => {
return axiosInstance.get("/positions", {
...{ headers: withHeaders() },
params,
});
};
export const getPositionById = async (
id: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.get(`/positions/${id}`, { headers: withHeaders() });
};
export const getPositionHierarchy = async (
id: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.get(`/positions/hierarchy/${id}`, {
headers: withHeaders(),
});
};
// Returns the direct children of a position: { count, items: PositionDto[] }
// where each item has parentPositionId === id.
export const getChildPositions = async (
id: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.get(`/positions/children/${id}`, {
headers: withHeaders(),
});
};
export const getPositionList = async (
id: string | number,
params: PositionQueryParams,
): Promise<AxiosResponse> => {
return axiosInstance.get(`/positions/list/${id}`, {
headers: withHeaders(),
params,
});
};
export const fetchPositionsByTypeAndUnit = async (
positionTypeKey: string,
unitId: string,
): Promise<OrganizationsPositions[]> => {
const { data } = await axiosInstance.get(
`/positions/list/position-type/${positionTypeKey}/unitId/${unitId}`,
{ headers: withHeaders() },
);
return Array.isArray(data) ? data : (data?.items ?? []);
};
export const fetchPositionsByRankAndUnit = async (
unitId: string,
rank: number,
): Promise<OrganizationsPositions[]> => {
const { data } = await axiosInstance.get(
`/positions/list/${unitId}/rank/${rank}`,
{
headers: withHeaders(),
},
);
if (Array.isArray(data)) return data;
if (Array.isArray(data?.items)) return data.items;
if (Array.isArray(data?.data)) return data.data;
return data?.data?.items ?? [];
};
export const getEmployeesUnderPosition = async (
positionId: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.get(`/positions/current/${positionId}/employees`, {
headers: withHeaders(),
});
};
export const createPosition = async (
data: PositionPayload,
): Promise<AxiosResponse> => {
return axiosInstance.post("/positions", data, { headers: withHeaders() });
};
export const updatePosition = async (
id: string | number,
data: PositionPayload,
): Promise<AxiosResponse> => {
return axiosInstance.put(`/positions/${id}`, data, {
headers: withHeaders(),
});
};
export const movePosition = async (
data: MovePostionPayload,
): Promise<AxiosResponse> => {
return axiosInstance.patch(`/positions/change-parent`, data, {
headers: withHeaders(),
});
};
export const deletePosition = async (
id: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.delete(`/positions/${id}`, { headers: withHeaders() });
};
export const softDeletePosition = async (
id: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.delete(`/positions/${id}/soft`, {
headers: withHeaders(),
});
};
export const getArchivedPositions = async (
parentId: string | number,
params?: PositionQueryParams,
): Promise<AxiosResponse> => {
return axiosInstance.get(`/positions/archived/${parentId}`, {
headers: withHeaders(),
params,
});
};
export const restorePosition = async (
id: string | number,
): Promise<AxiosResponse> => {
return axiosInstance.patch(`/positions/${id}/restore`, null, {
headers: withHeaders(),
});
};