/
viktorphp
/
front-socket
Обзор
Документация
Войти
/
viktorphp
/
front-socket
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/shared/api/rest/ancestors.ts
222 строки
7 KB
Иванов Виктор Евгеньевич
[feat-tree]: добавить страницу дерева предков, поправить основные баги
13 апр 2026, 21:32
13 апр 2026, 21:32
6a6f509
Код
Авторство
О чём код?
import { Gender, IAncestor, IAncestorContribution, IAncestorMedia, IFamilyRequest, ITreeNode, RelationType, } from '@shared/types'; import { del, get, patch, post } from './requests'; // ─── Типы запросов и ответов ─────────────────────────────────────────────── export interface ICreateAncestorDto { firstName?: string; lastName?: string; patronymic?: string; gender?: Gender; birthYear?: number; deathYear?: number; isAlive?: boolean; nameOnly?: boolean; userId?: string; } export interface IUpdateAncestorDto { firstName?: string; lastName?: string; patronymic?: string; gender?: Gender; birthYear?: number; deathYear?: number; isAlive?: boolean; nameOnly?: boolean; } export interface IAddRelationDto { relatedAncestorId: string; relationType: RelationType; } export interface ICreateContributionDto { description?: string; biography?: string; } // ─── CRUD предков ────────────────────────────────────────────────────────── export const createAncestor = async ( dto: ICreateAncestorDto ): Promise<IAncestor> => { const res = await post<{ ancestor: IAncestor }>('/ancestors', dto); return res.data.ancestor; }; export const searchAncestors = async (q: string): Promise<IAncestor[]> => { const res = await get<{ ancestors: IAncestor[] }>('/ancestors/search', { params: { q }, }); return res.data.ancestors; }; export const getAncestor = async (id: string): Promise<IAncestor> => { const res = await get<{ ancestor: IAncestor }>(`/ancestors/${id}`); return res.data.ancestor; }; export const updateAncestor = async ( id: string, dto: IUpdateAncestorDto ): Promise<IAncestor> => { const res = await patch<{ ancestor: IAncestor }>(`/ancestors/${id}`, dto); return res.data.ancestor; }; export const deleteAncestor = async (id: string): Promise<void> => { await del(`/ancestors/${id}`); }; // ─── Дерево ──────────────────────────────────────────────────────────────── export const getAncestorTree = async ( id: string, depth = 3 ): Promise<ITreeNode> => { const res = await get<{ tree: ITreeNode }>(`/ancestors/${id}/tree`, { params: { depth }, }); return res.data.tree; }; export const getUserTree = async ( userId: string, depth = 3 ): Promise<ITreeNode> => { const res = await get<{ tree: ITreeNode }>(`/users/${userId}/tree`, { params: { depth }, }); return res.data.tree; }; export const getUserTreeFlat = async (userId: string): Promise<IAncestor[]> => { const res = await get<{ ancestors: IAncestor[] }>( `/users/${userId}/tree/flat` ); return res.data.ancestors; }; export const getFreeAncestors = async ( userId: string ): Promise<IAncestor[]> => { const res = await get<{ ancestors: IAncestor[] }>( `/users/${userId}/tree/free` ); return res.data.ancestors; }; // ─── Связи ───────────────────────────────────────────────────────────────── export const addRelation = async ( ancestorId: string, dto: IAddRelationDto ): Promise<{ immediate: boolean; request?: IFamilyRequest }> => { const res = await post<{ message: string; request?: IFamilyRequest }>( `/ancestors/${ancestorId}/relations`, dto ); return { immediate: !res.data.request, request: res.data.request, }; }; export const detachAncestor = async (id: string): Promise<void> => { await post(`/ancestors/${id}/detach`); }; export const removeRelation = async ( ancestorId: string, relatedId: string ): Promise<void> => { await del(`/ancestors/${ancestorId}/relations/${relatedId}`); }; // ─── Вклады (contributions) ──────────────────────────────────────────────── export const upsertContribution = async ( ancestorId: string, dto: ICreateContributionDto ): Promise<IAncestorContribution> => { const res = await post<{ contribution: IAncestorContribution }>( `/ancestors/${ancestorId}/contributions`, dto ); return res.data.contribution; }; export const getContributions = async ( ancestorId: string ): Promise<IAncestorContribution[]> => { const res = await get<{ contributions: IAncestorContribution[] }>( `/ancestors/${ancestorId}/contributions` ); return res.data.contributions; }; // ─── Медиафайлы ──────────────────────────────────────────────────────────── export const uploadMedia = async ( ancestorId: string, file: File ): Promise<IAncestorMedia> => { const formData = new FormData(); formData.append('file', file); const res = await post<{ media: IAncestorMedia }>( `/ancestors/${ancestorId}/media`, formData, { headers: { 'Content-Type': 'multipart/form-data' } } ); return res.data.media; }; export const getAncestorMedia = async ( ancestorId: string ): Promise<IAncestorMedia[]> => { const res = await get<{ media: IAncestorMedia[] }>( `/ancestors/${ancestorId}/media` ); return res.data.media; }; export const getMediaUrl = async (mediaId: string): Promise<string> => { const res = await get<{ url: string }>(`/ancestors/media/${mediaId}/url`); return res.data.url; }; export const setAvatar = async (mediaId: string): Promise<IAncestorMedia> => { const res = await patch<{ media: IAncestorMedia }>( `/ancestors/media/${mediaId}/avatar` ); return res.data.media; }; export const deleteMedia = async (mediaId: string): Promise<void> => { await del(`/ancestors/media/${mediaId}`); }; // ─── Family requests ─────────────────────────────────────────────────────── export const getFamilyRequests = async (): Promise<IFamilyRequest[]> => { const res = await get<{ requests: IFamilyRequest[] }>('/family-requests'); return res.data.requests; }; export const acceptFamilyRequest = async (id: string): Promise<void> => { await post(`/family-requests/${id}/accept`); }; export const rejectFamilyRequest = async (id: string): Promise<void> => { await post(`/family-requests/${id}/reject`); };