import { splitId } from './vis-network' /** * @function makeFetch * @param method * @param url * @param body * @returns {Promise} */ 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().then(violations => { throw ValidationException(violations) }); } throw { msg: 'Error while updating AccompanyingPeriod Course.', sta: response.status, txt: response.statusText, err: new Error(), body: response.body }; }); } /** * @param violations * @constructor */ const ValidationException = (violations) => { this.violations = violations this.name = 'ValidationException' } /** * @function getFetch * @param url * @returns {Promise} */ const getFetch = (url) => { return makeFetch('GET', url, null) } /** * @function postFetch * @param url * @param body * @returns {Promise} */ const postFetch = (url, body) => { return makeFetch('POST', url, body) } /** * @function patchFetch * @param url * @param body * @returns {Promise} */ const patchFetch = (url, body) => { return makeFetch('PATCH', url, body) } /** * @function deleteFetch * @param url * @param body * @returns {Promise} */ const deleteFetch = (url, body) => { return makeFetch('DELETE', url, null) } /** * @function getHouseholdByPerson * @param person * @returns {Promise} */ 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} */ const getCoursesByPerson = (person) => { //console.log('getCoursesByPerson', person._id) return getFetch( `/api/1.0/person/accompanying-course/by-person/${person._id}.json`) } /** * @function getRelationshipsByPerson * @param person * @returns {Promise} */ const getRelationshipsByPerson = (person) => { //console.log('getRelationshipsByPerson', person.id) return getFetch( `/api/1.0/relations/relationship/by-person/${person._id}.json`) } /** * Return list of relations * @returns {Promise} */ const getRelationsList = () => { return getFetch(`/api/1.0/relations/relation.json`) } /** * @function postRelationship * @param relationship * @returns {Promise} */ const postRelationship = (relationship) => { //console.log(relationship) return postFetch( `/api/1.0/relations/relationship.json`, { type: 'relationship', fromPerson: { type: 'person', id: splitId(relationship.from, 'id') }, toPerson: { type: 'person', id: splitId(relationship.to, 'id') }, relation: { type: 'relation', id: relationship.relation.id }, reverse: relationship.reverse } ) } /** * @function patchRelationship * @param relationship * @returns {Promise} */ const patchRelationship = (relationship) => { //console.log(relationship) let linkType = splitId(relationship.id, 'link') let id = splitId(linkType, 'id') return patchFetch( `/api/1.0/relations/relationship/${id}.json`, { type: 'relationship', fromPerson: { type: 'person', id: splitId(relationship.from, 'id') }, toPerson: { type: 'person', id: splitId(relationship.to, 'id') }, relation: { type: 'relation', id: relationship.relation.id }, reverse: relationship.reverse } ) } /** * @function deleteRelationship * @param relationship * @returns {Promise} */ const deleteRelationship = (relationship) => { //console.log(relationship) let linkType = splitId(relationship.id, 'link') let id = splitId(linkType, 'id') return deleteFetch( `/api/1.0/relations/relationship/${id}.json` ) } export { getHouseholdByPerson, getCoursesByPerson, getRelationshipsByPerson, getRelationsList, postRelationship, patchRelationship, deleteRelationship }