Skip to content

Commit

Permalink
Fix Join workspace using invitation link (#382)
Browse files Browse the repository at this point in the history
* update the workspace method for confirmationlink

* update the confirmmembership function of workspace

* comments added

* lints removed

* Bump version up to 1.0.7

* Bump version up to 1.0.8

* Bump version up to 1.0.9

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
robonetphy and github-actions[bot] authored Feb 15, 2022
1 parent fddfcdc commit e411612
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.0.8",
"version": "1.0.9",
"main": "index.ts",
"license": "UNLICENSED",
"scripts": {
Expand Down
46 changes: 19 additions & 27 deletions src/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,41 +230,33 @@ export default class WorkspaceModel extends AbstractModel<WorkspaceDBScheme> imp
*
* @param member - member for whom confirm membership
*/
public async confirmMembership(member: UserModel): Promise<boolean> {
const { matchedCount, modifiedCount } = await this.collection.updateOne(
{
userId: new ObjectId(member._id.toString()),
},
{ $unset: { isPending: '' } }
);

const isUserAlreadyConfirmedInvitation = matchedCount > 0 && modifiedCount === 0;
public async confirmMembership(member: UserModel): Promise<void> {
/**
* Check if user is already a member of workspace or not.
*/
const isUserAlreadyConfirmedInvitation = await this.teamCollection.findOne({
userId: new ObjectId(member._id.toString()),
});

if (isUserAlreadyConfirmedInvitation) {
throw new Error('User is already confirmed the invitation');
}

/**
* In case user was invited via email instead of invite link
* If user is not member of workspace
* then edit team collection.
*/
if (matchedCount === 0) {
await this.collection.updateOne(
{
userEmail: member.email,
await this.teamCollection.updateOne(
{
userEmail: member.email,
},
{
$set: { userId: new ObjectId(member._id.toString()) },
$unset: {
userEmail: '',
},
{
$set: { userId: new ObjectId(member._id.toString()) },
$unset: {
userEmail: '',
isPending: '',
},
}
);

return false;
}

return true;
}
);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/resolvers/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,8 @@ module.exports = {
throw new ApolloError('The link is broken');
}

const membershipExists = await workspace.confirmMembership(currentUser);

if (membershipExists) {
await currentUser.confirmMembership(workspaceId);
} else {
await currentUser.addWorkspace(workspaceId);
}
await workspace.confirmMembership(currentUser);
await currentUser.addWorkspace(workspaceId);

return {
recordId: workspace._id.toString(),
Expand Down

0 comments on commit e411612

Please sign in to comment.