metis-desk/modules/companies/src/web/telework/telework-api.ts
David 2382e0ea20 feat: add telework authorization management functionality
- Implemented RESTful API routes for telework authorizations, including GET and POST methods for retrieving and saving authorizations.
- Created Sequelize repository for managing telework authorization data with methods for loading and saving authorizations.
- Developed migration scripts to set up necessary database tables for telework authorizations and revisions.
- Added persistence mapping for telework authorization entities to facilitate data transformation between domain and database layers.
- Introduced common schemas for validating telework authorization requests and responses using Zod.
- Built frontend components for managing telework authorizations, including forms for creating and editing authorizations, and summaries for displaying authorization details.
- Integrated internationalization support for English and Spanish languages in the telework module.
- Added unit tests to ensure functionality and correctness of telework authorization components and API interactions.
2026-10-06 18:10:35 +02:00

44 lines
1.4 KiB
TypeScript

import { z } from "zod/v4";
import {
type TeleworkAuthorizationInput,
TeleworkAuthorizationResponseSchema,
TeleworkAuthorizationsResponseSchema,
} from "../../common/telework-authorizations";
export const teleworkKeys = { company: (id: string) => ["telework-authorizations", id] };
export class TeleworkApiError extends Error {
constructor(public readonly code: string) {
super(code);
}
}
async function request(
companyId: string,
values?: TeleworkAuthorizationInput,
token?: string,
id?: string
) {
const response = await fetch(
`/api/telework-authorizations/${companyId}${id ? `/${id}/update` : ""}`,
{
method: values ? "POST" : "GET",
credentials: "same-origin",
headers: values ? { "content-type": "application/json", "x-csrf-token": token ?? "" } : {},
...(values ? { body: JSON.stringify({ values }) } : {}),
}
);
const body: unknown = await response.json();
if (!response.ok) {
const error = z.object({ code: z.string() }).safeParse(body);
throw new TeleworkApiError(error.success ? error.data.code : "INTERNAL_ERROR");
}
return body;
}
export const teleworkApi = {
async get(companyId: string) {
return TeleworkAuthorizationsResponseSchema.parse(await request(companyId));
},
async save(companyId: string, values: TeleworkAuthorizationInput, token: string, id?: string) {
return TeleworkAuthorizationResponseSchema.parse(await request(companyId, values, token, id));
},
};