2025-06-01 14:05:41 +00:00
|
|
|
|
import { makeRequest } from "@frontend/kitui";
|
|
|
|
|
import { parseAxiosError } from "@utils/parse-error";
|
|
|
|
|
|
|
|
|
|
const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz`;
|
|
|
|
|
|
|
|
|
|
// Types
|
|
|
|
|
export interface AuditoryItem {
|
|
|
|
|
id: number;
|
|
|
|
|
quiz_id: number;
|
|
|
|
|
sex: boolean;
|
|
|
|
|
age: string;
|
|
|
|
|
deleted: boolean;
|
|
|
|
|
created_at: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AuditoryResponse {
|
|
|
|
|
data: AuditoryItem[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Request Types
|
|
|
|
|
export interface AuditoryGetRequest {
|
|
|
|
|
quizId: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AuditoryDeleteRequest {
|
|
|
|
|
id: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AuditoryAddRequest {
|
|
|
|
|
sex: boolean;
|
|
|
|
|
age: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parameters
|
|
|
|
|
export interface AuditoryGetParams {
|
|
|
|
|
quizId: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AuditoryDeleteParams {
|
|
|
|
|
quizId: number;
|
|
|
|
|
auditoryId: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AuditoryAddParams {
|
|
|
|
|
quizId: number;
|
|
|
|
|
body: AuditoryAddRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// API calls
|
2025-06-01 21:27:22 +00:00
|
|
|
|
export const auditoryGet = async ({ quizId }: AuditoryGetParams): Promise<[AuditoryItem[] | null, string?]> => {
|
2025-06-01 14:05:41 +00:00
|
|
|
|
if (!quizId) {
|
|
|
|
|
return [null, "Quiz ID is required"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-06-01 21:27:22 +00:00
|
|
|
|
const response = await makeRequest<AuditoryGetRequest, AuditoryItem[]>({
|
2025-06-01 14:05:41 +00:00
|
|
|
|
url: `${API_URL}/quiz/${quizId}/auditory`,
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [response];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
return [null, `Не удалось получить аудиторию. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const auditoryDelete = async ({ quizId, auditoryId }: AuditoryDeleteParams): Promise<[AuditoryResponse | null, string?]> => {
|
|
|
|
|
if (!quizId || !auditoryId) {
|
|
|
|
|
return [null, "Quiz ID and Auditory ID are required"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await makeRequest<AuditoryDeleteRequest, AuditoryResponse>({
|
2025-06-01 21:27:22 +00:00
|
|
|
|
url: `${API_URL}/quiz/${quizId}/auditory/${auditoryId}`,
|
2025-06-01 14:05:41 +00:00
|
|
|
|
method: "DELETE",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [response];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
return [null, `Не удалось удалить аудиторию. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const auditoryAdd = async ({ quizId, body }: AuditoryAddParams): Promise<[AuditoryResponse | null, string?]> => {
|
|
|
|
|
if (!quizId) {
|
|
|
|
|
return [null, "Quiz ID is required"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await makeRequest<AuditoryAddRequest, AuditoryResponse>({
|
|
|
|
|
url: `${API_URL}/quiz/${quizId}/auditory`,
|
|
|
|
|
body,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [response];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
return [null, `Не удалось добавить аудиторию. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|