378 lines
10 KiB
TypeScript
378 lines
10 KiB
TypeScript
import type {
|
|
CircuitListDto,
|
|
CreateFloorInput,
|
|
CreateProjectDeviceInput,
|
|
CreateRoomInput,
|
|
ConsumerWithCalculatedValues,
|
|
CreateConsumerInput,
|
|
CreateGlobalDeviceInput,
|
|
DistributionBoardDto,
|
|
FloorDto,
|
|
GlobalDeviceDto,
|
|
ProjectDeviceDto,
|
|
ProjectDeviceDisconnectResultDto,
|
|
ProjectDeviceSyncPreviewDto,
|
|
ProjectDeviceSyncRestoreRowDto,
|
|
ProjectDeviceSyncResultDto,
|
|
ProjectDto,
|
|
RoomDto,
|
|
UpdateConsumerInput,
|
|
CircuitTreeResponseDto,
|
|
CircuitTreeCircuitDto,
|
|
CircuitTreeDeviceRowDto,
|
|
CreateCircuitInputDto,
|
|
UpdateCircuitInputDto,
|
|
CreateCircuitDeviceRowInputDto,
|
|
UpdateCircuitDeviceRowInputDto,
|
|
} from "../types";
|
|
import type { ProjectDeviceSyncField } from "../../shared/constants/project-device-sync-fields";
|
|
|
|
async function request<T>(url: string, init?: RequestInit): Promise<T> {
|
|
const response = await fetch(url, {
|
|
...init,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...init?.headers,
|
|
},
|
|
cache: "no-store",
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const details = await response.text();
|
|
throw new Error(details || `Anfrage fehlgeschlagen (Status ${response.status})`);
|
|
}
|
|
|
|
if (response.status === 204) {
|
|
return undefined as T;
|
|
}
|
|
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
export function listProjects() {
|
|
return request<ProjectDto[]>("/api/projects");
|
|
}
|
|
|
|
export function getProject(projectId: string) {
|
|
return request<ProjectDto>(`/api/projects/${projectId}`);
|
|
}
|
|
|
|
export function createProject(name: string) {
|
|
return request<ProjectDto>("/api/projects", {
|
|
method: "POST",
|
|
body: JSON.stringify({ name }),
|
|
});
|
|
}
|
|
|
|
export function updateProjectSettings(
|
|
projectId: string,
|
|
input: { singlePhaseVoltageV: number; threePhaseVoltageV: number }
|
|
) {
|
|
return request<ProjectDto>(`/api/projects/${projectId}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function listDistributionBoards(projectId: string) {
|
|
return request<DistributionBoardDto[]>(`/api/projects/${projectId}/distribution-boards`);
|
|
}
|
|
|
|
export function createDistributionBoard(projectId: string, name: string) {
|
|
return request<DistributionBoardDto>(`/api/projects/${projectId}/distribution-boards`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ name }),
|
|
});
|
|
}
|
|
|
|
export function listCircuitLists(projectId: string) {
|
|
return request<CircuitListDto[]>(`/api/projects/${projectId}/circuit-lists`);
|
|
}
|
|
|
|
export function getCircuitTree(projectId: string, circuitListId: string) {
|
|
return request<CircuitTreeResponseDto>(`/api/projects/${projectId}/circuit-lists/${circuitListId}/tree`);
|
|
}
|
|
|
|
export function createCircuit(projectId: string, circuitListId: string, input: CreateCircuitInputDto) {
|
|
return request(`/api/projects/${projectId}/circuit-lists/${circuitListId}/circuits`, {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function createCircuitWithDeviceRows(
|
|
projectId: string,
|
|
circuitListId: string,
|
|
input: {
|
|
circuit: CreateCircuitInputDto;
|
|
deviceRows: CreateCircuitDeviceRowInputDto[];
|
|
}
|
|
) {
|
|
return request<{
|
|
circuit: CircuitTreeCircuitDto;
|
|
deviceRows: CircuitTreeDeviceRowDto[];
|
|
}>(
|
|
`/api/projects/${projectId}/circuit-lists/${circuitListId}/circuits-with-device-rows`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
}
|
|
);
|
|
}
|
|
|
|
export function updateCircuitById(circuitId: string, input: UpdateCircuitInputDto) {
|
|
return request(`/api/circuits/${circuitId}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function deleteCircuitById(circuitId: string) {
|
|
return request<void>(`/api/circuits/${circuitId}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
|
|
export function getNextCircuitIdentifier(sectionId: string) {
|
|
return request<{ sectionId: string; nextIdentifier: string }>(
|
|
`/api/circuit-sections/${sectionId}/next-identifier`
|
|
);
|
|
}
|
|
|
|
export function createCircuitDeviceRow(circuitId: string, input: CreateCircuitDeviceRowInputDto) {
|
|
return request(`/api/circuits/${circuitId}/device-rows`, {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function updateCircuitDeviceRowById(rowId: string, input: UpdateCircuitDeviceRowInputDto) {
|
|
return request(`/api/circuit-device-rows/${rowId}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function deleteCircuitDeviceRowById(rowId: string) {
|
|
return request<void>(`/api/circuit-device-rows/${rowId}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
|
|
export function moveCircuitDeviceRowById(
|
|
rowId: string,
|
|
input: { targetCircuitId?: string; targetSectionId?: string; createNewCircuit?: boolean }
|
|
) {
|
|
return request(`/api/circuit-device-rows/${rowId}/move`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function moveCircuitDeviceRowsBulk(input: {
|
|
rowIds: string[];
|
|
targetCircuitId?: string;
|
|
targetSectionId?: string;
|
|
createNewCircuit?: boolean;
|
|
}) {
|
|
return request<{ movedRowIds: string[]; targetCircuitId: string; createdCircuitId?: string }>(
|
|
"/api/circuit-device-rows/move-bulk",
|
|
{
|
|
method: "PATCH",
|
|
body: JSON.stringify(input),
|
|
}
|
|
);
|
|
}
|
|
|
|
export function renumberCircuitSection(sectionId: string) {
|
|
return request(`/api/circuit-sections/${sectionId}/renumber`, {
|
|
method: "POST",
|
|
});
|
|
}
|
|
|
|
export function reorderSectionCircuits(sectionId: string, orderedCircuitIds: string[]) {
|
|
return request(`/api/circuit-sections/${sectionId}/circuits/reorder`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify({ orderedCircuitIds }),
|
|
});
|
|
}
|
|
|
|
export function updateSectionEquipmentIdentifiers(
|
|
sectionId: string,
|
|
identifiers: Array<{ circuitId: string; equipmentIdentifier: string }>
|
|
) {
|
|
return request(`/api/circuit-sections/${sectionId}/equipment-identifiers`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify({ identifiers }),
|
|
});
|
|
}
|
|
|
|
export function listFloors(projectId: string) {
|
|
return request<FloorDto[]>(`/api/projects/${projectId}/floors`);
|
|
}
|
|
|
|
export function createFloor(projectId: string, input: CreateFloorInput) {
|
|
return request<FloorDto>(`/api/projects/${projectId}/floors`, {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function listRooms(projectId: string) {
|
|
return request<RoomDto[]>(`/api/projects/${projectId}/rooms`);
|
|
}
|
|
|
|
export function createRoom(projectId: string, input: CreateRoomInput) {
|
|
return request<RoomDto>(`/api/projects/${projectId}/rooms`, {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function listConsumers(projectId: string) {
|
|
return request<ConsumerWithCalculatedValues[]>(`/api/consumers/projects/${projectId}`);
|
|
}
|
|
|
|
export function createConsumer(input: CreateConsumerInput) {
|
|
return request<ConsumerWithCalculatedValues>("/api/consumers", {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function updateConsumer(consumerId: string, input: UpdateConsumerInput) {
|
|
return request<ConsumerWithCalculatedValues>(`/api/consumers/${consumerId}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function deleteConsumer(consumerId: string) {
|
|
return request<void>(`/api/consumers/${consumerId}`, { method: "DELETE" });
|
|
}
|
|
|
|
export function listGlobalDevices() {
|
|
return request<GlobalDeviceDto[]>("/api/global-devices");
|
|
}
|
|
|
|
export function createGlobalDevice(input: CreateGlobalDeviceInput) {
|
|
return request<GlobalDeviceDto>("/api/global-devices", {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function updateGlobalDevice(globalDeviceId: string, input: CreateGlobalDeviceInput) {
|
|
return request<GlobalDeviceDto>(`/api/global-devices/${globalDeviceId}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function deleteGlobalDevice(globalDeviceId: string) {
|
|
return request<void>(`/api/global-devices/${globalDeviceId}`, { method: "DELETE" });
|
|
}
|
|
|
|
export function copyProjectDeviceToGlobal(projectId: string, projectDeviceId: string) {
|
|
return request<GlobalDeviceDto>(`/api/global-devices/import-project/${projectId}/${projectDeviceId}`, {
|
|
method: "POST",
|
|
});
|
|
}
|
|
|
|
export function listProjectDevices(projectId: string) {
|
|
return request<ProjectDeviceDto[]>(`/api/project-devices/projects/${projectId}`);
|
|
}
|
|
|
|
export function createProjectDevice(projectId: string, input: CreateProjectDeviceInput) {
|
|
return request<ProjectDeviceDto>(`/api/project-devices/projects/${projectId}`, {
|
|
method: "POST",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function updateProjectDevice(
|
|
projectId: string,
|
|
projectDeviceId: string,
|
|
input: CreateProjectDeviceInput
|
|
) {
|
|
return request<ProjectDeviceDto>(`/api/project-devices/projects/${projectId}/${projectDeviceId}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify(input),
|
|
});
|
|
}
|
|
|
|
export function deleteProjectDevice(projectId: string, projectDeviceId: string) {
|
|
return request<void>(`/api/project-devices/projects/${projectId}/${projectDeviceId}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
|
|
export function copyGlobalDeviceToProject(projectId: string, globalDeviceId: string) {
|
|
return request<ProjectDeviceDto>(`/api/project-devices/projects/${projectId}/import-global/${globalDeviceId}`, {
|
|
method: "POST",
|
|
});
|
|
}
|
|
|
|
export function getProjectDeviceSyncPreview(projectId: string, projectDeviceId: string) {
|
|
return request<ProjectDeviceSyncPreviewDto>(
|
|
`/api/project-devices/projects/${projectId}/${projectDeviceId}/links`
|
|
);
|
|
}
|
|
|
|
export function synchronizeProjectDeviceRows(
|
|
projectId: string,
|
|
projectDeviceId: string,
|
|
rowIds: string[],
|
|
fields: ProjectDeviceSyncField[]
|
|
) {
|
|
return request<ProjectDeviceSyncResultDto>(
|
|
`/api/project-devices/projects/${projectId}/${projectDeviceId}/synchronize`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({ rowIds, fields }),
|
|
}
|
|
);
|
|
}
|
|
|
|
export function disconnectProjectDeviceRows(
|
|
projectId: string,
|
|
projectDeviceId: string,
|
|
rowIds: string[]
|
|
) {
|
|
return request<ProjectDeviceDisconnectResultDto>(
|
|
`/api/project-devices/projects/${projectId}/${projectDeviceId}/disconnect`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({ rowIds }),
|
|
}
|
|
);
|
|
}
|
|
|
|
export function restoreProjectDeviceRows(
|
|
projectId: string,
|
|
projectDeviceId: string,
|
|
rows: ProjectDeviceSyncRestoreRowDto[]
|
|
) {
|
|
return request<ProjectDeviceSyncPreviewDto>(
|
|
`/api/project-devices/projects/${projectId}/${projectDeviceId}/restore`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({ rows }),
|
|
}
|
|
);
|
|
}
|
|
|
|
export function reconnectProjectDeviceRows(
|
|
projectId: string,
|
|
projectDeviceId: string,
|
|
rowIds: string[]
|
|
) {
|
|
return request<ProjectDeviceSyncPreviewDto>(
|
|
`/api/project-devices/projects/${projectId}/${projectDeviceId}/reconnect`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({ rowIds }),
|
|
}
|
|
);
|
|
}
|