2025-01-27 15:03:48 +03:00
|
|
|
import { queryOptions } from '@tanstack/react-query';
|
2025-02-10 13:29:28 +03:00
|
|
|
import { z } from 'zod';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-01-28 19:47:24 +03:00
|
|
|
import { axiosDelete, axiosGet, axiosPatch, axiosPost } from '@/backend/apiTransport';
|
2025-01-27 15:03:48 +03:00
|
|
|
import { DELAYS } from '@/backend/configuration';
|
2025-02-10 01:32:55 +03:00
|
|
|
import { ILibraryItem, ILibraryItemData, LibraryItemID } from '@/features/library/models/library';
|
2025-01-27 15:03:48 +03:00
|
|
|
import {
|
2025-01-30 19:55:38 +03:00
|
|
|
IArgument,
|
2025-01-27 15:03:48 +03:00
|
|
|
ICstSubstitute,
|
2025-01-30 19:55:38 +03:00
|
|
|
ICstSubstituteEx,
|
|
|
|
IOperation,
|
2025-01-27 15:03:48 +03:00
|
|
|
OperationID,
|
|
|
|
OperationType
|
2025-02-10 01:32:55 +03:00
|
|
|
} from '@/features/oss/models/oss';
|
2025-02-10 15:49:56 +03:00
|
|
|
import { IConstituentaReference, ITargetCst } from '@/features/rsform/models/rsform';
|
2025-01-28 19:47:24 +03:00
|
|
|
import { information } from '@/utils/labels';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-01-30 19:55:38 +03:00
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data from server.
|
|
|
|
*/
|
|
|
|
export interface IOperationDTO extends Omit<IOperation, 'substitutions' | 'arguments'> {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents backend data for {@link IOperationSchema}.
|
|
|
|
*/
|
|
|
|
export interface IOperationSchemaDTO extends ILibraryItemData {
|
|
|
|
items: IOperationDTO[];
|
|
|
|
arguments: IArgument[];
|
|
|
|
substitutions: ICstSubstituteEx[];
|
|
|
|
}
|
|
|
|
|
2025-02-10 14:11:28 +03:00
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} position.
|
|
|
|
*/
|
|
|
|
export const schemaOperationPosition = z.object({
|
|
|
|
id: z.number(),
|
|
|
|
position_x: z.number(),
|
|
|
|
position_y: z.number()
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} position.
|
|
|
|
*/
|
|
|
|
export type IOperationPosition = z.infer<typeof schemaOperationPosition>;
|
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data, used in creation process.
|
|
|
|
*/
|
|
|
|
export interface IOperationCreateDTO {
|
|
|
|
positions: IOperationPosition[];
|
|
|
|
item_data: {
|
|
|
|
alias: string;
|
|
|
|
operation_type: OperationType;
|
|
|
|
title: string;
|
|
|
|
comment: string;
|
|
|
|
position_x: number;
|
|
|
|
position_y: number;
|
|
|
|
result: LibraryItemID | null;
|
|
|
|
};
|
|
|
|
arguments: OperationID[] | undefined;
|
|
|
|
create_schema: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents data response when creating {@link IOperation}.
|
|
|
|
*/
|
|
|
|
export interface IOperationCreatedResponse {
|
2025-01-30 19:55:38 +03:00
|
|
|
new_operation: IOperationDTO;
|
|
|
|
oss: IOperationSchemaDTO;
|
2025-01-27 15:03:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents target {@link IOperation}.
|
|
|
|
*/
|
|
|
|
export interface ITargetOperation {
|
|
|
|
positions: IOperationPosition[];
|
|
|
|
target: OperationID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data, used in destruction process.
|
|
|
|
*/
|
2025-02-10 14:11:28 +03:00
|
|
|
export const schemaOperationDelete = z.object({
|
|
|
|
target: z.number(),
|
|
|
|
positions: z.array(schemaOperationPosition),
|
|
|
|
keep_constituents: z.boolean(),
|
|
|
|
delete_schema: z.boolean()
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data, used in destruction process.
|
|
|
|
*/
|
|
|
|
export type IOperationDeleteDTO = z.infer<typeof schemaOperationDelete>;
|
2025-01-27 15:03:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents data response when creating {@link IRSForm} for Input {@link IOperation}.
|
|
|
|
*/
|
|
|
|
export interface IInputCreatedResponse {
|
|
|
|
new_schema: ILibraryItem;
|
2025-01-30 19:55:38 +03:00
|
|
|
oss: IOperationSchemaDTO;
|
2025-01-27 15:03:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data, used in setInput process.
|
|
|
|
*/
|
2025-02-10 14:11:28 +03:00
|
|
|
export const schemaInputUpdate = z.object({
|
2025-02-10 13:29:28 +03:00
|
|
|
target: z.number(),
|
2025-02-10 14:11:28 +03:00
|
|
|
positions: z.array(schemaOperationPosition),
|
2025-02-10 13:29:28 +03:00
|
|
|
input: z.number().nullable()
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data, used in setInput process.
|
|
|
|
*/
|
2025-02-10 14:11:28 +03:00
|
|
|
export type IInputUpdateDTO = z.infer<typeof schemaInputUpdate>;
|
2025-01-27 15:03:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents {@link IOperation} data, used in update process.
|
|
|
|
*/
|
|
|
|
export interface IOperationUpdateDTO extends ITargetOperation {
|
|
|
|
item_data: {
|
|
|
|
alias: string;
|
|
|
|
title: string;
|
|
|
|
comment: string;
|
|
|
|
};
|
|
|
|
arguments: OperationID[] | undefined;
|
|
|
|
substitutions: ICstSubstitute[] | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents data, used relocating {@link IConstituenta}s between {@link ILibraryItem}s.
|
|
|
|
*/
|
2025-02-10 15:49:56 +03:00
|
|
|
export const schemaCstRelocate = z.object({
|
|
|
|
destination: z.number(),
|
|
|
|
items: z.array(z.number()).refine(data => data.length > 0)
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents data, used relocating {@link IConstituenta}s between {@link ILibraryItem}s.
|
|
|
|
*/
|
|
|
|
export type ICstRelocateDTO = z.infer<typeof schemaCstRelocate>;
|
2025-01-27 15:03:48 +03:00
|
|
|
|
|
|
|
export const ossApi = {
|
|
|
|
baseKey: 'oss',
|
|
|
|
|
|
|
|
getOssQueryOptions: ({ itemID }: { itemID?: LibraryItemID }) => {
|
|
|
|
return queryOptions({
|
|
|
|
queryKey: [ossApi.baseKey, 'item', itemID],
|
|
|
|
staleTime: DELAYS.staleShort,
|
|
|
|
queryFn: meta =>
|
|
|
|
!itemID
|
|
|
|
? undefined
|
2025-01-30 19:55:38 +03:00
|
|
|
: axiosGet<IOperationSchemaDTO>({
|
2025-01-28 19:47:24 +03:00
|
|
|
endpoint: `/api/oss/${itemID}/details`,
|
|
|
|
options: { signal: meta.signal }
|
|
|
|
})
|
2025-01-27 15:03:48 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2025-01-28 19:47:24 +03:00
|
|
|
updatePositions: ({
|
|
|
|
itemID,
|
|
|
|
positions,
|
|
|
|
isSilent
|
|
|
|
}: {
|
|
|
|
itemID: LibraryItemID;
|
|
|
|
positions: IOperationPosition[];
|
|
|
|
isSilent?: boolean;
|
|
|
|
}) =>
|
|
|
|
axiosPatch({
|
|
|
|
endpoint: `/api/oss/${itemID}/update-positions`,
|
|
|
|
request: {
|
|
|
|
data: { positions: positions },
|
|
|
|
successMessage: isSilent ? undefined : information.changesSaved
|
|
|
|
}
|
|
|
|
}),
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-01-28 19:47:24 +03:00
|
|
|
operationCreate: ({ itemID, data }: { itemID: LibraryItemID; data: IOperationCreateDTO }) =>
|
|
|
|
axiosPost<IOperationCreateDTO, IOperationCreatedResponse>({
|
|
|
|
endpoint: `/api/oss/${itemID}/create-operation`,
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: response => information.newOperation(response.new_operation.alias)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
operationDelete: ({ itemID, data }: { itemID: LibraryItemID; data: IOperationDeleteDTO }) =>
|
2025-01-30 19:55:38 +03:00
|
|
|
axiosDelete<IOperationDeleteDTO, IOperationSchemaDTO>({
|
2025-01-28 19:47:24 +03:00
|
|
|
endpoint: `/api/oss/${itemID}/delete-operation`,
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.operationDestroyed
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
inputCreate: ({ itemID, data }: { itemID: LibraryItemID; data: ITargetOperation }) =>
|
|
|
|
axiosPatch<ITargetOperation, IInputCreatedResponse>({
|
|
|
|
endpoint: `/api/oss/${itemID}/create-input`,
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.newLibraryItem
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
inputUpdate: ({ itemID, data }: { itemID: LibraryItemID; data: IInputUpdateDTO }) =>
|
2025-01-30 19:55:38 +03:00
|
|
|
axiosPatch<IInputUpdateDTO, IOperationSchemaDTO>({
|
2025-01-28 19:47:24 +03:00
|
|
|
endpoint: `/api/oss/${itemID}/set-input`,
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.changesSaved
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
operationUpdate: ({ itemID, data }: { itemID: LibraryItemID; data: IOperationUpdateDTO }) =>
|
2025-01-30 19:55:38 +03:00
|
|
|
axiosPatch<IOperationUpdateDTO, IOperationSchemaDTO>({
|
2025-01-28 19:47:24 +03:00
|
|
|
endpoint: `/api/oss/${itemID}/update-operation`,
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.changesSaved
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
operationExecute: ({ itemID, data }: { itemID: LibraryItemID; data: ITargetOperation }) =>
|
2025-01-30 19:55:38 +03:00
|
|
|
axiosPost<ITargetOperation, IOperationSchemaDTO>({
|
2025-01-28 19:47:24 +03:00
|
|
|
endpoint: `/api/oss/${itemID}/execute-operation`,
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.operationExecuted
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2025-02-10 15:49:56 +03:00
|
|
|
relocateConstituents: (data: ICstRelocateDTO) =>
|
2025-01-30 19:55:38 +03:00
|
|
|
axiosPost<ICstRelocateDTO, IOperationSchemaDTO>({
|
2025-02-10 15:49:56 +03:00
|
|
|
endpoint: `/api/oss/relocate-constituents`,
|
2025-01-28 19:47:24 +03:00
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.changesSaved
|
|
|
|
}
|
|
|
|
}),
|
2025-01-27 15:03:48 +03:00
|
|
|
getPredecessor: (data: ITargetCst) =>
|
2025-01-28 19:47:24 +03:00
|
|
|
axiosPost<ITargetCst, IConstituentaReference>({
|
|
|
|
endpoint: '/api/oss/get-predecessor',
|
|
|
|
request: { data: data }
|
|
|
|
})
|
2025-01-27 15:03:48 +03:00
|
|
|
};
|