diff --git a/app/actions/electronicPharmacyStockRecords.js b/app/actions/electronicPharmacyStockRecords.js index 8515d77..86f7b04 100644 --- a/app/actions/electronicPharmacyStockRecords.js +++ b/app/actions/electronicPharmacyStockRecords.js @@ -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; @@ -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, @@ -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 { diff --git a/app/actions/labRecords.js b/app/actions/labRecords.js index be7ca94..448cc12 100644 --- a/app/actions/labRecords.js +++ b/app/actions/labRecords.js @@ -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'; @@ -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(); @@ -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) => { @@ -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'); diff --git a/app/actions/sync.js b/app/actions/sync.js index 507f99c..f80a7d6 100644 --- a/app/actions/sync.js +++ b/app/actions/sync.js @@ -5,8 +5,6 @@ import db from '../db'; import { fetchPaginated, fetchAuthenticated } from '../utils/fetch'; import { syncSites } from './sites'; -import { syncSpecimenSources } from './specimenSources'; -import { syncCultureTypes } from './cultureTypes'; import { syncAntibioticConsumptionStats } from './antibioticConsumptionStats'; import { syncAntibiotics } from './antibiotics'; import { syncLabRecords } from './labRecords'; @@ -23,14 +21,6 @@ const SYNC_FINISH = 'SYNC_FINISH'; // TODO: We should honor this order, currently the async process randomizes everything export const entities = [ - { - name: 'SpecimenSource', - syncAction: syncSpecimenSources - }, - { - name: 'CultureType', - syncAction: syncCultureTypes - }, { name: 'ClinicalService', syncAction: syncEntities('ClinicalService') @@ -226,27 +216,28 @@ export const remoteUpload = ( if (collectionToCreate.length === 0) return; - collectionToCreate.forEach(async currentEntity => { - const mapped = await Promise.resolve(mapper(currentEntity)); - fetchAuthenticated(url, user.auth, { - method: 'POST', - body: snakeCaseKeys({ [entityName]: mapped }) - }) - .then(async res => { - const existingEntity = await entity.findOne({ - where: { remoteId: res.id }, - order: [['id', 'asc']] - }); - if (existingEntity && existingEntity.id !== currentEntity.id) - existingEntity.destroy(); - return currentEntity.update({ - remoteId: res.id, - lastSyncAt: new Date() - }); + return Promise.all( + collectionToCreate.map(async currentEntity => { + const mapped = await Promise.resolve(mapper(currentEntity)); + return fetchAuthenticated(url, user.auth, { + method: 'POST', + body: snakeCaseKeys({ [entityName]: mapped }) }) - .catch(e => console.log(e)); - }); - return Promise.resolve(); + .then(async res => { + const existingEntity = await entity.findOne({ + where: { remoteId: res.id }, + order: [['id', 'asc']] + }); + if (existingEntity && existingEntity.id !== currentEntity.id) + existingEntity.destroy(); + return currentEntity.update({ + remoteId: res.id, + lastSyncAt: new Date() + }); + }) + .catch(e => console.log(e)); + }) + ); }; export const remoteUploadUpdate = ( @@ -265,33 +256,34 @@ export const remoteUploadUpdate = ( const collectionToUpdate = await entity.findAll({ where: sequelize.literal(query) }); - collectionToUpdate.forEach(async currentEntity => { - const mapped = await Promise.resolve(mapper(currentEntity)); - fetchAuthenticated(url(currentEntity.remoteId), user.auth, { - method: 'PUT', - body: snakeCaseKeys({ [entityName]: mapped }) - }) - .then(item => - entity.update( - { - lastSyncAt: moment(item.updated_at) - .local() - .toDate(), - updatedAt: moment(item.updated_at) - .local() - .toDate() - }, - { - where: { - id: currentEntity.id + return Promise.all( + collectionToUpdate.map(async currentEntity => { + const mapped = await Promise.resolve(mapper(currentEntity)); + return fetchAuthenticated(url(currentEntity.remoteId), user.auth, { + method: 'PUT', + body: snakeCaseKeys({ [entityName]: mapped }) + }) + .then(item => + entity.update( + { + lastSyncAt: moment(item.updated_at) + .local() + .toDate(), + updatedAt: moment(item.updated_at) + .local() + .toDate() }, - silent: true - } + { + where: { + id: currentEntity.id + }, + silent: true + } + ) ) - ) - .catch(e => console.log(e)); - }); - return Promise.resolve(); + .catch(e => console.log(e)); + }) + ); }; export { SYNC_START, SYNC_STOP, SYNC_FINISH };