Skip to content

Commit

Permalink
Merge pull request #5977 from beyondessential/dev
Browse files Browse the repository at this point in the history
merge: update branch with latest dev
  • Loading branch information
avaek authored Oct 22, 2024
2 parents 1bded11 + d1d048e commit 4cc4bbd
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 808 deletions.
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);
});
});
});
54 changes: 35 additions & 19 deletions packages/database/src/modelClasses/SurveyResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion packages/meditrak-app/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ android {
applicationId "com.tupaiameditrak"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 143
versionCode 144
versionName "1.14.144"
}
signingConfigs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 143;
CURRENT_PROJECT_VERSION = 144;
DEVELOPMENT_TEAM = 352QMCKRKJ;
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = 352QMCKRKJ;
ENABLE_BITCODE = NO;
Expand Down Expand Up @@ -528,7 +528,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 143;
CURRENT_PROJECT_VERSION = 144;
DEVELOPMENT_TEAM = 352QMCKRKJ;
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = 352QMCKRKJ;
INFOPLIST_FILE = TupaiaMediTrak/Info.plist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ export const ExportConfig = ({ onClose, selectedDashboardItems }: ExportDashboar
<ExportSubtitle>Edit export settings and click 'Download'.</ExportSubtitle>
</ExportSettingsInstructionsContainer>
<ExportSetting>
{hasChartItems && (
<section>
<ExportSettingsWrapper>
<DisplayFormatSettings />
</ExportSettingsWrapper>
<section>
<ExportSettingsWrapper>
<DisplayFormatSettings />
</ExportSettingsWrapper>
{hasChartItems && (
<ExportSettingsWrapper>
<DisplayOptionsSettings />
</ExportSettingsWrapper>
</section>
)}
)}
</section>
<MailingListSection
selectedDashboardItems={selectedDashboardItems}
settings={{
Expand Down
14 changes: 10 additions & 4 deletions packages/tupaia-web/src/layout/UserMenu/UserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,16 @@ export const UserInfo = ({ user, isLandingPage, secondaryColor, isLoggedIn }: Us
const userProjectName = user?.project?.name || 'Explore';
return (
<UsernameContainer $isLandingPage={isLandingPage}>
{userName} |
<Tooltip>
<ProjectButton modal={MODAL_ROUTES.PROJECT_SELECT}>{userProjectName}</ProjectButton>
</Tooltip>
{userName}
{!isLandingPage ? (
<>
{' '}
|
<Tooltip>
<ProjectButton modal={MODAL_ROUTES.PROJECT_SELECT}>{userProjectName}</ProjectButton>
</Tooltip>
</>
) : null}
</UsernameContainer>
);
}
Expand Down
Loading

0 comments on commit 4cc4bbd

Please sign in to comment.