126 lines
2.7 KiB
JavaScript

/**
* @function makeFetch
* @param method
* @param url
* @param body
* @returns {Promise<Response>}
*/
const makeFetch = (method, url, body) => {
return fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: (body !== null) ? JSON.stringify(body) : null
})
.then(response => {
if (response.ok) {
return response.json();
}
if (response.status === 422) {
return response.json();
}
throw {
msg: 'Error while updating AccompanyingPeriod Course.',
sta: response.status,
txt: response.statusText,
err: new Error(),
body: response.body
};
});
}
/**
* @function getFetch
* @param url
* @returns {Promise<Response>}
*/
const getFetch = (url) => {
return makeFetch('GET', url, null);
}
/**
* @function postFetch
* @param url
* @param body
* @returns {Promise<Response>}
*/
const postFetch = (url, body) => {
return makeFetch('POST', url, body);
}
/**
* @function patchFetch
* @param url
* @param body
* @returns {Promise<Response>}
*/
const patchFetch = (url, body) => {
return makeFetch('PATCH', url, body);
}
/**
* @function getHouseholdByPerson
* @param person
* @returns {Promise<Response>}
*/
const getHouseholdByPerson = (person) => {
//console.log('getHouseholdByPerson', person.id)
if (person.current_household_id === null) {
throw 'Currently the person has not household!'
}
return getFetch(
`/api/1.0/person/household/${person.current_household_id}.json`)
}
/**
* @function getCoursesByPerson
* @param person
* @returns {Promise<Response>}
*/
const getCoursesByPerson = (person) => {
//console.log('getCoursesByPerson', person._id)
return getFetch(
`/api/1.0/person/accompanying-course/by-person/${person._id}.json`)
}
/**
* @function getRelationshipByPerson
* @param person
* @returns {Promise<Response>}
*/
const getRelationshipByPerson = (person) => {
//console.log('getRelationshipByPerson', person.id)
return getFetch(
`/api/1.0/relations/relationship/by-person/${person._id}.json`)
}
/**
* @function postRelationship
* @param person
* @returns {Promise<Response>}
*/
const postRelationship = (person) => {
//console.log('postRelationship', person.id)
return postFetch(
`/api/1.0/relations/relationship.json`,
{
from: { type: 'person', id: 0 },
to: { type: 'person', id: 0 },
relation: { type: 'relation', id: 0 },
reverse: bool
}
)
}
export {
getHouseholdByPerson,
getCoursesByPerson,
getRelationshipByPerson,
postRelationship
}