Skip to content

Commit 1288b1d

Browse files
committed
feat(bin): assign affiliate
1 parent 177bbdf commit 1288b1d

11 files changed

Lines changed: 396 additions & 148 deletions

File tree

bin/assign-affiliate.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node
2+
3+
const assert = require('assert');
4+
const Redis = require('ioredis').Cluster;
5+
const { argv } = require('yargs')
6+
.option('id', {
7+
type: 'string',
8+
description: 'user id that is being altered',
9+
required: true,
10+
})
11+
.option('code', {
12+
type: 'string',
13+
description: 'referral code to assign to the user',
14+
required: true,
15+
})
16+
.option('overwrite', {
17+
type: 'boolean',
18+
description: 'when referral was already set - must pass to overwrite',
19+
default: false,
20+
});
21+
22+
(async () => {
23+
const conf = require('../lib/config');
24+
const {
25+
USERS_REFERRAL_INDEX,
26+
USERS_REFERRAL_FIELD,
27+
USERS_METADATA,
28+
USERS_ALIAS_TO_ID,
29+
} = require('../lib/constants');
30+
31+
const handleRedisPipelineError = require('../lib/utils/pipelineError');
32+
const redisKey = require('../lib/utils/key');
33+
const redisConfig = conf.get('/redis', { env: process.env.NODE_ENV });
34+
const audience = conf.get('/jwt/defaultAudience', { env: process.env.NODE_ENV });
35+
const opts = Object.assign({}, redisConfig.options, { lazyConnect: true });
36+
const redis = new Redis(redisConfig.hosts, opts);
37+
const metaKey = redisKey(argv.id, USERS_METADATA, audience);
38+
39+
try {
40+
await redis.connect();
41+
42+
// ensure that this user exists
43+
assert.equal(await redis.exists(metaKey), 1, 'user does not exist');
44+
45+
// ensure that referral was not already set
46+
if (argv.overwrite !== true) {
47+
assert.equal(
48+
await redis.hexists(metaKey, USERS_REFERRAL_FIELD),
49+
0,
50+
'referral was already set, pass --overwrite to args to force it'
51+
);
52+
}
53+
54+
// verify that referral code is valid, which is basically
55+
// checking whether that alias exists or not
56+
assert.equal(
57+
await redis.hexists(USERS_ALIAS_TO_ID, argv.code),
58+
1,
59+
'referral code is invalid - it does not exist'
60+
);
61+
62+
const commands = [
63+
['hset', audience, `"${argv.code}"`],
64+
['sadd', `${USERS_REFERRAL_INDEX}:${argv.code}`, argv.id],
65+
];
66+
67+
await redis.pipeline(commands).exec().then(handleRedisPipelineError);
68+
} catch (e) {
69+
console.error('Failed', e); // eslint-disable-line no-console
70+
} finally {
71+
redis.disconnect();
72+
}
73+
})();

bin/batch-register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const { CHALLENGE_TYPE_EMAIL } = require('../lib/constants');
1717
const config = conf.get('/', { env: process.env.NODE_ENV });
1818
const amqpConfig = omit(config.amqp.transport, ['queue', 'neck', 'listen', 'onComplete']);
1919
const audience = config.jwt.defaultAudience;
20-
const prefix = config.router.routes.prefix;
20+
const { prefix } = config.router.routes;
2121

2222
/**
2323
* Registers batch users from stdin

bin/bearer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const assert = require('assert');
99

1010
const config = conf.get('/', { env: process.env.NODE_ENV });
1111
const amqpConfig = config.amqp.transport;
12-
const prefix = config.router.routes.prefix;
12+
const { prefix } = config.router.routes;
1313

1414
const username = process.argv[2];
1515
const name = process.argv[3];

bin/dump.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
// quickly dump specific fields for users
4-
const argv = require('yargs')
4+
const { argv } = require('yargs')
55
.option('field', {
66
alias: 'f',
77
describe: 'fields to dump',
@@ -57,8 +57,7 @@ const argv = require('yargs')
5757
filter: JSON.parse,
5858
})
5959
.demandOption(['field'], 'Please provide at least 1 field to dump')
60-
.help('h')
61-
.argv;
60+
.help('h');
6261

6362
// deps
6463
const fs = require('fs');
@@ -123,7 +122,7 @@ switch (argv.output) {
123122
*/
124123
const writeUserToOutput = (user) => {
125124
const attributes = user.metadata[audience];
126-
const id = user.id;
125+
const { id } = user;
127126
const username = (argv.username && attributes[argv.username]) || attributes[USERS_USERNAME_FIELD];
128127

129128
if (argv.toDate) {

bin/password.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const conf = require('../lib/config');
77

88
const config = conf.get('/', { env: process.env.NODE_ENV });
99
const redisConfig = config.redis;
10-
const updatePassword = require('../lib/actions/updatePassword').updatePassword;
10+
const { updatePassword } = require('../lib/actions/updatePassword');
1111

1212
const username = process.argv[2];
1313
const password = process.argv[3];

package.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@
2828
},
2929
"homepage": "https://github.com/makeomatic/ms-users#readme",
3030
"dependencies": {
31-
"@microfleet/core": "^10.6.1",
32-
"@microfleet/transport-amqp": "^13.1.0",
31+
"@microfleet/core": "^10.7.1",
32+
"@microfleet/transport-amqp": "^13.1.1",
3333
"babel-runtime": "^6.25.0",
3434
"bell": "^9.3.1",
3535
"bluebird": "^3.5.1",
3636
"bunyan": "^1.8.12",
3737
"bytes": "^3.0.0",
3838
"common-errors": "^1.0.5",
3939
"csv-write-stream": "^2.0.0",
40-
"disposable-email-domains": "^1.0.33",
40+
"disposable-email-domains": "^1.0.34",
4141
"dlock": "^8.0.0",
4242
"flake-idgen": "^1.1.0",
4343
"get-stdin": "^6.0.0",
4444
"handlebars": "^4.0.11",
45-
"hapi": "^17.5.1",
45+
"hapi": "^17.5.2",
4646
"ioredis": "^3.2.2",
4747
"is": "^3.2.1",
48-
"jsonwebtoken": "^8.2.2",
48+
"jsonwebtoken": "^8.3.0",
4949
"jwa": "^1.1.6",
5050
"lodash": "^4.17.10",
5151
"moment": "^2.22.2",
5252
"ms-conf": "^3.1.2",
5353
"ms-flakeless": "^4.1.0",
5454
"ms-mailer-client": "^7.0.0",
55-
"ms-mailer-templates": "^1.8.0",
55+
"ms-mailer-templates": "^1.10.0",
5656
"ms-token": "^2.0.0",
5757
"ms-validation": "^7.1.0",
5858
"otplib": "^10.0.0",
@@ -64,43 +64,43 @@
6464
"serialize-error": "^2.1.0",
6565
"serialize-javascript": "^1.5.0",
6666
"stdout-stream": "^1.4.0",
67-
"tough-cookie": "^2.4.2",
68-
"uuid": "^3.2.1",
69-
"vision": "^5.3.2",
70-
"yargs": "^11.0.0"
67+
"tough-cookie": "^2.4.3",
68+
"uuid": "^3.3.2",
69+
"vision": "^5.3.3",
70+
"yargs": "^12.0.1"
7171
},
7272
"devDependencies": {
7373
"@makeomatic/deploy": "^6.1.2",
74-
"@semantic-release/changelog": "^2.0.2",
74+
"@semantic-release/changelog": "^2.1.1",
7575
"@semantic-release/exec": "^2.2.4",
76-
"@semantic-release/git": "^5.0.0",
76+
"@semantic-release/git": "^6.0.1",
7777
"apidoc": "^0.17.6",
7878
"apidoc-plugin-schema": "^0.1.8",
7979
"babel-cli": "^6.24.1",
80-
"babel-eslint": "^8.2.3",
80+
"babel-eslint": "^8.2.5",
8181
"babel-plugin-istanbul": "^4.1.6",
8282
"babel-plugin-transform-class-properties": "^6.24.1",
8383
"babel-plugin-transform-object-rest-spread": "^6.23.0",
8484
"babel-plugin-transform-strict-mode": "^6.24.1",
8585
"babel-register": "^6.24.1",
8686
"chai": "^4.1.1",
8787
"cheerio": "^1.0.0-rc.2",
88-
"codecov": "^3.0.2",
89-
"cross-env": "^5.1.6",
90-
"eslint": "^4.19.1",
88+
"codecov": "^3.0.4",
89+
"cross-env": "^5.2.0",
90+
"eslint": "^5.1.0",
9191
"eslint-config-makeomatic": "^2.0.1",
92-
"eslint-plugin-import": "^2.12.0",
93-
"eslint-plugin-mocha": "^5.0.0",
92+
"eslint-plugin-import": "^2.13.0",
93+
"eslint-plugin-mocha": "^5.1.0",
9494
"eslint-plugin-promise": "^3.8.0",
9595
"faker": "^4.1.0",
9696
"glob": "^7.1.2",
9797
"json": "^9.0.6",
9898
"md5": "^2.2.1",
9999
"mocha": "^5.2.0",
100100
"nyc": "^12.0.2",
101-
"puppeteer": "^1.4.0",
101+
"puppeteer": "^1.5.0",
102102
"rimraf": "^2.6.1",
103-
"sinon": "^5.1.0"
103+
"sinon": "^6.1.3"
104104
},
105105
"engines": {
106106
"node": ">= 8.9.0",

schemas/alias.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
"properties": {
99
"username": {
1010
"type": "string",
11-
"minLength": 1
11+
"minLength": 1,
12+
"maxLength": 64
1213
},
1314
"alias": {
14-
"type": "string",
15-
"minLength": 3,
16-
"maxLength": 15,
17-
"pattern": "^[a-zA-Z.0-9]+$"
15+
"$ref": "common.json#/definitions/alias"
1816
}
1917
}
2018
}

schemas/common.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
{ "pattern": "^\\d+$" },
7272
{ "pattern": "^(fb|sso)/[\\d_]+$" }
7373
]
74+
},
75+
"alias": {
76+
"type": "string",
77+
"pattern": "^[a-zA-Z0-9\\-\\.\\_]{3,20}$"
7478
}
7579
}
7680
}

schemas/register.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
"$ref": "common.json#/definitions/userId"
1111
},
1212
"alias": {
13-
"type": "string",
14-
"minLength": 3,
15-
"maxLength": 20,
16-
"pattern": "^[a-zA-Z0-9\\-\\.\\_]+$"
13+
"$ref": "common.json#/definitions/alias"
1714
},
1815
"password": {
1916
"type": "string"

test/suites/bin.dump.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const assert = require('assert');
22
const path = require('path');
33
const fs = require('fs');
4-
const exec = require('child_process').exec;
4+
const { exec } = require('child_process');
55

66
describe('binary: dump', function suite() {
77
const binaryPath = path.resolve(__dirname, '../../bin/dump.js');
@@ -60,7 +60,7 @@ describe('binary: dump', function suite() {
6060

6161
it('is able to use filter, users generated are random, so cant know for sure whats returned', function test(next) {
6262
exec(`${binaryPath} -f firstName lastName -o csv --filter '${JSON.stringify({
63-
'username': '@yahoo.com',
63+
username: '@yahoo.com',
6464
})}'`, { env }, (err, stdout, stderr) => {
6565
if (err) return next(err);
6666
if (stderr) return next(new Error(stderr));

0 commit comments

Comments
 (0)