- Implemented Person entity with validation and update methods. - Created CalendarDate value object for date validation. - Developed migration script to transition company_contacts to person and department structures. - Established Sequelize models for departments and assignments. - Introduced request DTOs for person and department management. - Added shared DTO schemas for validation. - Built assignment and department forms with validation and submission handling. - Created people section to manage and display company personnel. - Implemented person form for adding and editing personnel details.
81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import type { z } from "zod/v4";
|
|
|
|
import {
|
|
CompaniesResponseSchema,
|
|
CompanyErrorResponseSchema,
|
|
CompanyResponseSchema,
|
|
} from "../../common";
|
|
|
|
export const companyKeys = {
|
|
all: ["companies"],
|
|
list: ["companies", "list"],
|
|
detail: (id: string) => ["companies", "detail", id],
|
|
};
|
|
export class CompanyApiError extends Error {
|
|
constructor(public readonly code: string) {
|
|
super(code);
|
|
}
|
|
}
|
|
export async function companyRequest<T>(
|
|
path: string,
|
|
schema: z.ZodType<T>,
|
|
body?: unknown,
|
|
csrfToken?: string
|
|
): Promise<T> {
|
|
const response = await fetch(`/api/companies${path}`, {
|
|
method: body === undefined ? "GET" : "POST",
|
|
credentials: "same-origin",
|
|
headers:
|
|
body === undefined
|
|
? {}
|
|
: { "content-type": "application/json", "x-csrf-token": csrfToken ?? "" },
|
|
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
});
|
|
const json: unknown = await response.json();
|
|
if (!response.ok) {
|
|
const error = CompanyErrorResponseSchema.safeParse(json);
|
|
throw new CompanyApiError(error.success ? error.data.code : "INTERNAL_ERROR");
|
|
}
|
|
return schema.parse(json);
|
|
}
|
|
export const companyApi = {
|
|
list: () => companyRequest("", CompaniesResponseSchema),
|
|
get: (id: string) => companyRequest(`/${id}`, CompanyResponseSchema),
|
|
save: (id: string | undefined, body: unknown, token: string) =>
|
|
companyRequest(id ? `/${id}/update` : "", CompanyResponseSchema, body, token),
|
|
person: (companyId: string, id: string | undefined, body: unknown, token: string) =>
|
|
companyRequest(
|
|
`/${companyId}/people${id ? `/${id}/update` : ""}`,
|
|
CompanyResponseSchema,
|
|
body,
|
|
token
|
|
),
|
|
department: (companyId: string, id: string | undefined, body: unknown, token: string) =>
|
|
companyRequest(
|
|
`/${companyId}/departments${id ? `/${id}/update` : ""}`,
|
|
CompanyResponseSchema,
|
|
body,
|
|
token
|
|
),
|
|
assignment: (
|
|
companyId: string,
|
|
personId: string,
|
|
id: string | undefined,
|
|
body: unknown,
|
|
token: string
|
|
) =>
|
|
companyRequest(
|
|
`/${companyId}/people/${personId}/assignments${id ? `/${id}/update` : ""}`,
|
|
CompanyResponseSchema,
|
|
body,
|
|
token
|
|
),
|
|
site: (companyId: string, id: string | undefined, body: unknown, token: string) =>
|
|
companyRequest(
|
|
`/${companyId}/sites${id ? `/${id}/update` : ""}`,
|
|
CompanyResponseSchema,
|
|
body,
|
|
token
|
|
),
|
|
};
|