-
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-423: Add 'Sync Groups' to the Admin Panel (kobo sync link part 1) #4088
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
49a97ab
Refactor: Move survey code in sync group table
IgorNadj f244dcf
Refactor: rename action for clarity
IgorNadj 9179039
Minor: remove obsolete code
IgorNadj f710e51
Add CRUD for data service data groups
IgorNadj b3a3027
Remove trigger from table
IgorNadj 20c68eb
Minor: improve error message
IgorNadj e6789f3
Add Sync Groups page
IgorNadj 0c70dc3
Add permissions checks to sync groups CRUD
IgorNadj f33a7c4
RN-423: Reworked migration to now keep the 'code' column
rohan-bes 8369ebc
RN-423: Renamed 'syncGroupCode' to 'historicSyncGroupCode'
rohan-bes 603ad3a
RN-423: Remove 'historicSyncGroupCode' entirely
rohan-bes fbfbfb2
RN-423: Remove Sync Service model, and move sync logging to Sync Grou…
rohan-bes dca2df8
RN-423: Fixed up incorrect references to old syncGroupCode in config
rohan-bes d86e19b
RN-423: Fix reference to dataGroupCode -> data_group_code
rohan-bes 1a3d0f2
RN-423: Fixed tests
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
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
81 changes: 81 additions & 0 deletions
81
packages/admin-panel/src/pages/resources/SyncGroupsPage.js
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,81 @@ | ||
/** | ||
* Tupaia MediTrak | ||
* Copyright (c) 2017 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { ResourcePage } from './ResourcePage'; | ||
|
||
const SERVICE_TYPES = [{ label: 'Kobo', value: 'kobo' }]; | ||
|
||
const FIELDS = [ | ||
{ | ||
Header: 'Survey Code', | ||
source: 'data_group_code', | ||
}, | ||
{ | ||
Header: 'Service Type', | ||
source: 'service_type', | ||
editConfig: { | ||
options: SERVICE_TYPES, | ||
}, | ||
}, | ||
{ | ||
Header: 'Config', | ||
source: 'config', | ||
type: 'jsonTooltip', | ||
editConfig: { | ||
type: 'jsonEditor', | ||
default: '{}', | ||
}, | ||
}, | ||
]; | ||
|
||
const COLUMNS = [ | ||
...FIELDS, | ||
{ | ||
Header: 'Edit', | ||
type: 'edit', | ||
source: 'id', | ||
actionConfig: { | ||
editEndpoint: 'dataServiceSyncGroups', | ||
fields: [...FIELDS], | ||
}, | ||
}, | ||
{ | ||
Header: 'Delete', | ||
source: 'id', | ||
type: 'delete', | ||
actionConfig: { | ||
endpoint: 'dataServiceSyncGroups', | ||
}, | ||
}, | ||
]; | ||
|
||
const EDIT_CONFIG = { | ||
title: 'Edit Sync Group', | ||
}; | ||
|
||
const CREATE_CONFIG = { | ||
title: 'Add Sync Group', | ||
actionConfig: { | ||
editEndpoint: 'dataServiceSyncGroups', | ||
fields: FIELDS, | ||
}, | ||
}; | ||
|
||
export const SyncGroupsPage = ({ getHeaderEl }) => ( | ||
<ResourcePage | ||
title="Sync Groups" | ||
endpoint="dataServiceSyncGroups" | ||
columns={COLUMNS} | ||
editConfig={EDIT_CONFIG} | ||
createConfig={CREATE_CONFIG} | ||
getHeaderEl={getHeaderEl} | ||
/> | ||
); | ||
|
||
SyncGroupsPage.propTypes = { | ||
getHeaderEl: PropTypes.func.isRequired, | ||
}; |
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
26 changes: 26 additions & 0 deletions
26
packages/central-server/src/apiV2/syncGroups/CreateSyncGroups.js
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,26 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { CreateHandler } from '../CreateHandler'; | ||
import { | ||
assertAnyPermissions, | ||
assertAdminPanelAccess, | ||
assertBESAdminAccess, | ||
} from '../../permissions'; | ||
|
||
export class CreateSyncGroups extends CreateHandler { | ||
async assertUserHasAccess() { | ||
await this.assertPermissions( | ||
assertAnyPermissions( | ||
[assertBESAdminAccess, assertAdminPanelAccess], | ||
'You need either BES Admin or Tupaia Admin Panel access to create a Sync Group', | ||
), | ||
); | ||
} | ||
|
||
async createRecord() { | ||
return this.insertRecord(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/central-server/src/apiV2/syncGroups/DeleteSyncGroups.js
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,17 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { DeleteHandler } from '../DeleteHandler'; | ||
import { assertAnyPermissions, assertBESAdminAccess } from '../../permissions'; | ||
import { assertSyncGroupEditPermissions } from './assertSyncGroupPermissions'; | ||
|
||
export class DeleteSyncGroups extends DeleteHandler { | ||
async assertUserHasAccess() { | ||
const syncGroupChecker = accessPolicy => | ||
assertSyncGroupEditPermissions(accessPolicy, this.models, this.recordId); | ||
|
||
await this.assertPermissions(assertAnyPermissions([assertBESAdminAccess, syncGroupChecker])); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/central-server/src/apiV2/syncGroups/EditSyncGroups.js
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,21 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { EditHandler } from '../EditHandler'; | ||
import { assertAnyPermissions, assertBESAdminAccess } from '../../permissions'; | ||
import { assertSyncGroupEditPermissions } from './assertSyncGroupPermissions'; | ||
|
||
export class EditSyncGroups extends EditHandler { | ||
async assertUserHasAccess() { | ||
const syncGroupChecker = accessPolicy => | ||
assertSyncGroupEditPermissions(accessPolicy, this.models, this.recordId); | ||
|
||
await this.assertPermissions(assertAnyPermissions([assertBESAdminAccess, syncGroupChecker])); | ||
} | ||
|
||
async editRecord() { | ||
await this.updateRecord(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/central-server/src/apiV2/syncGroups/GETSyncGroups.js
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,20 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { GETHandler } from '../GETHandler'; | ||
import { assertAdminPanelAccess } from '../../permissions'; | ||
import { createSyncGroupDBFilter } from './assertSyncGroupPermissions'; | ||
|
||
export class GETSyncGroups extends GETHandler { | ||
permissionsFilteredInternally = true; | ||
|
||
async assertUserHasAccess() { | ||
await this.assertPermissions(assertAdminPanelAccess); | ||
} | ||
|
||
async getPermissionsFilter(criteria, options) { | ||
return createSyncGroupDBFilter(this.accessPolicy, this.models, criteria, options); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/central-server/src/apiV2/syncGroups/assertSyncGroupPermissions.js
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,75 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { QUERY_CONJUNCTIONS, TYPES } from '@tupaia/database'; | ||
import { assertSurveyEditPermissions } from '../surveys/assertSurveyPermissions'; | ||
import { hasBESAdminAccess } from '../../permissions'; | ||
import { fetchCountryIdsByPermissionGroupId, mergeMultiJoin } from '../utilities'; | ||
|
||
const { RAW } = QUERY_CONJUNCTIONS; | ||
|
||
export const assertSyncGroupEditPermissions = async (accessPolicy, models, syncGroupId) => { | ||
const syncGroup = await models.dataServiceSyncGroup.findById(syncGroupId); | ||
if (!syncGroup) { | ||
throw new Error(`No Sync Group exists with id ${syncGroupId}`); | ||
} | ||
|
||
const dataGroup = await models.dataGroup.findOne({ code: syncGroup.data_group_code }); | ||
if (!dataGroup) { | ||
throw new Error(`Sync Group is not linked to an existing Data Group`); | ||
} | ||
|
||
const survey = await models.survey.findOne({ data_group_id: dataGroup.id }); | ||
if (!survey) { | ||
throw new Error(`No Survey found for Data Group used by Sync Group`); | ||
} | ||
|
||
return assertSurveyEditPermissions(accessPolicy, models, survey.id); | ||
}; | ||
|
||
export const createSyncGroupDBFilter = async (accessPolicy, models, criteria, options) => { | ||
const dbConditions = { ...criteria }; | ||
const dbOptions = { ...options }; | ||
|
||
if (hasBESAdminAccess(accessPolicy)) { | ||
return { dbConditions, dbOptions }; | ||
} | ||
|
||
const countryIdsByPermissionGroupId = await fetchCountryIdsByPermissionGroupId( | ||
accessPolicy, | ||
models, | ||
); | ||
|
||
dbOptions.multiJoin = mergeMultiJoin( | ||
[ | ||
{ | ||
joinWith: TYPES.DATA_GROUP, | ||
joinCondition: [ | ||
`${TYPES.DATA_GROUP}.code`, | ||
`${TYPES.DATA_SERVICE_SYNC_GROUP}.data_group_code`, | ||
], | ||
}, | ||
{ | ||
joinWith: TYPES.SURVEY, | ||
joinCondition: [`${TYPES.SURVEY}.data_group_id`, `${TYPES.DATA_GROUP}.id`], | ||
}, | ||
], | ||
dbOptions.multiJoin, | ||
); | ||
|
||
dbConditions[RAW] = { | ||
sql: ` | ||
( | ||
survey.country_ids | ||
&& | ||
ARRAY( | ||
SELECT TRIM('"' FROM JSON_ARRAY_ELEMENTS(?::JSON->survey.permission_group_id)::TEXT) | ||
) | ||
)`, | ||
parameters: JSON.stringify(countryIdsByPermissionGroupId), | ||
}; | ||
|
||
return { dbConditions, dbOptions }; | ||
}; |
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,9 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export { DeleteSyncGroups } from './DeleteSyncGroups'; | ||
export { EditSyncGroups } from './EditSyncGroups'; | ||
export { GETSyncGroups } from './GETSyncGroups'; | ||
export { CreateSyncGroups } from './CreateSyncGroups'; |
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.
We may need to consider how to change this permission check... I suspect there will be some lesmis users who want to configure the sync group but don't have BES Admin...