Skip to content

Commit 9309387

Browse files
committed
feat: delete inactivated users
* updateMetadata no lodash * doc addons * cleanUsers supperess error parameter
1 parent 879ebb5 commit 9309387

4 files changed

Lines changed: 64 additions & 10 deletions

File tree

rfcs/inactive_users_cleanup.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ Record contains `userId` and `current timestamp`.
3131
## Activation process
3232
When the user succeeds activation `userId`,the entry deleted from `inactive-users`.
3333

34-
## `users:cleanup` hook
34+
## `users:cleanup` hook `cleanUsers(suppress?)`
35+
`suppress` parameter defines function error behavior. If parameter set, the function throws errors,
36+
otherwise, function calls `log.error` with `error.message` as message.
37+
38+
Other option, is to define new config parameter as object and move `config.deleteInactiveAccounts` into it:
39+
```javascript
40+
const conf = {
41+
deleteInactiveUsers: {
42+
ttl: seconds, // replaces deleteInactiveAccounts
43+
suppressErrors: true || false,
44+
},
45+
}
46+
```
3547
Calls `deleteInactivatedUsers` script with TTL parameter from `service.config.deleteInactiveAccounts`.
3648

3749
## Redis Delete Inactive User Script

src/utils/inactiveUsers/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ function removeFromInactiveUsers(redis, userId) {
2222
redis.zrem(USERS_INACTIVATED, userId);
2323
}
2424

25-
async function cleanUsers() {
25+
/**
26+
* Deletes users that didn't pass activation
27+
* @param suppressError boolean throw or suppress error
28+
* @returns {Promise<number>}
29+
*/
30+
async function cleanUsers(suppressError = true) {
2631
const { redis } = this;
2732
const { deleteInactiveAccounts } = this.config;
2833
let deletedUsers = 0;
2934

3035
try {
3136
deletedUsers = await deleteInactiveUsers(redis, deleteInactiveAccounts);
3237
} catch (e) {
33-
this.log.error(e);
38+
if (suppressError) {
39+
this.log.error({ error: e }, e.message);
40+
} else {
41+
throw e;
42+
}
3443
}
3544

3645
return deletedUsers;

src/utils/updateMetadata.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable no-mixed-operators */
22
const Promise = require('bluebird');
33
const is = require('is');
4-
const ld = require('lodash');
54
const { HttpStatusError } = require('common-errors');
65
const mapValues = require('lodash/mapValues');
76
const redisKey = require('../utils/key.js');
@@ -22,13 +21,18 @@ function callUpdateMetadataScript(redis, userId, ops) {
2221
// Stabilizes Lua script response
2322
function mapUpdateResponse(jsonStr) {
2423
const decodedData = JSONParse(jsonStr);
25-
const result = ld.map(decodedData, (audienceProcessResult) => {
26-
return ld.mapValues(audienceProcessResult, (ops) => {
27-
if (ops.length === undefined) {
28-
return ops;
24+
const result = [];
25+
26+
decodedData.forEach((metaResult) => {
27+
const opResult = {};
28+
for (const [key, ops] of Object.entries(metaResult)) {
29+
if (ops.length !== undefined && ops.length === 1) {
30+
[opResult[key]] = ops;
31+
} else {
32+
opResult[key] = ops;
2933
}
30-
return ops.length > 1 ? ops : ops[0];
31-
});
34+
}
35+
result.push(opResult);
3236
});
3337

3438
return result.length > 1 ? result : result[0];

test/suites/deleteInactivatedUsers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ describe('#inactive user', function registerSuite() {
4444
return simpleDispatcher(this.users.router)('users.register', { ...regUserNoAlias });
4545
});
4646

47+
describe('throw suppress error', function test() {
48+
const lua = 'return { foo bar }';
49+
50+
beforeEach(function pretest() {
51+
const { redis } = this.users;
52+
redis.defineCommand('deleteInactivatedUsers', { lua });
53+
});
54+
55+
it('throws error', async function subtest() {
56+
let err;
57+
try {
58+
await cleanUsers.call(this.users, false);
59+
} catch (e) {
60+
err = e;
61+
}
62+
expect(err).to.be.an('error');
63+
});
64+
65+
it('suppresses error', async function subtest() {
66+
let err;
67+
try {
68+
await cleanUsers.call(this.users);
69+
} catch (e) {
70+
err = e;
71+
}
72+
expect(err).to.be.an('undefined');
73+
});
74+
});
75+
4776
it('deletes inactive user', function test() {
4877
return delay(() => {
4978
return cleanUsers.call(this.users)

0 commit comments

Comments
 (0)