diff --git a/components/FormikFields/PaperInputPicker/HouseholdManager/index.js b/components/FormikFields/PaperInputPicker/HouseholdManager/index.js index 1ce63a9e3..59413148c 100644 --- a/components/FormikFields/PaperInputPicker/HouseholdManager/index.js +++ b/components/FormikFields/PaperInputPicker/HouseholdManager/index.js @@ -63,8 +63,10 @@ const HouseholdManager = (props) => { longitude: 0 } }; - postHouseholdWithRelation(postParams).then((id) => { - setFieldValue(formikKey, id); + + postHouseholdWithRelation(postParams).then((household) => { + const { objectId } = household; + setFieldValue(formikKey, objectId); }); }; @@ -77,8 +79,9 @@ const HouseholdManager = (props) => { longitude: 0 } }; - postHousehold(postParams).then((id) => { - setFieldValue(formikKey, id); + postHousehold(postParams).then((household) => { + const { objectId } = household; + setFieldValue(formikKey, objectId); }); setHouseholdSet(true); }; diff --git a/modules/cached-resources/Post/__tests__/post.test.js b/modules/cached-resources/Post/__tests__/post.test.js index 5fdd3b509..f9fb20af4 100644 --- a/modules/cached-resources/Post/__tests__/post.test.js +++ b/modules/cached-resources/Post/__tests__/post.test.js @@ -1,23 +1,61 @@ import uuid from 'react-native-uuid'; -import { postIdentificationForm } from '../post'; - -it('postIdentificationForm', async () => { - const photoFile = ''; - - const localObject = { - fname: 'Hi' - }; - const parseUser = uuid.v4(); - - const postParams = { - parseClass: 'SurveyData', - parseUser, - photoFile, - localObject - }; - - const postedOfflineForm = await postIdentificationForm(postParams); - - expect(postedOfflineForm).toHaveProperty('objectId') +import checkOnlineStatus from '../../../offline'; +import { postHousehold, postHouseholdWithRelation, postIdentificationForm } from '../post'; + +jest.mock('../../../offline', () => jest.fn()); + +describe('test offline', () => { + test('postIdentificationForm', async () => { + checkOnlineStatus.mockResolvedValue(false); + + const postParams = { + parseClass: 'SurveyData', + parseUser: uuid.v4(), + photoFile: '', + localObject: { + fname: 'Hi' + } + }; + + const postedOfflineForm = await postIdentificationForm(postParams); + + expect(postedOfflineForm).toHaveProperty('objectId'); + }); + + test('postHousehold', async () => { + checkOnlineStatus.mockResolvedValue(false); + const postParams = { + parseClass: 'Household', + localObject: { + latitude: 0, + longitude: 0 + } + }; + + const postedOfflineHousehold = await postHousehold(postParams); + + expect(postedOfflineHousehold).toHaveProperty('objectId'); + expect(postedOfflineHousehold.objectId).toContain('Household'); + }); + + test('Post Household with a relationship', async () => { + checkOnlineStatus.mockResolvedValue(false); + + const postParams = { + parseParentClassID: uuid.v4(), + parseParentClass: 'Household', + parseClass: 'Household', + localObject: { + relationship: 'father', + latitude: 0, + longitude: 0 + } + }; + + const postedHouseholdWithRelation = await postHouseholdWithRelation(postParams); + + expect(postedHouseholdWithRelation).toHaveProperty('objectId'); + expect(postedHouseholdWithRelation.objectId).toContain('Household'); + }); }); diff --git a/modules/cached-resources/Post/post.js b/modules/cached-resources/Post/post.js index c1d3c5923..176938410 100644 --- a/modules/cached-resources/Post/post.js +++ b/modules/cached-resources/Post/post.js @@ -9,30 +9,28 @@ import { generateRandomID } from '../../utils'; const postIdentificationForm = async (postParams) => { const isConnected = await checkOnlineStatus(); if (isConnected) { - postObjectsToClass(postParams).then((surveyee) => { + return postObjectsToClass(postParams).then((surveyee) => { const surveyeeSanitized = JSON.parse(JSON.stringify(surveyee)); - return surveyeeSanitized - }). - catch(()=>error); + return surveyeeSanitized; + }).catch((error) => error); } - return getData('offlineIDForms').then(async offlineIDForms => { - const offlineResidentIdForms = offlineIDForms + return getData('offlineIDForms').then(async (offlineIDForms) => { + const offlineResidentIdForms = offlineIDForms; const idParams = postParams; - const { localObject } = idParams - + const { localObject } = idParams; + localObject.objectId = `PatientID-${generateRandomID()}`; if (offlineResidentIdForms) { const forms = offlineResidentIdForms.concat(idParams); await storeData(forms, 'offlineIDForms'); return localObject; - } + } const idData = [idParams]; await storeData(idData, 'offlineIDForms'); return localObject; - }); }; @@ -137,65 +135,61 @@ function postSupplementaryAssetForm(postParams) { }); } -function postHousehold(postParams) { - return new Promise((resolve, reject) => { - checkOnlineStatus().then((connected) => { - if (connected) { - postObjectsToClass(postParams).then((result) => { - resolve(result.id); - }, (error) => { - reject(error); - }); - } else { - getData('offlineHouseholds').then(async (households) => { - const id = `Household-${generateRandomID()}`; - const householdParams = postParams; - householdParams.localObject.objectId = id; - if (households !== null || households === []) { - const forms = households.concat(householdParams); - await storeData(forms, 'offlineHouseholds'); - resolve(id); - } else { - const householdData = [householdParams]; - // idData[id] = postParams; - await storeData(householdData, 'offlineHouseholds'); - resolve(id); - } - }); - } - }); +/** + * Function to post household form. Used for creating a new household + * @param {*} postParams + * @returns Househould object + */ +const postHousehold = async (postParams) => { + const isConnected = await checkOnlineStatus(); + + if (isConnected) { + return postObjectsToClass(postParams).then((result) => result.id).catch((error) => error); + } + + return getData('offlineHouseholds').then(async (offlineHouseholds) => { + const households = offlineHouseholds; + const householdParams = postParams; + + const { localObject } = householdParams; + localObject.objectId = `Household-${generateRandomID()}`; + + if (households) { + const forms = households.concat(householdParams); + await storeData(forms, 'offlineHouseholds'); + return localObject; + } + + const householdData = [householdParams]; + await storeData(householdData, 'offlineHouseholds'); + return localObject; }); -} +}; -function postHouseholdWithRelation(postParams) { - return new Promise((resolve, reject) => { - checkOnlineStatus().then((connected) => { - if (connected) { - postObjectsToClassWithRelation(postParams).then((result) => { - resolve(result.id); - }, (error) => { - reject(error); - }); - } else { - getData('offlineHouseholdsRelation').then(async (householdsRelation) => { - const id = `Household-${generateRandomID()}`; - const householdParams = postParams; - householdParams.localObject.objectId = id; - if (householdsRelation !== null || householdsRelation === []) { - const forms = householdsRelation.concat(householdParams); - await storeData(forms, 'offlineHouseholdsRelation'); - resolve(id); - } else { - const householdData = [householdParams]; - // idData[id] = postParams; - await storeData(householdData, 'offlineHouseholdsRelation'); - resolve(id); - } - }); - } - }); +const postHouseholdWithRelation = async (postParams) => { + const isConnected = await checkOnlineStatus(); + + if (isConnected) { + return postObjectsToClassWithRelation(postParams).then((result) => result.id).catch((er) => er); + } + + return getData('offlineHouseholdsRelation').then(async (householdsRelation) => { + const allOfflineHouseholdsWithRelationships = householdsRelation; + const householdParams = postParams; + const { localObject } = householdParams; + localObject.objectId = `Household-${generateRandomID()}`; + + if (allOfflineHouseholdsWithRelationships) { + const forms = allOfflineHouseholdsWithRelationships.concat(householdParams); + await storeData(forms, 'offlineHouseholdsRelation'); + return localObject; + } + + const householdData = [householdParams]; + await storeData(householdData, 'offlineHouseholdsRelation'); + return localObject; }); -} +}; export { postAssetForm,