2024-03-20 14:44:51 +00:00
|
|
|
|
import { makeRequest } from "@frontend/kitui";
|
|
|
|
|
|
2024-03-21 14:24:56 +00:00
|
|
|
|
import { parseAxiosError } from "@utils/parse-error";
|
2024-03-20 14:44:51 +00:00
|
|
|
|
|
2024-03-21 14:24:56 +00:00
|
|
|
|
const apiUrl = process.env.REACT_APP_DOMAIN + "/squiz/statistic";
|
2024-03-20 14:44:51 +00:00
|
|
|
|
|
|
|
|
|
export type DevicesResponse = {
|
|
|
|
|
device: Record<string, number>;
|
|
|
|
|
os: Record<string, number>;
|
|
|
|
|
browser: Record<string, number>;
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 14:24:56 +00:00
|
|
|
|
export type GeneralResponse = {
|
|
|
|
|
open: Record<string, number>;
|
|
|
|
|
result: Record<string, number>;
|
|
|
|
|
avtime: Record<string, number>;
|
|
|
|
|
conversation: Record<string, number>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type QuestionsResponse = {
|
|
|
|
|
funnel: number[];
|
|
|
|
|
results: Record<string, number>;
|
|
|
|
|
questions: Record<string, Record<string, number>>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getDevices = async (
|
2024-03-20 14:44:51 +00:00
|
|
|
|
quizId: string,
|
2024-03-28 09:18:24 +00:00
|
|
|
|
to: number,
|
|
|
|
|
from: number,
|
2024-03-21 14:24:56 +00:00
|
|
|
|
): Promise<[DevicesResponse | null, string?]> => {
|
2024-03-20 14:44:51 +00:00
|
|
|
|
try {
|
|
|
|
|
const devicesResponse = await makeRequest<unknown, DevicesResponse>({
|
|
|
|
|
method: "POST",
|
2024-03-21 14:24:56 +00:00
|
|
|
|
url: `${apiUrl}/${quizId}/devices`,
|
2024-03-20 14:44:51 +00:00
|
|
|
|
withCredentials: true,
|
2024-03-28 09:18:24 +00:00
|
|
|
|
body: {to, from}
|
2024-03-20 14:44:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [devicesResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-03-21 14:24:56 +00:00
|
|
|
|
return [null, `Не удалось получить статистику о девайсах. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getGeneral = async (
|
|
|
|
|
quizId: string,
|
2024-03-28 09:18:24 +00:00
|
|
|
|
to: number,
|
|
|
|
|
from: number,
|
2024-03-21 14:24:56 +00:00
|
|
|
|
): Promise<[GeneralResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const generalResponse = await makeRequest<unknown, GeneralResponse>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${apiUrl}/${quizId}/general`,
|
|
|
|
|
withCredentials: true,
|
2024-03-28 09:18:24 +00:00
|
|
|
|
body: {to, from}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [generalResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Не удалось получить ключевые метрики. ${error}`];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getQuestions = async (
|
|
|
|
|
quizId: string,
|
2024-03-28 09:18:24 +00:00
|
|
|
|
to: number,
|
|
|
|
|
from: number,
|
2024-03-21 14:24:56 +00:00
|
|
|
|
): Promise<[QuestionsResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const questionsResponse = await makeRequest<unknown, QuestionsResponse>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${apiUrl}/${quizId}/questions`,
|
|
|
|
|
withCredentials: true,
|
2024-03-28 09:18:24 +00:00
|
|
|
|
body: {to, from}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [questionsResponse];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
|
|
return [null, `Не удалось получить статистику по результатам. ${error}`];
|
2024-03-20 14:44:51 +00:00
|
|
|
|
}
|
|
|
|
|
};
|