Files
edr-platform/apps/edr-freight-web/portal/src/services/fileUploadSettings.service.ts
2026-05-20 08:15:29 +03:00

148 lines
3.2 KiB
TypeScript

import { api } from "./crud";
import type {
CreateFileUploadFieldDto,
CreateFileUploadSettingDto,
FileUploadField,
FileUploadSetting,
UpdateFileUploadFieldDto,
UpdateFileUploadSettingDto,
} from "@/types/fileUploadSettings";
const BASE = "/api/file-upload-settings";
/**
* Handles both:
* 1. raw payload
* 2. { data: payload }
*/
type Envelope<T> = { data: T } | T;
function unwrap<T>(payload: Envelope<T>): T {
if (
payload &&
typeof payload === "object" &&
"data" in (payload as object)
) {
return (payload as { data: T }).data;
}
return payload as T;
}
export const fileUploadSettingsService = {
// GET /file-upload-settings
list: async (): Promise<FileUploadSetting[]> => {
const response =
await api.get<Envelope<FileUploadSetting[]>>(BASE);
return unwrap(response);
},
// GET /file-upload-settings/:id
getById: async (
id: string
): Promise<FileUploadSetting> => {
const response =
await api.get<Envelope<FileUploadSetting>>(
`${BASE}/${id}`
);
return unwrap(response);
},
// GET /file-upload-settings/by-code/:code
getByCode: async (
code: string
): Promise<FileUploadSetting> => {
const response =
await api.get<Envelope<FileUploadSetting>>(
`${BASE}/by-code/${encodeURIComponent(code)}`
);
return unwrap(response);
},
// POST /file-upload-settings
create: async (
payload: CreateFileUploadSettingDto
): Promise<FileUploadSetting> => {
const response =
await api.post<Envelope<FileUploadSetting>>(
BASE,
payload
);
return unwrap(response);
},
// PATCH /file-upload-settings/:id
update: async (
id: string,
payload: UpdateFileUploadSettingDto
): Promise<FileUploadSetting> => {
const response =
await api.patch<Envelope<FileUploadSetting>>(
`${BASE}/${id}`,
payload
);
return unwrap(response);
},
// DELETE /file-upload-settings/:id
remove: async (id: string): Promise<void> => {
await api.delete(`${BASE}/${id}`);
},
// PUT /file-upload-settings/:id/fields
replaceFields: async (
id: string,
fields: CreateFileUploadFieldDto[]
): Promise<FileUploadField[]> => {
const response =
await api.put<Envelope<FileUploadField[]>>(
`${BASE}/${id}/fields`,
fields
);
return unwrap(response);
},
// POST /file-upload-settings/:id/fields
addField: async (
id: string,
payload: CreateFileUploadFieldDto
): Promise<FileUploadField> => {
const response =
await api.post<Envelope<FileUploadField>>(
`${BASE}/${id}/fields`,
payload
);
return unwrap(response);
},
// PATCH /file-upload-settings/fields/:fieldId
updateField: async (
fieldId: string,
payload: UpdateFileUploadFieldDto
): Promise<FileUploadField> => {
const response =
await api.patch<Envelope<FileUploadField>>(
`${BASE}/fields/${fieldId}`,
payload
);
return unwrap(response);
},
// DELETE /file-upload-settings/fields/:fieldId
removeField: async (
fieldId: string
): Promise<void> => {
await api.delete(
`${BASE}/fields/${fieldId}`
);
},
};