Skip to content

Commit

Permalink
Force requests to complete on syncing (#313)
Browse files Browse the repository at this point in the history
For #310
Replace `forEach` with `Promise.all` to enforce requests completion
  • Loading branch information
lmatayoshi authored Jun 17, 2020
1 parent c46e7c4 commit 1bc2e3b
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 166 deletions.
110 changes: 61 additions & 49 deletions app/actions/electronicPharmacyStockRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORDS =
'FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORDS';
const FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORD =
'FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORD';
const UPLOAD_ELECTRONIC_PHARMACY_STOCK_RECORDS =
'UPLOAD_ELECTRONIC_PHARMACY_STOCK_RECORDS';

const uploadMapper = async (attr, record) => {
const { rows, ...withoutRows } = attr;
Expand All @@ -31,8 +33,10 @@ export const fetchElectronicPharmacyStockRecord = fetchEntitySingular(
'ElectronicPharmacyStockRecord'
);

export const syncElectronicPharmacyStockRecords = () => async dispatch =>
dispatch(uploadNewElectronicPharmacyStockRecords());
export const syncElectronicPharmacyStockRecords = () => async dispatch => {
dispatch({ type: UPLOAD_ELECTRONIC_PHARMACY_STOCK_RECORDS });
return dispatch(uploadNewElectronicPharmacyStockRecords());
};

export const uploadNewElectronicPharmacyStockRecords = () => async (
dispatch,
Expand All @@ -49,55 +53,63 @@ export const uploadNewElectronicPharmacyStockRecords = () => async (
});
if (collectionToCreate.length === 0) return;

collectionToCreate.forEach(async electronicPharmacyStockRecord => {
const body = new FormData();
const contents = fs.readFileSync(electronicPharmacyStockRecord.filePath);
const blob = new Blob([contents]);
const electronicPharmacyValues = electronicPharmacyStockRecord.dataValues;
const mapper = await uploadMapper(
electronicPharmacyValues,
electronicPharmacyStockRecord
);
const rowsFile = createJSONFile(electronicPharmacyValues.rows, 'rows.json');
return Promise.all(
collectionToCreate.map(async electronicPharmacyStockRecord => {
const body = new FormData();
const contents = fs.readFileSync(electronicPharmacyStockRecord.filePath);
const blob = new Blob([contents]);
const electronicPharmacyValues = electronicPharmacyStockRecord.dataValues;
const mapper = await uploadMapper(
electronicPharmacyValues,
electronicPharmacyStockRecord
);
const rowsFile = createJSONFile(
electronicPharmacyValues.rows,
'rows.json'
);

// eslint-disable-next-line
Object.keys(mapper).forEach(key => {
if (isObject(mapper[key])) {
body.append(key, JSON.stringify(mapper[key]));
} else {
body.append(key, mapper[key]);
}
});
body.append('sheet_file', blob);
body.append('rows_file', rowsFile);
fetchAuthenticated('/api/v1/electronic_pharmacy_stock_records', user.auth, {
method: 'POST',
body,
contentType: null
})
.then(res =>
electronicPharmacyStockRecord.update({
remoteId: res.id,
lastSyncAt: new Date()
})
// eslint-disable-next-line
Object.keys(mapper).forEach(key => {
if (isObject(mapper[key])) {
body.append(key, JSON.stringify(mapper[key]));
} else {
body.append(key, mapper[key]);
}
});
body.append('sheet_file', blob);
body.append('rows_file', rowsFile);
return fetchAuthenticated(
'/api/v1/electronic_pharmacy_stock_records',
user.auth,
{
method: 'POST',
body,
contentType: null
}
)
.then(() => {
fs.unlink(
electronicPharmacyStockRecord.filePath,
e =>
e
? console.log(e)
: console.log(
`${
electronicPharmacyStockRecord.filePath
} file deleted successfully`
)
);
return Promise.resolve();
})
.catch(e => console.log(e));
});
return Promise.resolve();
.then(res =>
electronicPharmacyStockRecord.update({
remoteId: res.id,
lastSyncAt: new Date()
})
)
.then(() => {
fs.unlink(
electronicPharmacyStockRecord.filePath,
e =>
e
? console.log(e)
: console.log(
`${
electronicPharmacyStockRecord.filePath
} file deleted successfully`
)
);
return Promise.resolve();
})
.catch(e => console.log(e));
})
);
};

export {
Expand Down
129 changes: 67 additions & 62 deletions app/actions/labRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { fetchEntity } from './fetch';

const FETCH_LAB_RECORDS = 'FETCH_LAB_RECORDS';
const FETCHED_LAB_RECORDS = 'FETCHED_LAB_RECORDS';
const UPLOAD_LAB_RECORDS = 'UPLOAD_LAB_RECORDS';
const FETCH_LAB_RECORDS_FAILED = 'FETCH_LAB_RECORDS_FAILED';
const FETCHED_LAB_RECORD = 'FETCHED_LAB_RECORD';
const FETCHING_LAB_RECORD = 'FETCHING_LAB_RECORD';
Expand All @@ -22,10 +23,12 @@ const uploadMapper = async (attr, record) => {
});
};

export const syncLabRecords = () => async dispatch =>
dispatch(uploadNewLabRecords()).then(() =>
export const syncLabRecords = () => async dispatch => {
dispatch({ type: UPLOAD_LAB_RECORDS });
return dispatch(uploadNewLabRecords()).then(() =>
dispatch(uploadUpdatedLabRecords())
);
};

export const uploadNewLabRecords = () => async (dispatch, getState) => {
const { user } = getState();
Expand All @@ -36,49 +39,50 @@ export const uploadNewLabRecords = () => async (dispatch, getState) => {
});
if (collectionToCreate.length === 0) return;

collectionToCreate.forEach(async labRecord => {
const body = new FormData();
const contents = fs.readFileSync(labRecord.filePath);
const blob = new Blob([contents]);
const labRecordValues = labRecord.dataValues;
const mapper = await uploadMapper(labRecordValues, labRecord);
const rows = labRecordValues.rows.map((row, index) => ({
content: [...row],
row: index
}));
const rowsFile = createJSONFile(rows, 'rows.json');
return Promise.all(
collectionToCreate.map(async labRecord => {
const body = new FormData();
const contents = fs.readFileSync(labRecord.filePath);
const blob = new Blob([contents]);
const labRecordValues = labRecord.dataValues;
const mapper = await uploadMapper(labRecordValues, labRecord);
const rows = labRecordValues.rows.map((row, index) => ({
content: [...row],
row: index
}));
const rowsFile = createJSONFile(rows, 'rows.json');

// eslint-disable-next-line
Object.keys(mapper).forEach(key => {
if (isObject(mapper[key])) {
body.append(key, JSON.stringify(mapper[key]));
} else {
body.append(key, mapper[key]);
}
});
body.append('sheet_file', blob);
body.append('rows_file', rowsFile);
fetchAuthenticated('/api/v1/lab_record_imports', user.auth, {
method: 'POST',
body,
contentType: null
})
.then(res =>
labRecord.update({ remoteId: res.id, lastSyncAt: new Date() })
)
.then(() => {
fs.unlink(
labRecord.filePath,
e =>
e
? console.log(e)
: console.log(`${labRecord.filePath} file deleted successfully`)
);
return Promise.resolve();
// eslint-disable-next-line
Object.keys(mapper).map(key => {
if (isObject(mapper[key])) {
body.append(key, JSON.stringify(mapper[key]));
} else {
body.append(key, mapper[key]);
}
});
body.append('sheet_file', blob);
body.append('rows_file', rowsFile);
return fetchAuthenticated('/api/v1/lab_record_imports', user.auth, {
method: 'POST',
body,
contentType: null
})
.catch(e => console.log(e));
});
return Promise.resolve();
.then(res =>
labRecord.update({ remoteId: res.id, lastSyncAt: new Date() })
)
.then(() => {
fs.unlink(
labRecord.filePath,
e =>
e
? console.log(e)
: console.log(`${labRecord.filePath} file deleted successfully`)
);
return Promise.resolve();
})
.catch(e => console.log(e));
})
);
};

export const uploadUpdatedLabRecords = () => async (dispatch, getState) => {
Expand All @@ -90,25 +94,26 @@ export const uploadUpdatedLabRecords = () => async (dispatch, getState) => {
)
});

collectionToUpdate.forEach(async labRecord => {
const body = new FormData();
body.append('rows_file', createJSONFile(labRecord.rows, 'rows.json'));
fetchAuthenticated(
`/api/v1/lab_record_imports/${labRecord.remoteId}`,
user.auth,
{
method: 'PUT',
body,
contentType: null
}
)
.then(() => {
const updatedAt = new Date();
return labRecord.update({ lastSyncAt: updatedAt, updatedAt });
})
.catch(e => console.log(e));
});
return Promise.resolve();
return Promise.all(
collectionToUpdate.map(async labRecord => {
const body = new FormData();
body.append('rows_file', createJSONFile(labRecord.rows, 'rows.json'));
return fetchAuthenticated(
`/api/v1/lab_record_imports/${labRecord.remoteId}`,
user.auth,
{
method: 'PUT',
body,
contentType: null
}
)
.then(() => {
const updatedAt = new Date();
return labRecord.update({ lastSyncAt: updatedAt, updatedAt });
})
.catch(e => console.log(e));
})
);
};

export const fetchLabRecords = fetchEntity('LabRecord');
Expand Down
Loading

0 comments on commit 1bc2e3b

Please sign in to comment.