-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into odds-and-sods
- Loading branch information
Showing
123 changed files
with
2,550 additions
and
796 deletions.
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
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
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
31 changes: 31 additions & 0 deletions
31
.../migrations/20240303205520-AddEntityAttributesFilterToMapOverlaysTable-modifies-schema.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,31 @@ | ||
'use strict'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
}; | ||
|
||
exports.up = function (db) { | ||
return db.addColumn('map_overlay', 'entity_attributes_filter', { | ||
type: 'jsonb', | ||
defaultValue: '{}', | ||
notNull: true, | ||
}); | ||
}; | ||
|
||
exports.down = function (db) { | ||
return db.removeColumn('map_overlay', 'entity_attributes_filter'); | ||
}; | ||
|
||
exports._meta = { | ||
version: 1, | ||
}; |
100 changes: 100 additions & 0 deletions
100
...atabase/src/migrations/20240305204428-ChangeNestedDelimiterReportConfigs-modifies-data.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,100 @@ | ||
'use strict'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
}; | ||
|
||
const getReportsWithDataSourceEntityFilter = async db => { | ||
// Get all reports with dataSourceEntityFilter set | ||
const reports = await db.runSql( | ||
`SELECT * from report where config->>'transform' LIKE '%dataSourceEntityFilter%';`, | ||
); | ||
return reports.rows; | ||
}; | ||
|
||
const reformatReportConfig = (report, initialDelimiter, newDelimiter) => { | ||
const { config } = report; | ||
|
||
const newTransforms = config.transform.map(transform => { | ||
// If the transform doesn't have aggregations, return it as is | ||
if (!transform.parameters?.aggregations) return transform; | ||
|
||
// If the transform has aggregations, update the dataSourceEntityFilter to use standard jsonb syntax | ||
const updatedAggregations = transform.parameters.aggregations.map(aggregation => { | ||
if (aggregation.config?.dataSourceEntityFilter) { | ||
return { | ||
...aggregation, | ||
config: { | ||
...aggregation.config, | ||
dataSourceEntityFilter: Object.entries( | ||
aggregation.config.dataSourceEntityFilter, | ||
).reduce((result, [key, value]) => { | ||
const newKey = key.replace(initialDelimiter, newDelimiter); | ||
return { | ||
...result, | ||
[newKey]: value, | ||
}; | ||
}, {}), | ||
}, | ||
}; | ||
} | ||
return aggregation; | ||
}); | ||
|
||
return { | ||
...transform, | ||
parameters: { | ||
...transform.parameters, | ||
aggregations: updatedAggregations, | ||
}, | ||
}; | ||
}); | ||
return { | ||
...config, | ||
transform: newTransforms, | ||
}; | ||
}; | ||
|
||
const updateReport = async (db, report, newConfig) => { | ||
await db.runSql( | ||
`UPDATE report SET config = '${JSON.stringify(newConfig).replaceAll( | ||
"'", | ||
"''", | ||
)}'::json WHERE id = '${report.id}'`, | ||
); | ||
}; | ||
|
||
exports.up = async function (db) { | ||
const reportsToChange = await getReportsWithDataSourceEntityFilter(db); | ||
await Promise.all( | ||
reportsToChange.map(report => { | ||
const newConfig = reformatReportConfig(report, '_', '->>'); | ||
return updateReport(db, report, newConfig); | ||
}), | ||
); | ||
return null; | ||
}; | ||
|
||
exports.down = async function (db) { | ||
const reportsToChange = await getReportsWithDataSourceEntityFilter(db); | ||
await Promise.all( | ||
reportsToChange.map(report => { | ||
const newConfig = reformatReportConfig(report, '->>', '_'); | ||
return updateReport(db, report, newConfig); | ||
}), | ||
); | ||
}; | ||
|
||
exports._meta = { | ||
version: 1, | ||
}; |
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
29 changes: 29 additions & 0 deletions
29
packages/datatrak-web-server/src/app/middleware/attachAccessPolicy.ts
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,29 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { RequestHandler } from 'express'; | ||
import { AccessPolicyBuilder, mergeAccessPolicies } from '@tupaia/auth'; | ||
import { AccessPolicy } from '@tupaia/access-policy'; | ||
|
||
const { API_CLIENT_NAME } = process.env; | ||
|
||
export const attachAccessPolicy: RequestHandler = async (req, res, next) => { | ||
const accessPolicyBuilder = new AccessPolicyBuilder(req.models); | ||
const apiUser = await req.models.user.findOne({ email: API_CLIENT_NAME }); | ||
if (!apiUser) { | ||
throw new Error('API Client not found'); | ||
} | ||
const apiAccessPolicy = await accessPolicyBuilder.getPolicyForUser(apiUser.id); | ||
|
||
// If we have a session, merge it with the api user | ||
// Otherwise just use the api user policy | ||
req.accessPolicy = new AccessPolicy( | ||
req.session | ||
? mergeAccessPolicies(req.session.accessPolicy.policy, apiAccessPolicy) | ||
: apiAccessPolicy, | ||
); | ||
|
||
next(); | ||
}; |
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,6 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export { attachAccessPolicy } from './attachAccessPolicy'; |
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.