Skip to content
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

[IPCT1-629] - add filter login inactivity #189

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/api/src/controllers/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CommunityController {
inactivity,
unidentified,
blocked,
loginInactivity,
} = req.query;
if (search === undefined || typeof search !== 'string') {
throw new Error('invalid search!');
Expand All @@ -85,6 +86,7 @@ class CommunityController {
inactivity: inactivity === 'true',
unidentified: unidentified === 'true',
blocked: blocked === 'true',
loginInactivity: loginInactivity === 'true',
})
.then((r) => standardResponse(res, 200, true, r))
.catch((e) =>
Expand All @@ -99,6 +101,7 @@ class CommunityController {
inactivity,
unidentified,
blocked,
loginInactivity,
} = req.query;
if (active === undefined || typeof active !== 'string') {
active = 'true';
Expand All @@ -119,6 +122,7 @@ class CommunityController {
inactivity: inactivity === 'true',
unidentified: unidentified === 'true',
blocked: blocked === 'true',
loginInactivity: loginInactivity === 'true',
}
)
.then((r) => standardResponse(res, 200, true, r))
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ export default {
claimLocationTimeframe: validatedEnv.CLAIM_LOCATION_TIMEFRAME,
claimInactivityThreshold: validatedEnv.CLAIM_INACTIVITY_THRESHOLD,

/**
* Days without login to consider a user inactive
*/
loginInactivityThreshold: validatedEnv.LOGIN_INACTIVITY_THRESHOLD,

defaultLimit: validatedEnv.DEFAULT_LIMIT,
defaultOffset: validatedEnv.DEFAULT_OFFSET,

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config/validatenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function validateEnv() {
COMMUNITY_ADMIN_ADDRESS: str({ devDefault: onlyOnTestEnv('xyz') }),
CLAIM_LOCATION_TIMEFRAME: num({ default: 150 }),
CLAIM_INACTIVITY_THRESHOLD: num({ default: 4 }),
LOGIN_INACTIVITY_THRESHOLD: num({ default: 10 }),
DEFAULT_LIMIT: num({ default: 10 }),
DEFAULT_OFFSET: num({ default: 0 }),
ENABLED_CACHE_WITH_REDIS: bool({ default: true }),
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/services/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export type BeneficiaryFilterType = {
active?: boolean;
suspect?: boolean;
inactivity?: boolean;
loginInactivity?: boolean;
unidentified?: boolean;
blocked?: boolean;
};
12 changes: 12 additions & 0 deletions packages/core/src/services/ubi/beneficiary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,18 @@ export default class BeneficiaryService {
};
}

if (filter.loginInactivity) {
const date = new Date();
date.setDate(date.getDate() - config.loginInactivityThreshold);

where = {
...where,
'$"user"."lastLogin"$': {
[Op.lt]: date,
},
};
}

return where;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/services/ubi/managers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ export default class ManagerService {
from manager m, manager mq left join "app_user" u on u.address = mq."address"
where mq.active = true and m."communityId" = mq."communityId" and m."address" = :managerAddress and mq."address" = :address
order by mq."createdAt" desc
`
`;

return await this.sequelize.query(query, {
type: QueryTypes.SELECT,
replacements: {
managerAddress,
address,
}
},
});
}

Expand Down Expand Up @@ -126,15 +126,15 @@ export default class ManagerService {
order by mq."createdAt" desc
offset :offset
limit :limit
`
`;

return await this.sequelize.query(query, {
type: QueryTypes.SELECT,
replacements: {
managerAddress,
offset,
limit,
}
},
});
}

Expand Down
28 changes: 28 additions & 0 deletions packages/core/tests/integration/beneficiary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Sequelize } from 'sequelize';
import { assert, spy, replace, restore, SinonSpy } from 'sinon';
import tk from 'timekeeper';

import config from '../../src/config';
import { models, sequelize as database } from '../../src/database';
import { ManagerAttributes } from '../../src/database/models/ubi/manager';
import { AppUser } from '../../src/interfaces/app/appUser';
Expand Down Expand Up @@ -376,6 +377,33 @@ describe('beneficiary service', () => {
expect(result.length).to.be.equal(1);
expect(result[0].address).to.be.equal(listUsers[4].address);
});

it('should list suspect and login inactive beneficiaries', async () => {
const date = new Date();
date.setDate(date.getDate() - config.loginInactivityThreshold);

await sequelize.models.AppUserModel.update(
{
suspect: true,
lastLogin: date,
},
{ where: { address: listUsers[0].address } }
);
const result = await BeneficiaryService.list(
listManagers[0].address,
0,
5,
{
suspect: true,
loginInactivity: true,
}
);

expect(result.length).to.be.equal(1);
expect(result[0].address).to.be.equal(listUsers[0].address);
// eslint-disable-next-line no-unused-expressions
expect(result[0].suspect).to.be.true;
});
});

describe('search', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/integration/community.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cronJobs } from '@impactmarket/worker';
import { expect } from 'chai';
import faker from 'faker';
import { Sequelize } from 'sequelize';
Expand All @@ -11,7 +12,6 @@ import { CommunityContentStorage } from '../../src/services/storage';
import BeneficiaryService from '../../src/services/ubi/beneficiary';
import CommunityService from '../../src/services/ubi/community';
import ManagerService from '../../src/services/ubi/managers';
import { cronJobs } from '@impactmarket/worker';
import { sequelizeSetup, truncate } from '../config/sequelizeSetup';
import { randomTx } from '../config/utils';
import BeneficiaryFactory from '../factories/beneficiary';
Expand Down