diff --git a/app/actions/electronicPharmacyStockRecords.js b/app/actions/electronicPharmacyStockRecords.js index 71a0c97..8515d77 100644 --- a/app/actions/electronicPharmacyStockRecords.js +++ b/app/actions/electronicPharmacyStockRecords.js @@ -3,6 +3,7 @@ import snakeCaseKeys from 'snakecase-keys'; import { isObject } from 'lodash'; import { fetchAuthenticated } from '../utils/fetch'; import { fetchEntity, fetchEntitySingular } from './fetch'; +import createJSONFile from '../utils/attachments'; import db from '../db'; const FETCH_ELECTRONIC_PHARMACY_STOCK_RECORDS = @@ -14,12 +15,14 @@ const FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORDS = const FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORD = 'FETCHED_ELECTRONIC_PHARMACY_STOCK_RECORD'; -const uploadMapper = async (attr, record) => - snakeCaseKeys({ - ...attr, +const uploadMapper = async (attr, record) => { + const { rows, ...withoutRows } = attr; + return snakeCaseKeys({ + ...withoutRows, date: Object.values(attr.date), siteId: await record.getRemoteSiteId() }); +}; export const fetchElectronicPharmacyStockRecords = fetchEntity( 'ElectronicPharmacyStockRecord' @@ -50,10 +53,13 @@ export const uploadNewElectronicPharmacyStockRecords = () => async ( const body = new FormData(); const contents = fs.readFileSync(electronicPharmacyStockRecord.filePath); const blob = new Blob([contents]); + const electronicPharmacyValues = electronicPharmacyStockRecord.dataValues; const mapper = await uploadMapper( - electronicPharmacyStockRecord.dataValues, + electronicPharmacyValues, electronicPharmacyStockRecord ); + const rowsFile = createJSONFile(electronicPharmacyValues.rows, 'rows.json'); + // eslint-disable-next-line Object.keys(mapper).forEach(key => { if (isObject(mapper[key])) { @@ -63,6 +69,7 @@ export const uploadNewElectronicPharmacyStockRecords = () => async ( } }); body.append('sheet_file', blob); + body.append('rows_file', rowsFile); fetchAuthenticated('/api/v1/electronic_pharmacy_stock_records', user.auth, { method: 'POST', body, diff --git a/app/actions/labRecords.js b/app/actions/labRecords.js index ca43eb7..c891cf1 100644 --- a/app/actions/labRecords.js +++ b/app/actions/labRecords.js @@ -4,6 +4,7 @@ import { isObject } from 'lodash'; import db from '../db'; import { fetchAuthenticated } from '../utils/fetch'; +import createJSONFile from '../utils/attachments'; import { fetchEntity } from './fetch'; const FETCH_LAB_RECORDS = 'FETCH_LAB_RECORDS'; @@ -12,16 +13,14 @@ const FETCH_LAB_RECORDS_FAILED = 'FETCH_LAB_RECORDS_FAILED'; const FETCHED_LAB_RECORD = 'FETCHED_LAB_RECORD'; const FETCHING_LAB_RECORD = 'FETCHING_LAB_RECORD'; -const uploadMapper = async (attr, record) => - snakeCaseKeys({ - ...attr, +const uploadMapper = async (attr, record) => { + const { rows, ...withoutRows } = attr; + return snakeCaseKeys({ + ...withoutRows, date: Object.values(attr.date), - siteId: await record.getRemoteSiteId(), - lab_records_attributes: attr.rows.map((row, index) => ({ - content: [...row], - row: index - })) + siteId: await record.getRemoteSiteId() }); +}; export const syncLabRecords = () => async dispatch => dispatch(uploadNewLabRecords()).then(() => @@ -41,7 +40,18 @@ export const uploadNewLabRecords = () => async (dispatch, getState) => { const body = new FormData(); const contents = fs.readFileSync(labRecord.filePath); const blob = new Blob([contents]); - const mapper = await uploadMapper(labRecord.dataValues, labRecord); + const labRecordValues = labRecord.dataValues; + const mapper = await uploadMapper(labRecordValues, labRecord); + const labRecordAttributes = labRecordValues.rows.map((row, index) => ({ + content: [...row], + row: index + })); + const labRecordAttributesFile = createJSONFile( + labRecordAttributes, + 'lab_records_attributes.json' + ); + const rowsFile = createJSONFile(labRecordValues.rows, 'rows.json'); + // eslint-disable-next-line Object.keys(mapper).forEach(key => { if (isObject(mapper[key])) { @@ -51,6 +61,8 @@ export const uploadNewLabRecords = () => async (dispatch, getState) => { } }); body.append('sheet_file', blob); + body.append('lab_records_attributes_file', labRecordAttributesFile); + body.append('rows_file', rowsFile); fetchAuthenticated('/api/v1/lab_record_imports', user.auth, { method: 'POST', body, @@ -84,12 +96,15 @@ 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: { rows: labRecord.rows } + body, + contentType: null } ) .then(() => { diff --git a/app/utils/attachments.js b/app/utils/attachments.js new file mode 100644 index 0000000..667d372 --- /dev/null +++ b/app/utils/attachments.js @@ -0,0 +1,8 @@ +const createJSONFile = (content, filename) => { + const blob = new Blob([JSON.stringify(content)], { + type: 'application/json' + }); + return new File([blob], filename); +}; + +export default createJSONFile;