front-hub/src/api/verification.ts

112 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-05-27 15:43:38 +00:00
import makeRequest from "@api/makeRequest";
2023-07-06 21:52:07 +00:00
2024-05-27 15:43:38 +00:00
import { jsonToFormdata } from "@root/utils/jsonToFormdata";
import { parseAxiosError } from "@root/utils/parse-error";
2023-09-15 12:28:46 +00:00
import type {
2024-05-27 15:43:38 +00:00
Verification,
SendDocumentsArgs,
UpdateDocumentsArgs,
} from "@root/model/auth";
2023-07-06 21:52:07 +00:00
2024-05-27 15:43:38 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/verification/v1.0.0/verification`;
2023-07-06 21:52:07 +00:00
2024-05-28 13:38:01 +00:00
export const verification = async (
2024-05-27 15:43:38 +00:00
userId: string
2024-05-28 13:38:01 +00:00
): Promise<[Verification | null, string?]> => {
2024-05-27 15:43:38 +00:00
try {
const verificationResponse = await makeRequest<never, Verification>({
method: "GET",
2024-07-10 15:51:18 +00:00
url: API_URL,
// url: `${API_URL}/${userId}`,
2024-05-27 15:43:38 +00:00
useToken: true,
withCredentials: true,
});
verificationResponse.files = verificationResponse.files.map((obj) => {
obj.url = obj.url
.replace(
"https://hub.pena.digital",
process.env.REACT_APP_DOMAIN?.toString() || ""
)
.replace(
"https://shub.pena.digital",
process.env.REACT_APP_DOMAIN?.toString() || ""
);
return obj;
});
return [verificationResponse];
} catch (nativeError) {
const [error, status] = parseAxiosError(nativeError);
if (status === 404) {
return [null, "нет данных"];
}
return [null, `Ошибка запроса верификации. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2024-05-28 13:38:01 +00:00
export const sendDocuments = async (
2024-05-27 15:43:38 +00:00
documents: SendDocumentsArgs
2024-05-28 13:38:01 +00:00
): Promise<[Verification | "OK" | null, string?]> => {
2024-05-27 15:43:38 +00:00
try {
const sendDocumentsResponse = await makeRequest<FormData, Verification>({
method: "POST",
2024-05-28 13:38:01 +00:00
url: API_URL,
body: jsonToFormdata({ ...documents, egrule: documents.inn }),
2024-05-27 15:43:38 +00:00
useToken: true,
withCredentials: true,
});
return [sendDocumentsResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка отправки документов. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2023-09-15 12:28:46 +00:00
2024-05-28 13:38:01 +00:00
export const updateDocuments = async (
2024-05-27 15:43:38 +00:00
documents: UpdateDocumentsArgs
2024-05-28 13:38:01 +00:00
): Promise<[Verification | "OK" | null, string?]> => {
2024-05-27 15:43:38 +00:00
try {
const updateDocumentsResponse = await makeRequest<FormData, Verification>({
method: "PUT",
url: `${API_URL}`,
2024-05-27 15:43:38 +00:00
body: jsonToFormdata(
documents.inn ? { ...documents, egrule: documents.inn } : documents
),
2024-05-28 13:38:01 +00:00
useToken: true,
withCredentials: true,
2024-05-27 15:43:38 +00:00
});
return [updateDocumentsResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка обновления документов. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2024-05-27 15:43:38 +00:00
export const updateDocument = async (
body: FormData
): Promise<[Verification | "OK" | null, string?]> => {
try {
const updateDocumentResponse = await makeRequest<FormData, Verification>({
method: "PATCH",
2024-05-28 13:38:01 +00:00
url: API_URL,
2024-05-27 15:43:38 +00:00
body,
useToken: true,
withCredentials: true,
});
return [updateDocumentResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка обновления документа. ${error}`];
}
};