WIP Добавил обновление данных доверенных лиц

This commit is contained in:
DwCay
2023-04-28 17:47:43 +03:00
parent 58a570a102
commit adb7982605
8 changed files with 331 additions and 56 deletions

View File

@@ -41,8 +41,13 @@ function request(method, url, headers = {}, body, type = "") {
headers: {
Authorization: `Bearer ${token}`,
...headers,
"Content-Type": "application/json",
Accept: "application/json",
},
};
if (body) {
requestOptions.body = JSON.stringify(body);
}
}
if (method === "POST" || method === "PUT") {
if (type && type === "formData") {
@@ -74,8 +79,8 @@ function get(url, type, headers, attempts = 3) {
return handleRequest("GET", url, headers, attempts, null, type);
}
function del(url, headers, attempts = 3) {
return handleRequest("DELETE", url, headers, attempts, null);
function del(url, body, headers, attempts = 3) {
return handleRequest("DELETE", url, headers, attempts, body);
}
function post(url, body, type, headers, attempts = 3) {

View File

@@ -62,3 +62,130 @@ export function getRequestArrayData(
});
return requests.concat(deleteRequests);
}
export function getRequestConfidant(confidant, initConfidant, initPerson) {
const medicalId = localStorage.getItem("medicalId");
const fullName = `${confidant.last_name.value} ${confidant.first_name.value} ${confidant.patronymic.value}`;
let contacts = [];
const phone = confidant.phone.value
? { username: confidant.phone.value, kind: "PHONE" }
: null;
const email = confidant.email.value
? { username: confidant.email.value, kind: "EMAIL" }
: null;
if (confidant.phone.id) phone.id = confidant.phone.id;
if (confidant.email.id) email.id = confidant.email.id;
if (phone) contacts.push(phone);
if (email) contacts.push(email);
contacts = contacts.concat(confidant.networks);
if (!confidant.id) {
return fetchWrapper
.post(`general/person/create/`, { full_name: fullName })
.then((res) => {
return Promise.allSettled(
getRequestArrayData(
[],
contacts,
"person",
res.id,
"general",
"contact"
)
).then(() =>
fetchWrapper.post(
`medical_card/medical_history/${medicalId}/update/`,
{
confidant: [
{
person: {
id: res.id,
},
},
],
}
)
);
});
} else {
if (initPerson) {
if (initConfidant && initPerson.id !== initConfidant.id) {
fetchWrapper.del(
`medical_card/medical_history/${medicalId}/delete_confidant/`,
{
confidants: [
{
person: {
id: initConfidant.id,
},
},
],
}
);
}
return fetchWrapper
.post(`general/person/${confidant.id}/update/`, {
full_name: fullName,
})
.then(() => {
if (contacts.length) {
return Promise.allSettled(
getRequestArrayData(
initPerson.contacts,
contacts,
"person",
confidant.id,
"general",
"contact"
)
).then(() =>
fetchWrapper.post(
`medical_card/medical_history/${medicalId}/update/`,
{
confidant: [
{
person: {
id: confidant.id,
},
},
],
}
)
);
}
});
}
if (initConfidant && !initPerson) {
return fetchWrapper
.post(`general/person/${confidant.id}/update/`, {
full_name: fullName,
})
.then(() => {
if (contacts.length) {
return Promise.allSettled(
getRequestArrayData(
initConfidant.networks,
contacts,
"person",
confidant.id,
"general",
"contact"
)
).then(() =>
fetchWrapper.post(
`medical_card/medical_history/${medicalId}/update/`,
{
confidant: [
{
person: {
id: confidant.id,
},
},
],
}
)
);
}
});
}
}
}