Skip to content

Commit f97cf8b

Browse files
BrRenatjenyapoyarkov
authored andcommitted
fix: edited send organization invite mail (#404)
* feat: edited send organization invite mail * fix: added handling new action * fix: edited checking organization invite * fix: return config * fix: code refactor * fix: fix lint error
1 parent cb472e4 commit f97cf8b

8 files changed

Lines changed: 72 additions & 22 deletions

File tree

src/actions/organization/invites/accept.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { checkOrganizationExists } = require('../../../utils/organization');
33
const {
44
ErrorUserNotMember,
55
ORGANIZATIONS_MEMBERS,
6-
USERS_ACTION_INVITE,
6+
USERS_ACTION_ORGANIZATION_INVITE,
77
ErrorInvitationExpiredOrUsed,
88
} = require('../../../constants');
99
const redisKey = require('../../../utils/key.js');
@@ -16,7 +16,7 @@ async function verifyToken(tokenManager, params) {
1616
// we must ensure that token matches supplied ID
1717
// it can be overwritten by sending `anyUsername: true`
1818
const control = {
19-
action: USERS_ACTION_INVITE,
19+
action: USERS_ACTION_ORGANIZATION_INVITE,
2020
id: params.username,
2121
};
2222

src/actions/organization/invites/send.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const { ActionTransport } = require('@microfleet/core');
2-
const generateInvite = require('../../invite');
2+
const sendInviteMail = require('../../../utils/organization/sendInviteMail');
3+
const getInternalData = require('../../../utils/organization/getInternalData');
34
const redisKey = require('../../../utils/key');
45
const { checkOrganizationExists } = require('../../../utils/organization');
5-
const { ORGANIZATIONS_MEMBERS, ErrorUserNotMember } = require('../../../constants');
6+
const { ORGANIZATIONS_MEMBERS, ErrorUserNotMember, ORGANIZATIONS_NAME_FIELD } = require('../../../constants');
67

78
/**
89
* @api {amqp} <prefix>.invites.send Send invitation
@@ -29,11 +30,16 @@ async function sendOrganizationInvite({ params }) {
2930
if (!userInOrganization) {
3031
throw ErrorUserNotMember;
3132
}
33+
const organization = await getInternalData.call(this, organizationId, false);
3234

33-
return generateInvite.call(this, { params: {
35+
return sendInviteMail.call(this, {
3436
email: member.email,
35-
ctx: { firstName: member.firstName, lastName: member.lastName },
36-
} });
37+
ctx: {
38+
firstName: member.firstName,
39+
lastName: member.lastName,
40+
organization: organization[ORGANIZATIONS_NAME_FIELD],
41+
},
42+
});
3743
}
3844

3945
sendOrganizationInvite.allowed = checkOrganizationExists;

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module.exports = exports = {
8484
USERS_ACTION_RESET: 'reset',
8585
USERS_ACTION_REGISTER: 'register',
8686
USERS_ACTION_INVITE: 'invite',
87+
USERS_ACTION_ORGANIZATION_INVITE: 'organization-invite',
8788

8889
// invitations constants
8990
INVITATIONS_INDEX: 'user-invitations',

src/utils/challenges/email/generate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
USERS_ACTION_PASSWORD,
1414
USERS_ACTION_REGISTER,
1515
USERS_ACTION_INVITE,
16+
USERS_ACTION_ORGANIZATION_INVITE,
1617
} = require('../../../constants.js');
1718

1819
// will be replaced later
@@ -33,6 +34,7 @@ function generate(email, type, ctx = {}, opts = {}, nodemailer = {}) {
3334
case USERS_ACTION_ACTIVATE:
3435
case USERS_ACTION_RESET:
3536
case USERS_ACTION_INVITE:
37+
case USERS_ACTION_ORGANIZATION_INVITE:
3638
// generate secret
3739
context.qs = `?q=${context.token.secret}`;
3840
context.link = generateLink(server, paths[type]);

src/utils/organization/addOrganizationMembers.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable no-mixed-operators */
22
const Promise = require('bluebird');
33
const redisKey = require('../key.js');
4-
const generateInvite = require('../../actions/invite');
4+
const sendInviteMail = require('./sendInviteMail');
5+
const getInternalData = require('./getInternalData');
56
const handlePipeline = require('../pipelineError.js');
6-
const { ORGANIZATIONS_MEMBERS, USERS_ORGANIZATIONS } = require('../../constants.js');
7+
const { ORGANIZATIONS_MEMBERS, USERS_ORGANIZATIONS, ORGANIZATIONS_NAME_FIELD } = require('../../constants.js');
78

89
/**
910
* Updates metadata on a organization object
@@ -29,14 +30,19 @@ async function addOrganizationMembers(opts) {
2930
});
3031

3132
await pipe.exec().then(handlePipeline);
33+
const organization = await getInternalData.call(this, organizationId, false);
3234

3335
const membersIdsJob = [];
3436
for (const member of members) {
3537
membersIdsJob.push(
36-
generateInvite.call(this, { params: {
38+
sendInviteMail.call(this, {
3739
email: member.email,
38-
ctx: { firstName: member.firstName, lastName: member.lastName },
39-
} })
40+
ctx: {
41+
firstName: member.firstName,
42+
lastName: member.lastName,
43+
organization: organization[ORGANIZATIONS_NAME_FIELD],
44+
},
45+
})
4046
);
4147
}
4248

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Promise = require('bluebird');
2+
const generateEmail = require('../challenges/email/generate.js');
3+
const {
4+
INVITATIONS_INDEX,
5+
TOKEN_METADATA_FIELD_CONTEXT,
6+
TOKEN_METADATA_FIELD_SENDED_AT,
7+
USERS_ACTION_ORGANIZATION_INVITE,
8+
} = require('../../constants.js');
9+
10+
module.exports = function sendInviteMail(params) {
11+
const { redis, tokenManager } = this;
12+
const { email, ctx = {} } = params;
13+
const now = Date.now();
14+
15+
return tokenManager
16+
.create({
17+
id: email,
18+
action: USERS_ACTION_ORGANIZATION_INVITE,
19+
regenerate: true,
20+
metadata: {
21+
[TOKEN_METADATA_FIELD_CONTEXT]: ctx,
22+
[TOKEN_METADATA_FIELD_SENDED_AT]: now,
23+
},
24+
})
25+
.then(token => Promise
26+
.bind(this, [email, USERS_ACTION_ORGANIZATION_INVITE, { ...ctx, token }, { send: true }])
27+
.spread(generateEmail)
28+
.tap(() => redis.sadd(INVITATIONS_INDEX, email)));
29+
};

test/configs/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
password: 'cpst-password',
1010
register: 'cpst-register',
1111
invite: 'rfx-invite',
12+
'organization-invite': 'sl-accept-invite',
1213
},
1314
},
1415
registrationLimits: {

yarn.lock

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,11 +1200,16 @@ bluebird-retry@^0.11.0:
12001200
resolved "https://registry.yarnpkg.com/bluebird-retry/-/bluebird-retry-0.11.0.tgz#1289ab22cbbc3a02587baad35595351dd0c1c047"
12011201
integrity sha1-EomrIsu8OgJYe6rTVZU1HdDBwEc=
12021202

1203-
bluebird@^3.5.0, bluebird@^3.5.1, bluebird@^3.5.2, bluebird@^3.5.3, bluebird@^3.5.4:
1203+
bluebird@^3.5.0, bluebird@^3.5.2, bluebird@^3.5.3, bluebird@^3.5.4:
12041204
version "3.5.4"
12051205
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.4.tgz#d6cc661595de30d5b3af5fcedd3c0b3ef6ec5714"
12061206
integrity sha512-FG+nFEZChJrbQ9tIccIfZJBz3J7mLrAhxakAbnrJWn8d7aKOC+LWifa0G+p4ZqKp4y13T7juYvdhq9NzKdsrjw==
12071207

1208+
bluebird@^3.5.1:
1209+
version "3.5.5"
1210+
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f"
1211+
integrity sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==
1212+
12081213
boolbase@~1.0.0:
12091214
version "1.0.0"
12101215
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
@@ -5248,9 +5253,9 @@ ms-mailer-client@^8.0.1:
52485253
lodash.merge "^4.6.1"
52495254

52505255
ms-mailer-templates@^1.11.1:
5251-
version "1.11.1"
5252-
resolved "https://registry.yarnpkg.com/ms-mailer-templates/-/ms-mailer-templates-1.11.1.tgz#e92ff0c88277aaa2558070eadae7163e7ff8a98f"
5253-
integrity sha512-IS1yWrQp0X4M7bN21P1aMA/G6qKI8l6g5+mnhufgSuLHPi0Paehff8GVxnh573oaHA2D37SKH9Y4j0glc+9yGw==
5256+
version "1.13.1"
5257+
resolved "https://registry.yarnpkg.com/ms-mailer-templates/-/ms-mailer-templates-1.13.1.tgz#29de47138f68633a3fc596fe99f86ffbe578bb89"
5258+
integrity sha512-LIVP1aolT7fefp/6lefDr2Xak2Uah+OLD53LBq75kU9Y7kBNDAIWmAs5Evsp5U84xJAGCC+yz29FkHAUGR+Ybg==
52545259
dependencies:
52555260
bluebird "^3.5.1"
52565261
common-errors "^1.0.5"
@@ -5360,9 +5365,9 @@ needle@^2.2.1:
53605365
sax "^1.2.4"
53615366

53625367
neo-async@^2.6.0:
5363-
version "2.6.0"
5364-
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835"
5365-
integrity sha512-MFh0d/Wa7vkKO3Y3LlacqAEeHK0mckVqzDieUKTT+KGxi+zIpeVsFxymkIiRpbpDziHc290Xr9A1O4Om7otoRA==
5368+
version "2.6.1"
5369+
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c"
5370+
integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==
53665371

53675372
nerf-dart@^1.0.0:
53685373
version "1.0.0"
@@ -8025,9 +8030,9 @@ uc.micro@^1.0.1, uc.micro@^1.0.5:
80258030
integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==
80268031

80278032
uglify-js@^3.1.4:
8028-
version "3.5.6"
8029-
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.5.6.tgz#8a5f8a06ee7415ac1fa302f4623bc7344b553da4"
8030-
integrity sha512-YDKRX8F0Y+Jr7LhoVk0n4G7ltR3Y7qFAj+DtVBthlOgCcIj1hyMigCfousVfn9HKmvJ+qiFlLDwaHx44/e5ZKw==
8033+
version "3.6.0"
8034+
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"
8035+
integrity sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==
80318036
dependencies:
80328037
commander "~2.20.0"
80338038
source-map "~0.6.1"

0 commit comments

Comments
 (0)