const getEntityTypeId = (payload) => { const body = {}; if (payload !== null) { body['type'] = payload.type; body['id'] = payload.id; }; console.log('body', body); return body; } /* * Endpoint v.2 chill_api_single_accompanying_course__entity * method GET/HEAD, get AccompanyingCourse Instance * * @id integer - id of accompanyingCourse */ const getAccompanyingCourse = (id) => { const url = `/api/1.0/person/accompanying-course/${id}.json`; return fetch(url) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint v.2 chill_api_single_accompanying_course__entity * method PATCH, patch AccompanyingCourse Instance * * @id integer - id of accompanyingCourse * @body Object - dictionary with changes to post */ const patchAccompanyingCourse = (id, body) => { console.log('body', body); const url = `/api/1.0/person/accompanying-course/${id}.json`; return fetch(url, { method: 'PATCH', headers: { 'Content-Type': 'application/json;charset=utf-8' }, body: JSON.stringify(body) }) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint v.2 chill_api_single_accompanying_course_participation, * method POST/DELETE, add/close a participation to the accompanyingCourse * * @id integer - id of accompanyingCourse * @payload integer - id of person * @method string - POST or DELETE */ const postParticipation = (id, payload, method) => { const body = getEntityTypeId(payload); const url = `/api/1.0/person/accompanying-course/${id}/participation.json`; return fetch(url, { method: method, headers: { 'Content-Type': 'application/json;charset=utf-8' }, body: JSON.stringify(body) }) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint v.2 chill_api_single_accompanying_course_requestor, * method POST/DELETE, add/close a requestor to the accompanyingCourse * * @id integer - id of accompanyingCourse * @payload object of type person|thirdparty * @method string - POST or DELETE */ const postRequestor = (id, payload, method) => { const body = getEntityTypeId(payload); const url = `/api/1.0/person/accompanying-course/${id}/requestor.json`; return fetch(url, { method: method, headers: { 'Content-Type': 'application/json;charset=utf-8' }, body: JSON.stringify(body) }) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint v.2 chill_api_single_accompanying_course_resource, * method POST/DELETE, add/remove a resource to the accompanyingCourse * * @id integer - id of accompanyingCourse * @payload object of type person|thirdparty * @method string - POST or DELETE */ const postResource = (id, payload, method) => { console.log('payload', payload); const body = { type: "accompanying_period_resource" }; if (method === 'DELETE') { body['id'] = payload.id; } else { body['resource'] = getEntityTypeId(payload); } console.log('body', body); const url = `/api/1.0/person/accompanying-course/${id}/resource.json`; return fetch(url, { method: method, headers: { 'Content-Type': 'application/json;charset=utf-8' }, body: JSON.stringify(body) }) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; export { getAccompanyingCourse, patchAccompanyingCourse, postParticipation, postRequestor, postResource };