Skip to content

Commit

Permalink
refactor: household offline
Browse files Browse the repository at this point in the history
  • Loading branch information
hopetambala committed Sep 24, 2022
1 parent ad389b7 commit a1819c1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

Expand All @@ -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);
};
Expand Down
78 changes: 58 additions & 20 deletions modules/cached-resources/Post/__tests__/post.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
126 changes: 60 additions & 66 deletions modules/cached-resources/Post/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

});
};

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a1819c1

Please sign in to comment.