-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RN-400: Meditrak permissions based sync (server-side changes) #3992
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f0f93c8
RN-400: Added array_concat_agg postgres aggregation functions for com…
rohan-bes 3dc7a58
RN-400: Added permissions_based_meditrak_sync_queue
rohan-bes 142d595
RN-400: Aded MeditrakSyncQueue ChangeHandler which manages the
rohan-bes 163f85f
RN-400: Changed SqlQuery.array() to SqlQuery.record()
rohan-bes 8e9c7ce
RN-400: Added logic to filter syncing by only data the device has per…
rohan-bes aeceffc
RN-400: Fix tests
rohan-bes 7662117
RN-400: Renamed 'getChangesFilter'
rohan-bes a9979fe
RN-400: Addressed PR comments
rohan-bes 8c4f290
RN-400: Switch to CREATE OR REPLACE aggregate
rohan-bes 05aa2e9
RN-400: Meditrak permissions based sync (app-side changes) (#3995)
rohan-bes 08302b3
RN-400: Fixed not all data syncing if user permissions change
rohan-bes da0e877
RN-400: Reworked permissions based sync query building
rohan-bes b3e8c08
RN-400: Reverted change to use permissions based sync on changes/coun…
rohan-bes 56df105
RN-400: Added unit tests for permissions based sync
rohan-bes 7a657ae
RN-400: Moved building of permissions based meditrak sync queue view …
rohan-bes 9f6da9b
Merge branch 'dev' into rn-400-permissions-based-sync-p1
rohan-bes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { respond } from '@tupaia/utils'; | ||
import { buildPermissionsBasedMeditrakSyncQuery } from './utilities'; | ||
import { allowNoPermissions } from '../permissions'; | ||
|
||
/** | ||
* Permissions based sync metadata | ||
* { | ||
* changeCount: number of changes since last sync | ||
* countries: countries included in the sync | ||
* permissionGroups: permissions groups included in the sync | ||
* } | ||
* Responds to GET requests to the /changes/metadata endpoint | ||
*/ | ||
export async function changesMetadata(req, res) { | ||
const { models } = req; | ||
|
||
await req.assertPermissions(allowNoPermissions); | ||
|
||
const { query, countries, permissionGroups } = await buildPermissionsBasedMeditrakSyncQuery(req, { | ||
select: 'count(*)', | ||
}); | ||
const queryResult = await query.executeOnDatabase(models.database); | ||
const changeCount = parseInt(queryResult[0].count); | ||
respond(res, { changeCount, countries, permissionGroups }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,24 @@ | |
* Copyright (c) 2017 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import keyBy from 'lodash.keyby'; | ||
import groupBy from 'lodash.groupby'; | ||
import { respond, DatabaseError } from '@tupaia/utils'; | ||
import { TYPES } from '@tupaia/database'; | ||
import { getChangesFilter, getColumnsForMeditrakApp } from './utilities'; | ||
import { | ||
supportsPermissionsBasedSync, | ||
buildMeditrakSyncQuery, | ||
buildPermissionsBasedMeditrakSyncQuery, | ||
getColumnsForMeditrakApp, | ||
} from './utilities'; | ||
import { allowNoPermissions } from '../permissions'; | ||
|
||
const MAX_CHANGES_RETURNED = 100; | ||
|
||
/** | ||
* Gets the record ready to sync down to a sync client, transforming any properties as required | ||
*/ | ||
async function getRecordForSync(record) { | ||
function getRecordForSync(record) { | ||
const recordWithoutNulls = {}; | ||
// Remove null entries to a) save bandwidth and b) remain consistent with previous mongo based db | ||
// which simply had no key for undefined properties, whereas postgres uses null | ||
|
@@ -34,45 +41,67 @@ async function getRecordForSync(record) { | |
*/ | ||
export async function getChanges(req, res) { | ||
const { database, models } = req; | ||
const { limit = MAX_CHANGES_RETURNED, offset = 0 } = req.query; | ||
const { limit = MAX_CHANGES_RETURNED, offset = 0, appVersion } = req.query; | ||
|
||
await req.assertPermissions(allowNoPermissions); | ||
|
||
try { | ||
const filter = await getChangesFilter(req); | ||
const changes = await database.find(TYPES.MEDITRAK_SYNC_QUEUE, filter, { | ||
sort: ['change_time'], | ||
const queryBuilder = supportsPermissionsBasedSync(appVersion) | ||
? buildPermissionsBasedMeditrakSyncQuery | ||
: buildMeditrakSyncQuery; | ||
const { query } = await queryBuilder(req, { | ||
select: (await models.meditrakSyncQueue.fetchFieldNames()).join(', '), | ||
sort: 'change_time ASC', | ||
limit, | ||
offset, | ||
}); | ||
const changesToSend = await Promise.all( | ||
changes.map(async change => { | ||
const { | ||
type: action, | ||
record_type: recordType, | ||
record_id: recordId, | ||
change_time: timestamp, | ||
} = change; | ||
const columns = await getColumnsForMeditrakApp(models.getModelForDatabaseType(recordType)); | ||
const changeObject = { action, recordType, timestamp }; | ||
if (action === 'delete') { | ||
changeObject.record = { id: recordId }; | ||
if (recordType === TYPES.GEOGRAPHICAL_AREA) { | ||
// TODO LEGACY Deal with this bug on app end for v3 api | ||
changeObject.recordType = 'area'; | ||
} | ||
const changes = await query.executeOnDatabase(database); | ||
const changesByRecordType = groupBy(changes, 'record_type'); | ||
const recordTypesToSync = Object.keys(changesByRecordType); | ||
const columnNamesByRecordType = Object.fromEntries( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rewrote this to do all the database stuff upfront in batch. Made server-side processing of changes much faster. Now the bottleneck is on the app side write to database. |
||
await Promise.all( | ||
recordTypesToSync.map(async recordType => [ | ||
recordType, | ||
await getColumnsForMeditrakApp(models.getModelForDatabaseType(recordType)), | ||
]), | ||
), | ||
); | ||
const changeRecords = ( | ||
await Promise.all( | ||
Object.entries(changesByRecordType).map(async ([recordType, changesForType]) => { | ||
const changeIds = changesForType.map(change => change.record_id); | ||
const columns = columnNamesByRecordType[recordType]; | ||
return database.find(recordType, { id: changeIds }, { lean: true, columns }); | ||
}), | ||
) | ||
).flat(); | ||
const changeRecordsById = keyBy(changeRecords, 'id'); | ||
|
||
const changesToSend = changes.map(change => { | ||
const { | ||
type: action, | ||
record_type: recordType, | ||
record_id: recordId, | ||
change_time: timestamp, | ||
} = change; | ||
const changeObject = { action, recordType, timestamp }; | ||
if (action === 'delete') { | ||
changeObject.record = { id: recordId }; | ||
if (recordType === TYPES.GEOGRAPHICAL_AREA) { | ||
// TODO LEGACY Deal with this bug on app end for v3 api | ||
changeObject.recordType = 'area'; | ||
} | ||
} else { | ||
const record = changeRecordsById[recordId]; | ||
if (!record) { | ||
const errorMessage = `Couldn't find record type ${recordType} with id ${recordId}`; | ||
changeObject.error = { error: errorMessage }; | ||
} else { | ||
const record = await database.findById(recordType, recordId, { lean: true, columns }); | ||
if (!record) { | ||
const errorMessage = `Couldn't find record type ${recordType} with id ${recordId}`; | ||
changeObject.error = { error: errorMessage }; | ||
} else { | ||
changeObject.record = await getRecordForSync(record); | ||
} | ||
changeObject.record = getRecordForSync(record); | ||
} | ||
return changeObject; | ||
}), | ||
); | ||
} | ||
return changeObject; | ||
}); | ||
respond(res, changesToSend); | ||
return; | ||
} catch (error) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 0 additions & 80 deletions
80
packages/central-server/src/apiV2/utilities/getChangesFilter.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this endpoint returns more/different data to the
/changes/count
endpoint, I decided to create a new one with a new name. Also makes maintaining support for the old logic a little easier.