From fadd6e2d687679ceb74244a3753b4db4875a9e7a Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 26 Oct 2021 06:00:24 +0530 Subject: [PATCH 01/37] resolver added --- src/resolvers/workspace.js | 62 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 1258a734..4198448c 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -88,7 +88,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -100,7 +100,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -318,6 +318,64 @@ module.exports = { return true; }, + /** + * Mutation in order to leave workspace + * @param {ResolverObj} _obj - object that contains the result returned from the resolver on the parent field + * @param {string} workspaceId - id of the workspace where the user should be removed + * @param {UserInContext} user - current authorized user {@see ../index.js} + * @param {ContextFactories} factories - factories for working with models + * @return {Promise} - true if operation is successful + */ + async deleteWorkspace(_obj, { workspaceId }, { user, factories }) { + console.log("Yay! It's Working"); + const userModel = await factories.usersFactory.findById(user.id); + const workspaceModel = await factories.workspacesFactory.findById(workspaceId); + + if (!workspaceModel) { + throw new UserInputError('There is no workspace with provided id'); + } + + const memberInfo = await workspaceModel.getMemberInfo(user.id); + + if (memberInfo.isAdmin) { + const membersInfo = (await workspaceModel.getMembers()); + const isThereOtherAdmins = !!membersInfo.find( + member => member.isAdmin && member.userId.toString() !== user.id + ); + + if (!isThereOtherAdmins) { + // const projectToWorkspace = new ProjectToWorkspace(workspaceId); + + // return projectToWorkspace.getProjects(ids); + // const project = await factories.projectsFactory.findById(projectId); + + // if (!project) { + // throw new ApolloError('There is no project with that id'); + // } + + // const workspaceModel = await factories.workspacesFactory.findById(project.workspaceId.toString()); + + // /** + // * Remove project events + // */ + // await new EventsFactory(project._id).remove(); + + // /** + // * Remove project from workspace + // */ + // await new ProjectToWorkspace(workspaceModel._id.toString()).remove(project._id); + + // /** + // * Remove project + // */ + // await project.remove(); + + // return true; + } + } + await workspaceModel.removeMember(userModel); + return true; + }, /** * Change workspace plan for free plan mutation implementation From 685d0672cea9fecd5f48e180d8665ee9cf39ca23 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 26 Oct 2021 06:00:43 +0530 Subject: [PATCH 02/37] mutation added in defination --- src/typeDefs/workspace.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/typeDefs/workspace.ts b/src/typeDefs/workspace.ts index d6e91803..9c543dd4 100644 --- a/src/typeDefs/workspace.ts +++ b/src/typeDefs/workspace.ts @@ -275,5 +275,16 @@ export default gql` """ workspaceId: ID! ): Boolean! @requireAuth + + """ + Mutation in order to delete workspace + Returns true if operation is successful + """ + deleteWorkspace( + """ + Workspace ID + """ + workspaceId: ID! + ): Boolean! @requireAuth } `; From 4930cb915b493a3dc6ccbb08dedc21f81aba91e0 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 26 Oct 2021 08:36:12 +0530 Subject: [PATCH 03/37] api added --- src/resolvers/workspace.js | 56 ++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 4198448c..4a01afb1 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -13,6 +13,7 @@ import { dateFromObjectId } from '../utils/dates'; const { ApolloError, UserInputError, ForbiddenError } = require('apollo-server-express'); const crypto = require('crypto'); +const EventsFactory = require('../models/eventsFactory'); /** * See all types and fields here {@see ../typeDefs/workspace.graphql} @@ -328,7 +329,6 @@ module.exports = { */ async deleteWorkspace(_obj, { workspaceId }, { user, factories }) { console.log("Yay! It's Working"); - const userModel = await factories.usersFactory.findById(user.id); const workspaceModel = await factories.workspacesFactory.findById(workspaceId); if (!workspaceModel) { @@ -339,41 +339,31 @@ module.exports = { if (memberInfo.isAdmin) { const membersInfo = (await workspaceModel.getMembers()); - const isThereOtherAdmins = !!membersInfo.find( - member => member.isAdmin && member.userId.toString() !== user.id - ); - - if (!isThereOtherAdmins) { - // const projectToWorkspace = new ProjectToWorkspace(workspaceId); - - // return projectToWorkspace.getProjects(ids); - // const project = await factories.projectsFactory.findById(projectId); - - // if (!project) { - // throw new ApolloError('There is no project with that id'); - // } - - // const workspaceModel = await factories.workspacesFactory.findById(project.workspaceId.toString()); - - // /** - // * Remove project events - // */ - // await new EventsFactory(project._id).remove(); - - // /** - // * Remove project from workspace - // */ - // await new ProjectToWorkspace(workspaceModel._id.toString()).remove(project._id); - - // /** - // * Remove project - // */ - // await project.remove(); + for (const member of membersInfo) { + const userModel = await factories.usersFactory.findById(member.userId.toString()); + await workspaceModel.removeMember(userModel); + } - // return true; + const projectToWorkspace = new ProjectToWorkspace(workspaceId); + const projectsInfo = await projectToWorkspace.getProjects(); + if (projectsInfo.length) { + for (const project of projectsInfo) { + /** + * Remove project events + */ + await new EventsFactory(project._id).remove(); + /** + * Remove project from workspace + */ + await projectToWorkspace.remove(project._id); + /** + * Remove project + */ + const projectModel = await factories.projectsFactory.findById(project.id.toString()); + await projectModel.remove(); + } } } - await workspaceModel.removeMember(userModel); return true; }, From b4ea989b28ea4d57781c3fc8e3158c2b6ca1fe96 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 26 Oct 2021 03:08:57 +0000 Subject: [PATCH 04/37] Bump version up to 1.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eacce437..bbf9f151 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.0.5", + "version": "1.0.6", "main": "index.ts", "license": "UNLICENSED", "scripts": { From c724783f5c3518e95599f179b448ff62ac76a0e9 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 31 Oct 2021 13:24:05 +0000 Subject: [PATCH 05/37] Bump version up to 1.0.7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 69375d18..e55a38aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.0.6", + "version": "1.0.7", "main": "index.ts", "license": "UNLICENSED", "scripts": { From a62bab1500637cd46e53e260dd23fb4cb3e1791e Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:54:39 +0530 Subject: [PATCH 06/37] delete workspace added --- src/models/workspace.ts | 18 ++++++++++++++++++ src/resolvers/workspace.js | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/models/workspace.ts b/src/models/workspace.ts index 28bb2d9b..fcb94720 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -127,6 +127,24 @@ export default class WorkspaceModel extends AbstractModel imp } } + /** + * Delete workspace data + * + * @param workspace - member to remove + */ + public async deleteWorkspace(): Promise { + /** + * Delete the workspace data. + */ + await this.collection.deleteOne({ + _id: new ObjectId(this._id), + }); + /** + * Delete the team collection. + */ + await this.teamCollection.drop(); + } + /** * Update invite hash of workspace * @param inviteHash - new invite hash diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 4a01afb1..dd148167 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -363,6 +363,8 @@ module.exports = { await projectModel.remove(); } } + + await workspaceModel.deleteWorkspace(); } return true; }, From 3df315c0026cda98d458b5dc83e5bf6071a6e00b Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Wed, 3 Nov 2021 21:43:03 +0530 Subject: [PATCH 07/37] the is removed added --- src/models/workspace.ts | 12 +++++++----- src/resolvers/workspace.js | 20 -------------------- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/models/workspace.ts b/src/models/workspace.ts index 1b13ec17..76cfc5b6 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -128,17 +128,19 @@ export default class WorkspaceModel extends AbstractModel imp } /** - * Delete workspace data - * - * @param workspace - member to remove + * Delete workspace data */ public async deleteWorkspace(): Promise { /** * Delete the workspace data. */ - await this.collection.deleteOne({ + await this.collection.updateOne({ _id: new ObjectId(this._id), - }); + }, + { + $set: { isRemoved: true }, + } + ); /** * Delete the team collection. */ diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 887491b1..ddb55cb0 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -344,26 +344,6 @@ module.exports = { await workspaceModel.removeMember(userModel); } - const projectToWorkspace = new ProjectToWorkspace(workspaceId); - const projectsInfo = await projectToWorkspace.getProjects(); - if (projectsInfo.length) { - for (const project of projectsInfo) { - /** - * Remove project events - */ - await new EventsFactory(project._id).remove(); - /** - * Remove project from workspace - */ - await projectToWorkspace.remove(project._id); - /** - * Remove project - */ - const projectModel = await factories.projectsFactory.findById(project.id.toString()); - await projectModel.remove(); - } - } - await workspaceModel.deleteWorkspace(); } return true; From 305189bd56155d47f261088201ef52688d2893f2 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Wed, 3 Nov 2021 21:49:39 +0530 Subject: [PATCH 08/37] lint removed --- src/models/workspace.ts | 9 +++++---- src/resolvers/workspace.js | 6 ++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/models/workspace.ts b/src/models/workspace.ts index 76cfc5b6..ba5dfb6b 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -128,15 +128,16 @@ export default class WorkspaceModel extends AbstractModel imp } /** - * Delete workspace data + * Delete workspace data. */ public async deleteWorkspace(): Promise { /** * Delete the workspace data. */ - await this.collection.updateOne({ - _id: new ObjectId(this._id), - }, + await this.collection.updateOne( + { + _id: new ObjectId(this._id), + }, { $set: { isRemoved: true }, } diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index ddb55cb0..76435676 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -13,7 +13,6 @@ import { dateFromObjectId } from '../utils/dates'; const { ApolloError, UserInputError, ForbiddenError } = require('apollo-server-express'); const crypto = require('crypto'); -const EventsFactory = require('../models/eventsFactory'); /** * See all types and fields here {@see ../typeDefs/workspace.graphql} @@ -89,7 +88,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +100,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -328,7 +327,6 @@ module.exports = { * @return {Promise} - true if operation is successful */ async deleteWorkspace(_obj, { workspaceId }, { user, factories }) { - console.log("Yay! It's Working"); const workspaceModel = await factories.workspacesFactory.findById(workspaceId); if (!workspaceModel) { From c35832cddf0bda4b2048985c5455d6eafe8e1c63 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 12:45:50 +0530 Subject: [PATCH 09/37] workspace and project removal undo --- src/models/workspace.ts | 11 +++-------- src/resolvers/workspace.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/models/workspace.ts b/src/models/workspace.ts index ba5dfb6b..82c83de6 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -134,14 +134,9 @@ export default class WorkspaceModel extends AbstractModel imp /** * Delete the workspace data. */ - await this.collection.updateOne( - { - _id: new ObjectId(this._id), - }, - { - $set: { isRemoved: true }, - } - ); + await this.collection.deleteOne({ + _id: new ObjectId(this._id), + }); /** * Delete the team collection. */ diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 76435676..186c7540 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -13,6 +13,7 @@ import { dateFromObjectId } from '../utils/dates'; const { ApolloError, UserInputError, ForbiddenError } = require('apollo-server-express'); const crypto = require('crypto'); +const EventsFactory = require('../models/eventsFactory'); /** * See all types and fields here {@see ../typeDefs/workspace.graphql} @@ -88,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -100,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -318,6 +319,7 @@ module.exports = { return true; }, + /** * Mutation in order to leave workspace * @param {ResolverObj} _obj - object that contains the result returned from the resolver on the parent field @@ -337,13 +339,39 @@ module.exports = { if (memberInfo.isAdmin) { const membersInfo = (await workspaceModel.getMembers()); + for (const member of membersInfo) { const userModel = await factories.usersFactory.findById(member.userId.toString()); + await workspaceModel.removeMember(userModel); } + const projectToWorkspace = new ProjectToWorkspace(workspaceId); + + const projectsInfo = await projectToWorkspace.getProjects(); + + if (projectsInfo.length) { + for (const project of projectsInfo) { + /** + * Remove project events + */ + await new EventsFactory(project._id).remove(); + /** + * Remove project from workspace + */ + await projectToWorkspace.remove(project._id); + /** + * Remove project + */ + const projectModel = await factories.projectsFactory.findById(project.id.toString()); + + await projectModel.remove(); + } + } + await workspaceModel.deleteWorkspace(); } + return true; }, From d28e39429e15479f81ada4037e8c8b284609bef3 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 12:47:24 +0530 Subject: [PATCH 10/37] lint removed --- src/resolvers/workspace.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 186c7540..1514b7ad 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); From 25cdd04f0b54671d821347196c5c19b65316e83e Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 17:42:24 +0530 Subject: [PATCH 11/37] the logic modified --- src/resolvers/workspace.js | 67 ++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 1514b7ad..2d7b4d2f 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -337,41 +337,46 @@ module.exports = { const memberInfo = await workspaceModel.getMemberInfo(user.id); - if (memberInfo.isAdmin) { - const membersInfo = (await workspaceModel.getMembers()); + /** + * Return if user is not Admin. + */ + if (!memberInfo.isAdmin) { + return false; + } - for (const member of membersInfo) { - const userModel = await factories.usersFactory.findById(member.userId.toString()); + const membersInfo = (await workspaceModel.getMembers()); - await workspaceModel.removeMember(userModel); - } + for (const member of membersInfo) { + const userModel = await factories.usersFactory.findById(member.userId.toString()); - const projectToWorkspace = new ProjectToWorkspace(workspaceId); - - const projectsInfo = await projectToWorkspace.getProjects(); - - if (projectsInfo.length) { - for (const project of projectsInfo) { - /** - * Remove project events - */ - await new EventsFactory(project._id).remove(); - /** - * Remove project from workspace - */ - await projectToWorkspace.remove(project._id); - /** - * Remove project - */ - const projectModel = await factories.projectsFactory.findById(project.id.toString()); - - await projectModel.remove(); - } - } + await userModel.removeWorkspace(workspaceId.toString()); + } - await workspaceModel.deleteWorkspace(); + const projectToWorkspace = new ProjectToWorkspace(workspaceId); + + const projectsInfo = await projectToWorkspace.getProjects(); + + if (projectsInfo.length) { + for (const project of projectsInfo) { + /** + * Remove project events + */ + await new EventsFactory(project._id).remove(); + /** + * Remove project from workspace + */ + await projectToWorkspace.remove(project._id); + /** + * Remove project + */ + const projectModel = await factories.projectsFactory.findById(project.id.toString()); + + await projectModel.remove(); + } } + await workspaceModel.deleteWorkspace(); + return true; }, From 30557c6389c9908900bee9d0a194c15409840cbe Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 17:42:50 +0530 Subject: [PATCH 12/37] the schema modified --- src/typeDefs/workspace.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typeDefs/workspace.ts b/src/typeDefs/workspace.ts index 9c543dd4..be13b89b 100644 --- a/src/typeDefs/workspace.ts +++ b/src/typeDefs/workspace.ts @@ -285,6 +285,6 @@ export default gql` Workspace ID """ workspaceId: ID! - ): Boolean! @requireAuth + ): Boolean! @requireAdmin } `; From d610da586ce63a814bec373a2cc6524fa69463ff Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 17:43:26 +0530 Subject: [PATCH 13/37] linting added --- src/resolvers/workspace.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 2d7b4d2f..50e47140 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); From c5e77f8ad7ee0c928cee8e6297a3dcfa1894cbc5 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 19:53:16 +0530 Subject: [PATCH 14/37] delete project collection in added --- src/models/projectToWorkspace.js | 9 +++++++++ src/resolvers/workspace.js | 14 +++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/models/projectToWorkspace.js b/src/models/projectToWorkspace.js index 02e2384d..4ecdca19 100644 --- a/src/models/projectToWorkspace.js +++ b/src/models/projectToWorkspace.js @@ -93,6 +93,15 @@ class ProjectToWorkspace { await this.collection.deleteOne({ projectId: projectId }); } + /** + * delete project collection. + * + * @return {Promise} + */ + async delete() { + await this.collection.drop(); + } + /** * Gets projects in workspace. * If ids were not passed, returns all projects in workspace. diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 50e47140..045aeba3 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -352,7 +352,7 @@ module.exports = { await userModel.removeWorkspace(workspaceId.toString()); } - const projectToWorkspace = new ProjectToWorkspace(workspaceId); + const projectToWorkspace = new ProjectToWorkspace(workspaceId.toString()); const projectsInfo = await projectToWorkspace.getProjects(); @@ -362,10 +362,6 @@ module.exports = { * Remove project events */ await new EventsFactory(project._id).remove(); - /** - * Remove project from workspace - */ - await projectToWorkspace.remove(project._id); /** * Remove project */ @@ -374,6 +370,10 @@ module.exports = { await projectModel.remove(); } } + /** + * Delete projects. + */ + await projectToWorkspace.delete(); await workspaceModel.deleteWorkspace(); From f4b9d17815ecfe623ff5513a9f3210bec566bc3f Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Thu, 4 Nov 2021 19:58:36 +0530 Subject: [PATCH 15/37] lint removed --- src/resolvers/workspace.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 045aeba3..486f2053 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); From 560fd0392fc9df39dfecd6a76957e49eae0f02d4 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:52:37 +0530 Subject: [PATCH 16/37] remove workspace using isRemoved flag --- src/models/workspace.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/models/workspace.ts b/src/models/workspace.ts index 82c83de6..7eee6f1d 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -128,14 +128,16 @@ export default class WorkspaceModel extends AbstractModel imp } /** - * Delete workspace data. + * remove workspace data using isRemoved Flag. */ - public async deleteWorkspace(): Promise { + public async removeWorkspaceByFlag(): Promise { /** * Delete the workspace data. */ - await this.collection.deleteOne({ + await this.collection.updateOne({ _id: new ObjectId(this._id), + }, { + $set: { isRemoved: true }, }); /** * Delete the team collection. From 8361c89c604401ae5cb69ff2a5fdde18fd06854b Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:53:05 +0530 Subject: [PATCH 17/37] remove project using isRemoved flag --- src/models/project.ts | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/models/project.ts b/src/models/project.ts index d511ee6a..d1d35657 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -235,14 +235,14 @@ export default class ProjectModel extends AbstractModel impleme await this.collection.updateOne({ _id: this._id, }, - { - $push: { - notifications: { - $each: [ rule ], - $position: 0, + { + $push: { + notifications: { + $each: [rule], + $position: 0, + }, }, - }, - }); + }); return rule; } @@ -383,4 +383,24 @@ export default class ProjectModel extends AbstractModel impleme console.log(error); } } + + /** + * Remove project data using isRemoved flag. + */ + public async removeByFlag(): Promise { + await this.collection.updateOne({ _id: this._id }, { + $set: { isRemoved: true }, + }); + + try { + /** + * Remove users in project collection + */ + await this.dbConnection.collection('users-in-project:' + this._id) + .drop(); + } catch (error) { + console.log(`Can't remove collection "users-in-project:${this._id}" because it doesn't exist.`); + console.log(error); + } + } } From 65722da3969d3b5a0890191446240b04fa8dccfc Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:53:33 +0530 Subject: [PATCH 18/37] remove deletion of projectToWorkspace Relation --- src/models/projectToWorkspace.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/models/projectToWorkspace.js b/src/models/projectToWorkspace.js index 4ecdca19..02e2384d 100644 --- a/src/models/projectToWorkspace.js +++ b/src/models/projectToWorkspace.js @@ -93,15 +93,6 @@ class ProjectToWorkspace { await this.collection.deleteOne({ projectId: projectId }); } - /** - * delete project collection. - * - * @return {Promise} - */ - async delete() { - await this.collection.drop(); - } - /** * Gets projects in workspace. * If ids were not passed, returns all projects in workspace. From 5115652d515d740e30c840fea413706aa26871f1 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:54:40 +0530 Subject: [PATCH 19/37] remove workspace from user using isRemoved flag --- src/models/user.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/models/user.ts b/src/models/user.ts index d5a7b575..4a738fb0 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -62,7 +62,7 @@ export interface UserNotificationsDBScheme { /** * Types of notifications to receive */ - whatToReceive: {[key in UserNotificationType]: boolean}; + whatToReceive: { [key in UserNotificationType]: boolean }; } /** @@ -317,7 +317,7 @@ export default class UserModel extends AbstractModel implements Us * Remove workspace from membership collection * @param workspaceId - id of workspace to remove */ - public async removeWorkspace(workspaceId: string): Promise<{workspaceId: string}> { + public async removeWorkspace(workspaceId: string): Promise<{ workspaceId: string }> { await this.membershipCollection.deleteOne({ workspaceId: new ObjectId(workspaceId), }); @@ -327,6 +327,22 @@ export default class UserModel extends AbstractModel implements Us }; } + /** + * Remove workspace from membership collection by using isRemovedFlag. + * @param workspaceId - id of workspace to remove + */ + public async removeWorkspaceByFlag(workspaceId: string): Promise<{ workspaceId: string }> { + await this.membershipCollection.updateOne({ + workspaceId: new ObjectId(workspaceId), + }, { + $set: { isRemoved: true }, + }); + + return { + workspaceId, + }; + } + /** * Confirm membership of workspace by id * @param workspaceId - workspace id to confirm @@ -351,7 +367,8 @@ export default class UserModel extends AbstractModel implements Us workspaceId: { $in: idsAsObjectId, }, - } : {}; + isRemoved: { $exists: false }, + } : { isRemoved: { $exists: false } }; const membershipDocuments = await this.membershipCollection.find(searchQuery).toArray(); From 1b9b7c3d6ceddbc866960295ecd66dd174f4b1c2 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:54:59 +0530 Subject: [PATCH 20/37] integration with the delete workspace --- src/resolvers/workspace.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 486f2053..f2439bcd 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -349,7 +349,10 @@ module.exports = { for (const member of membersInfo) { const userModel = await factories.usersFactory.findById(member.userId.toString()); - await userModel.removeWorkspace(workspaceId.toString()); + /** + * remove workspace by using isRemoved flag. + */ + await userModel.removeWorkspaceByFlag(workspaceId.toString()); } const projectToWorkspace = new ProjectToWorkspace(workspaceId.toString()); @@ -367,15 +370,11 @@ module.exports = { */ const projectModel = await factories.projectsFactory.findById(project.id.toString()); - await projectModel.remove(); + await projectModel.removeByFlag(); } } - /** - * Delete projects. - */ - await projectToWorkspace.delete(); - await workspaceModel.deleteWorkspace(); + await workspaceModel.removeWorkspaceByFlag(); return true; }, From e9b802c0ee735bcfb871d9d5173202da06c6fa8d Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:56:25 +0530 Subject: [PATCH 21/37] lint removed --- src/models/project.ts | 14 +++++++------- src/resolvers/workspace.js | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/models/project.ts b/src/models/project.ts index d1d35657..dd52d980 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -235,14 +235,14 @@ export default class ProjectModel extends AbstractModel impleme await this.collection.updateOne({ _id: this._id, }, - { - $push: { - notifications: { - $each: [rule], - $position: 0, - }, + { + $push: { + notifications: { + $each: [ rule ], + $position: 0, }, - }); + }, + }); return rule; } diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index f2439bcd..75f81a0a 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); From 6af620327b5934afdb6f49f31414c54c00a339b6 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Fri, 19 Nov 2021 23:57:10 +0530 Subject: [PATCH 22/37] rename the functions --- src/models/project.ts | 18 +++++++++--------- src/models/user.ts | 4 ++-- src/models/workspace.ts | 4 ++-- src/resolvers/workspace.js | 15 ++++++--------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/models/project.ts b/src/models/project.ts index dd52d980..b4d4c51e 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -235,14 +235,14 @@ export default class ProjectModel extends AbstractModel impleme await this.collection.updateOne({ _id: this._id, }, - { - $push: { - notifications: { - $each: [ rule ], - $position: 0, + { + $push: { + notifications: { + $each: [rule], + $position: 0, + }, }, - }, - }); + }); return rule; } @@ -385,9 +385,9 @@ export default class ProjectModel extends AbstractModel impleme } /** - * Remove project data using isRemoved flag. + * Mark project as removed. */ - public async removeByFlag(): Promise { + public async markProjectAsRemoved(): Promise { await this.collection.updateOne({ _id: this._id }, { $set: { isRemoved: true }, }); diff --git a/src/models/user.ts b/src/models/user.ts index 4a738fb0..93df8737 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -328,10 +328,10 @@ export default class UserModel extends AbstractModel implements Us } /** - * Remove workspace from membership collection by using isRemovedFlag. + * Mark workspace as removed. * @param workspaceId - id of workspace to remove */ - public async removeWorkspaceByFlag(workspaceId: string): Promise<{ workspaceId: string }> { + public async markWorkspaceAsRemoved(workspaceId: string): Promise<{ workspaceId: string }> { await this.membershipCollection.updateOne({ workspaceId: new ObjectId(workspaceId), }, { diff --git a/src/models/workspace.ts b/src/models/workspace.ts index 7eee6f1d..7f455e29 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -128,9 +128,9 @@ export default class WorkspaceModel extends AbstractModel imp } /** - * remove workspace data using isRemoved Flag. + * Mark workspace as removed. */ - public async removeWorkspaceByFlag(): Promise { + public async markWorkspaceAsRemoved(): Promise { /** * Delete the workspace data. */ diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 75f81a0a..1c35306d 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -348,11 +348,8 @@ module.exports = { for (const member of membersInfo) { const userModel = await factories.usersFactory.findById(member.userId.toString()); - - /** - * remove workspace by using isRemoved flag. - */ - await userModel.removeWorkspaceByFlag(workspaceId.toString()); + s + await userModel.markWorkspaceAsRemoved(workspaceId.toString()); } const projectToWorkspace = new ProjectToWorkspace(workspaceId.toString()); @@ -370,11 +367,11 @@ module.exports = { */ const projectModel = await factories.projectsFactory.findById(project.id.toString()); - await projectModel.removeByFlag(); + await projectModel.markProjectAsRemoved(); } } - await workspaceModel.removeWorkspaceByFlag(); + await workspaceModel.markWorkspaceAsRemoved(); return true; }, From e2f0af387d9a5c7eaec710ebe4f94d71cddf019c Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Fri, 19 Nov 2021 23:59:48 +0530 Subject: [PATCH 23/37] lint removed --- src/models/project.ts | 14 +++++++------- src/resolvers/workspace.js | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/models/project.ts b/src/models/project.ts index b4d4c51e..a9fcdddc 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -235,14 +235,14 @@ export default class ProjectModel extends AbstractModel impleme await this.collection.updateOne({ _id: this._id, }, - { - $push: { - notifications: { - $each: [rule], - $position: 0, - }, + { + $push: { + notifications: { + $each: [ rule ], + $position: 0, }, - }); + }, + }); return rule; } diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 1c35306d..4ed092c3 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -348,7 +348,7 @@ module.exports = { for (const member of membersInfo) { const userModel = await factories.usersFactory.findById(member.userId.toString()); - s + await userModel.markWorkspaceAsRemoved(workspaceId.toString()); } From ac47016ba302bab41920e39cd7ea0a84d602b4cc Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 7 Dec 2021 23:27:51 +0530 Subject: [PATCH 24/37] $ne=>$exists --- src/models/user.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/user.ts b/src/models/user.ts index 93df8737..6f9d586c 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -367,8 +367,8 @@ export default class UserModel extends AbstractModel implements Us workspaceId: { $in: idsAsObjectId, }, - isRemoved: { $exists: false }, - } : { isRemoved: { $exists: false } }; + isRemoved: { $ne: true }, + } : { isRemoved: { $ne: true } }; const membershipDocuments = await this.membershipCollection.find(searchQuery).toArray(); From f7f0c5039ec40e6f72b1f440197ceb00f434d413 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 7 Dec 2021 23:30:33 +0530 Subject: [PATCH 25/37] remove teamcollection dropping --- src/models/workspace.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/models/workspace.ts b/src/models/workspace.ts index 7f455e29..12157034 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -139,10 +139,6 @@ export default class WorkspaceModel extends AbstractModel imp }, { $set: { isRemoved: true }, }); - /** - * Delete the team collection. - */ - await this.teamCollection.drop(); } /** From 4e4fa39323552bb7a308f47b52072fb65cc2f1b2 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 7 Dec 2021 23:32:56 +0530 Subject: [PATCH 26/37] not delete the event factories --- src/resolvers/workspace.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 4ed092c3..7c655568 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -89,7 +89,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +101,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -358,10 +358,6 @@ module.exports = { if (projectsInfo.length) { for (const project of projectsInfo) { - /** - * Remove project events - */ - await new EventsFactory(project._id).remove(); /** * Remove project */ From 0a69fa6bb128de78b3ce5937af61d46e149b095d Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 7 Dec 2021 23:57:18 +0530 Subject: [PATCH 27/37] removed lint and event factory --- src/resolvers/workspace.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 7c655568..f9665d18 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -13,7 +13,6 @@ import { dateFromObjectId } from '../utils/dates'; const { ApolloError, UserInputError, ForbiddenError } = require('apollo-server-express'); const crypto = require('crypto'); -const EventsFactory = require('../models/eventsFactory'); /** * See all types and fields here {@see ../typeDefs/workspace.graphql} @@ -89,7 +88,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -101,7 +100,7 @@ module.exports = { if (!invitedUser) { await workspace.addUnregisteredMember(userEmail); } else { - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); From 628a86e47c8db95964b3c6fe34dd96a13218eaf8 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 20 Dec 2021 17:46:12 +0000 Subject: [PATCH 28/37] Bump version up to 1.0.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e55a38aa..06451a90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.0.7", + "version": "1.0.8", "main": "index.ts", "license": "UNLICENSED", "scripts": { From f116fc2ab8858a7b5d5fef98f35fe67c1fdf81a0 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Wed, 19 Jan 2022 23:00:05 +0530 Subject: [PATCH 29/37] forbidden error remove and replace with return flag --- src/models/user.ts | 6 +++--- src/resolvers/workspace.js | 6 +++--- src/typeDefs/workspace.ts | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/models/user.ts b/src/models/user.ts index 3f3e2925..c21cc1c8 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -367,14 +367,14 @@ export default class UserModel extends AbstractModel implements Us workspaceId: { $in: idsAsObjectId, }, - isRemoved: { + isRemoved: { $ne: true, }, isPending: { $ne: true, }, - } : { - isRemoved: { + } : { + isRemoved: { $ne: true, }, isPending: { diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 046c241c..48682780 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -11,7 +11,7 @@ import ProjectToWorkspace from '../models/projectToWorkspace'; import Validator from '../utils/validator'; import { dateFromObjectId } from '../utils/dates'; -const { ApolloError, UserInputError, ForbiddenError } = require('apollo-server-express'); +const { ApolloError, UserInputError } = require('apollo-server-express'); const crypto = require('crypto'); /** @@ -301,7 +301,7 @@ module.exports = { * @param {string} workspaceId - id of the workspace where the user should be removed * @param {UserInContext} user - current authorized user {@see ../index.js} * @param {ContextFactories} factories - factories for working with models - * @return {Promise} - true if operation is successful + * @return {Promise} - true if operation is successful, false if there is no other admin left. */ async leaveWorkspace(_obj, { workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); @@ -320,7 +320,7 @@ module.exports = { ); if (!isThereOtherAdmins) { - throw new ForbiddenError('You can\'t leave this workspace because you are the last admin'); + return false; } } await workspaceModel.removeMember(userModel); diff --git a/src/typeDefs/workspace.ts b/src/typeDefs/workspace.ts index be13b89b..25dae331 100644 --- a/src/typeDefs/workspace.ts +++ b/src/typeDefs/workspace.ts @@ -268,6 +268,7 @@ export default gql` """ Mutation in order to leave workspace Returns true if operation is successful + Returns false if there is no other admin lefts """ leaveWorkspace( """ From bc244c29cec17ccaf3a30347be8f29526227578f Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Fri, 28 Jan 2022 23:18:11 +0530 Subject: [PATCH 30/37] remove isAdmin from deleteworkspace --- src/resolvers/workspace.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 48682780..51299e7c 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -88,7 +88,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); + const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -104,7 +104,7 @@ module.exports = { /** * Check for a membership */ - const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); + const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); @@ -125,7 +125,7 @@ module.exports = { .digest('hex'); const inviteLink = `${process.env.GARAGE_URL}/join/${workspaceId}/${linkHash}`; - + console.log(inviteLink); await emailNotification({ type: SenderWorkerTaskType.WorkspaceInvite, payload: { @@ -343,15 +343,6 @@ module.exports = { throw new UserInputError('There is no workspace with provided id'); } - const memberInfo = await workspaceModel.getMemberInfo(user.id); - - /** - * Return if user is not Admin. - */ - if (!memberInfo.isAdmin) { - return false; - } - const membersInfo = (await workspaceModel.getMembers()); for (const member of membersInfo) { From f192ccca09736204241f44ecc23e563bcd1ec037 Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 1 Feb 2022 23:09:04 +0530 Subject: [PATCH 31/37] update data loader with not to get isRemoved filed --- src/dataLoaders.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dataLoaders.ts b/src/dataLoaders.ts index 6c696049..6f7abc72 100644 --- a/src/dataLoaders.ts +++ b/src/dataLoaders.ts @@ -83,6 +83,9 @@ export default class DataLoaders { const queryResult = await this.dbConnection.collection(collectionName) .find({ [fieldName]: { $in: values }, + isRemoved: { + $ne: true, + }, }) .toArray(); From 20c75ce914f8038cbcca40e883a4dd188849977e Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 1 Feb 2022 23:10:11 +0530 Subject: [PATCH 32/37] join by link and invitation link function updated --- src/models/workspacesFactory.ts | 11 ++++++++- src/resolvers/workspace.js | 41 ++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/models/workspacesFactory.ts b/src/models/workspacesFactory.ts index e1f7b2cd..ada11cda 100644 --- a/src/models/workspacesFactory.ts +++ b/src/models/workspacesFactory.ts @@ -99,7 +99,16 @@ export default class WorkspacesFactory extends AbstractModelFactory { - const workspaceData = await this.collection.findOne({ inviteHash }); + const workspaceData = await this.collection.findOne({ + inviteHash, + isRemoved: { + $ne: true, + }, + }); + + if (!workspaceData) { + return null; + } return workspaceData && new WorkspaceModel(workspaceData); } diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 51299e7c..954c4a92 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -125,7 +125,7 @@ module.exports = { .digest('hex'); const inviteLink = `${process.env.GARAGE_URL}/join/${workspaceId}/${linkHash}`; - console.log(inviteLink); + await emailNotification({ type: SenderWorkerTaskType.WorkspaceInvite, payload: { @@ -154,6 +154,10 @@ module.exports = { const currentUser = await factories.usersFactory.findById(user.id); const workspace = await factories.workspacesFactory.findByInviteHash(inviteHash); + if (!workspace) { + throw new ApolloError('There is no workspace with provided id'); + } + if (await workspace.getMemberInfo(user.id)) { throw new ApolloError('You are already member of this workspace'); } @@ -182,6 +186,10 @@ module.exports = { const currentUser = await factories.usersFactory.findById(user.id); const workspace = await factories.workspacesFactory.findById(workspaceId); + if (!workspace) { + throw new ApolloError('There is no workspace with provided id'); + } + const hash = crypto .createHash('sha256') .update(`${workspaceId}:${currentUser.email}:${process.env.INVITE_LINK_HASH_SALT}`) @@ -346,9 +354,32 @@ module.exports = { const membersInfo = (await workspaceModel.getMembers()); for (const member of membersInfo) { - const userModel = await factories.usersFactory.findById(member.userId.toString()); + /** + * remove members from workspace. + */ + if (member.userId) { + const userModel = await factories.usersFactory.findById(member.userId.toString()); - await userModel.markWorkspaceAsRemoved(workspaceId.toString()); + await userModel.markWorkspaceAsRemoved(workspaceId.toString()); + } + /** + * remove the members who's invitation is pending. + */ + if (member.userEmail) { + const invitedUser = await factories.usersFactory.findByEmail(member.userEmail); + + /** + * If user is already uses Hawk + */ + if (invitedUser) { + await invitedUser.removeWorkspace(workspaceId); + } + + /** + * Remove User's invitation from workspace. + */ + await workspaceModel.removeMemberByEmail(member.userEmail); + } } const projectToWorkspace = new ProjectToWorkspace(workspaceId.toString()); @@ -435,6 +466,7 @@ module.exports = { /** * Return empty object to call resolver for specific mutation + * @returns Empty object. */ workspace: () => ({}), }, @@ -524,6 +556,7 @@ module.exports = { /** * Returns type of the team member * @param {MemberDBScheme} memberData - result from resolver above + * @returns type of member. */ __resolveType(memberData) { return WorkspaceModel.isPendingMember(memberData) ? 'PendingMember' : 'ConfirmedMember'; @@ -539,6 +572,7 @@ module.exports = { * @param {ConfirmedMemberDBScheme} memberData - result from resolver above * @param _args - empty list of args * @param {ContextFactories} factories - factories for working with models + * @returns user data of the workspace */ user(memberData, _args, { factories }) { return factories.usersFactory.findById(memberData.userId.toString()); @@ -547,6 +581,7 @@ module.exports = { /** * True if user has admin permissions * @param {ConfirmedMemberDBScheme} memberData - result from resolver above + * @returns status of is user admin */ isAdmin(memberData) { return !WorkspaceModel.isPendingMember(memberData) && (memberData.isAdmin || false); From ee773b870955196c6c9cf71b71f823fd3aa5e2bf Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Tue, 1 Feb 2022 23:10:34 +0530 Subject: [PATCH 33/37] jsdocs warning resolved --- src/models/project.ts | 2 ++ src/models/projectToWorkspace.js | 5 ++++- src/models/user.ts | 6 +++--- src/models/workspace.ts | 6 +++++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/models/project.ts b/src/models/project.ts index a9fcdddc..734b2d8b 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -194,6 +194,7 @@ export default class ProjectModel extends AbstractModel impleme /** * Generates integration ID that's used in collector URL for sending events + * @returns integration ID as string. */ public static generateIntegrationId(): string { return uuid.v4(); @@ -203,6 +204,7 @@ export default class ProjectModel extends AbstractModel impleme * Generates new integration token with integration id field * * @param integrationId - integration id for using in collector URL + * @returns generated integration token. */ public static generateIntegrationToken(integrationId: string): string { const secret = uuid.v4(); diff --git a/src/models/projectToWorkspace.js b/src/models/projectToWorkspace.js index 02e2384d..ae50e596 100644 --- a/src/models/projectToWorkspace.js +++ b/src/models/projectToWorkspace.js @@ -15,7 +15,7 @@ const { ObjectID } = require('mongodb'); class ProjectToWorkspace { /** * Creates an instance of ProjectToWorkspace - * @param {string|ObjectID} workspaceId + * @param {string|ObjectID} workspaceId workspace. */ constructor(workspaceId) { this.workspaceId = new ObjectID(workspaceId); @@ -53,6 +53,9 @@ class ProjectToWorkspace { async findById(projectWorkspaceId) { const projectWorkspace = await this.collection.findOne({ _id: new ObjectID(projectWorkspaceId), + isRemoved: { + $ne: true, + }, }); if (!projectWorkspace) { diff --git a/src/models/user.ts b/src/models/user.ts index c21cc1c8..a257d216 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -165,6 +165,7 @@ export default class UserModel extends AbstractModel implements Us /** * Generate 16bytes password + * @returns generated password. */ public static generatePassword(): Promise { return new Promise((resolve, reject) => { @@ -172,7 +173,7 @@ export default class UserModel extends AbstractModel implements Us if (err) { return reject(err); } - + console.log(buff.toString('hex')); resolve(buff.toString('hex')); }); }); @@ -182,6 +183,7 @@ export default class UserModel extends AbstractModel implements Us * Compose default notifications settings for new users. * * @param email - user email from the sign-up form will be used as email-channel endpoint + * @returns default notification settings for new users. */ public static generateDefaultNotificationsSettings(email: string): UserNotificationsDBScheme { return { @@ -212,7 +214,6 @@ export default class UserModel extends AbstractModel implements Us * Change user's password * Hashes new password and updates the document * - * @param userId - user ID * @param newPassword - new user password */ public async changePassword(newPassword: string): Promise { @@ -230,7 +231,6 @@ export default class UserModel extends AbstractModel implements Us /** * Update user profile data - * @param userId - user ID * @param user – user object */ public async updateProfile(user: Partial): Promise { diff --git a/src/models/workspace.ts b/src/models/workspace.ts index c623bfc8..97fcd790 100644 --- a/src/models/workspace.ts +++ b/src/models/workspace.ts @@ -94,6 +94,7 @@ export default class WorkspaceModel extends AbstractModel imp /** * Generates SHA-256 hash that used as invite hash + * @returns invitation hash as string. */ public static generateInviteHash(): string { return crypto @@ -106,6 +107,7 @@ export default class WorkspaceModel extends AbstractModel imp * Checks is provided document represents pending member * * @param doc - doc to check + * @returns status of is current member pending. */ public static isPendingMember(doc: MemberDBScheme): doc is PendingMemberDBScheme { return !!(doc as PendingMemberDBScheme).userEmail && !(doc as ConfirmedMemberDBScheme).userId; @@ -427,6 +429,7 @@ export default class WorkspaceModel extends AbstractModel imp /** * Due date of the current workspace tariff plan + * @returns Date object of due date. */ public getTariffPlanDueDate(): Date { const lastChargeDate = new Date(this.lastChargeDate); @@ -435,7 +438,8 @@ export default class WorkspaceModel extends AbstractModel imp } /** - * Is tariff plan expired or not + * Is tariff plan expired or not. + * @returns current status of tariff plan. */ public isTariffPlanExpired(): boolean { const date = new Date(); From b37dfc883910d93784f7c3289588c3b9d9cb1f00 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 1 Feb 2022 18:33:50 +0000 Subject: [PATCH 34/37] Bump version up to 1.0.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3ce8b1c9..b58c813e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.0.8", + "version": "1.0.9", "main": "index.ts", "license": "UNLICENSED", "scripts": { From 52c265898bf2b6bc97be7f71a14f57e816ac7e95 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 15 Feb 2022 15:03:56 +0000 Subject: [PATCH 35/37] Bump version up to 1.0.10 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b58c813e..b89f7af5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.0.9", + "version": "1.0.10", "main": "index.ts", "license": "UNLICENSED", "scripts": { From bf1c901f2663de6baecdf458f76e79d85900e1de Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Wed, 16 Feb 2022 21:32:31 +0530 Subject: [PATCH 36/37] lint removed --- src/resolvers/workspace.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resolvers/workspace.js b/src/resolvers/workspace.js index 84c6abd2..736b7e00 100644 --- a/src/resolvers/workspace.js +++ b/src/resolvers/workspace.js @@ -88,7 +88,7 @@ module.exports = { */ async inviteToWorkspace(_obj, { userEmail, workspaceId }, { user, factories }) { const userModel = await factories.usersFactory.findById(user.id); - const [isWorkspaceBelongsToUser] = await userModel.getWorkspacesIds([workspaceId]); + const [ isWorkspaceBelongsToUser ] = await userModel.getWorkspacesIds([ workspaceId ]); if (!isWorkspaceBelongsToUser) { throw new ApolloError('There is no workspace with that id'); @@ -104,7 +104,7 @@ module.exports = { /** * Check for a membership */ - const [isUserInThatWorkspace] = await invitedUser.getWorkspacesIds([workspaceId]); + const [ isUserInThatWorkspace ] = await invitedUser.getWorkspacesIds([ workspaceId ]); if (isUserInThatWorkspace) { throw new ApolloError('User already invited to this workspace'); From 375d4daf3de9a5920e99d7511ce417dc3980be87 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 25 Dec 2022 20:29:16 +0000 Subject: [PATCH 37/37] Bump version up to 1.0.23 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 996acd1d..91016128 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.0.22", + "version": "1.0.23", "main": "index.ts", "license": "UNLICENSED", "scripts": {