-
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 pull request #5977 from beyondessential/dev
merge: update branch with latest dev
- Loading branch information
Showing
8 changed files
with
123 additions
and
808 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
packages/database/src/__tests__/modelClasses/SurveyResponse.test.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,67 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { getLeaderboard } from '../../modelClasses/SurveyResponse'; | ||
|
||
const USERS_EXCLUDED_FROM_LEADER_BOARD = [ | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
"'[email protected]'", | ||
]; | ||
const SYSTEM_USERS = ["'[email protected]'", "'[email protected]'", "'[email protected]'"]; | ||
|
||
const whitespace = /\s/g; | ||
const expectToBe = (expected, received) => { | ||
expect(received.replace(whitespace, '')).toBe(expected.replace(whitespace, '')); | ||
}; | ||
|
||
describe('getLeaderboard()', () => { | ||
it('should filter out internal users on standard projects', async () => { | ||
const expectedLeaderboard = `SELECT r.user_id, user_account.first_name, user_account.last_name, r.coconuts, r.pigs | ||
FROM ( | ||
SELECT user_id, FLOOR(COUNT(*)) as coconuts, FLOOR(COUNT(*) / 100) as pigs | ||
FROM survey_response | ||
JOIN survey on survey.id=survey_id | ||
WHERE survey.project_id = ? | ||
GROUP BY user_id | ||
) r | ||
JOIN user_account on user_account.id = r.user_id | ||
WHERE email NOT IN (${[...SYSTEM_USERS, ...USERS_EXCLUDED_FROM_LEADER_BOARD].join(', ')}) | ||
AND email NOT LIKE '%@beyondessential.com.au' AND email NOT LIKE '%@bes.au' | ||
ORDER BY coconuts DESC | ||
LIMIT ?;`; | ||
|
||
expectToBe(getLeaderboard('5dfc6eaf61f76a497716cddf'), expectedLeaderboard); | ||
}); | ||
|
||
it('should not filter out internal users on internal projects', async () => { | ||
const INTERNAL_PROJECT_IDS = [ | ||
'6684ac9d0f018e110b000a00', // bes_asset_demo | ||
'66a03660718c54751609eeed', // bes_asset_tracker | ||
'6704622a45a4fc4941071605', // bes_reporting | ||
]; | ||
const expectedLeaderboard = `SELECT r.user_id, user_account.first_name, user_account.last_name, r.coconuts, r.pigs | ||
FROM ( | ||
SELECT user_id, FLOOR(COUNT(*)) as coconuts, FLOOR(COUNT(*) / 100) as pigs | ||
FROM survey_response | ||
JOIN survey on survey.id=survey_id | ||
WHERE survey.project_id = ? | ||
GROUP BY user_id | ||
) r | ||
JOIN user_account on user_account.id = r.user_id | ||
WHERE email NOT IN (${SYSTEM_USERS.join(', ')}) | ||
ORDER BY coconuts DESC | ||
LIMIT ?;`; | ||
|
||
INTERNAL_PROJECT_IDS.forEach(projectId => { | ||
expectToBe(getLeaderboard(projectId), expectedLeaderboard); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -16,12 +16,44 @@ const USERS_EXCLUDED_FROM_LEADER_BOARD = [ | |
"'[email protected]'", // Andrew | ||
"'[email protected]'", // Gerry K | ||
"'[email protected]'", // Geoff F | ||
"'[email protected]'", // mSupply API Client | ||
"'[email protected]'", // Laos Schools Data Collector | ||
]; | ||
const SYSTEM_USERS = [ | ||
"'[email protected]'", // Tamanu Server | ||
"'[email protected]'", // Public User | ||
"'[email protected]'", // mSupply API Client | ||
]; | ||
const INTERNAL_EMAIL = ['@beyondessential.com.au', '@bes.au']; | ||
const INTERNAL_PROJECT_IDS = [ | ||
'6684ac9d0f018e110b000a00', // bes_asset_demo | ||
'66a03660718c54751609eeed', // bes_asset_tracker | ||
'6704622a45a4fc4941071605', // bes_reporting | ||
]; | ||
|
||
export function getLeaderboard(projectId = '') { | ||
const isInternalProject = projectId && INTERNAL_PROJECT_IDS.includes(projectId); | ||
|
||
const besUsersFilter = `AND ${INTERNAL_EMAIL.map(email => `email NOT LIKE '%${email}'`).join(' AND ')}`; | ||
const excludedUserAccountList = isInternalProject | ||
? SYSTEM_USERS | ||
: [...SYSTEM_USERS, ...USERS_EXCLUDED_FROM_LEADER_BOARD]; | ||
|
||
// FLOOR to force result to be returned as int, not string | ||
return `SELECT r.user_id, user_account.first_name, user_account.last_name, r.coconuts, r.pigs | ||
FROM ( | ||
SELECT user_id, FLOOR(COUNT(*)) as coconuts, FLOOR(COUNT(*) / 100) as pigs | ||
FROM survey_response | ||
JOIN survey on survey.id=survey_id | ||
${projectId ? 'WHERE survey.project_id = ?' : ''} | ||
GROUP BY user_id | ||
) r | ||
JOIN user_account on user_account.id = r.user_id | ||
WHERE email NOT IN (${excludedUserAccountList.join(',')}) | ||
${!isInternalProject ? besUsersFilter : ''} | ||
ORDER BY coconuts DESC | ||
LIMIT ?; | ||
`; | ||
} | ||
|
||
export class SurveyResponseRecord extends DatabaseRecord { | ||
static databaseRecord = RECORDS.SURVEY_RESPONSE; | ||
|
@@ -38,23 +70,7 @@ export class SurveyResponseModel extends MaterializedViewLogDatabaseModel { | |
|
||
async getLeaderboard(projectId = '', rowCount = 10) { | ||
const bindings = projectId ? [projectId, rowCount] : [rowCount]; | ||
return this.database.executeSql( | ||
`SELECT r.user_id, user_account.first_name, user_account.last_name, r.coconuts, r.pigs | ||
FROM ( | ||
SELECT user_id, FLOOR(COUNT(*)) as coconuts, FLOOR(COUNT(*) / 100) as pigs | ||
-- ^~~~~~~~~~~~~~~ FLOOR to force result to be returned as int, not string | ||
FROM survey_response | ||
JOIN survey on survey.id=survey_id | ||
${projectId ? 'WHERE survey.project_id = ?' : ''} | ||
GROUP BY user_id | ||
) r | ||
JOIN user_account on user_account.id = r.user_id | ||
WHERE ${INTERNAL_EMAIL.map(email => `email NOT LIKE '%${email}'`).join(' AND ')} | ||
AND email NOT IN (${USERS_EXCLUDED_FROM_LEADER_BOARD.join(',')}) | ||
ORDER BY coconuts DESC | ||
LIMIT ?; | ||
`, | ||
bindings, | ||
); | ||
const query = getLeaderboard(projectId); | ||
return this.database.executeSql(query, bindings); | ||
} | ||
} |
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
Oops, something went wrong.