diff --git a/package.json b/package.json index ec4a63f..4b41bfc 100644 --- a/package.json +++ b/package.json @@ -36,11 +36,11 @@ "express-validator": "^6.15.0", "graphql": "^15.8.0", "graphql-tag": "^2.12.6", - "helmet": "^6.1.1", + "helmet": "^6.1.2", "jsonwebtoken": "^9.0.0", "moment": "^2.29.4", "mongodb": "^5.2.0", - "mongoose": "^6.10.1", + "mongoose": "^7.0.3", "pino": "^8.11.0", "pino-pretty": "^10.0.0", "swagger-ui-express": "^4.6.2", diff --git a/src/models/admin.js b/src/models/admin.js index 792d843..175eb98 100644 --- a/src/models/admin.js +++ b/src/models/admin.js @@ -3,7 +3,6 @@ import moment from 'moment'; import bcryptjs from 'bcryptjs'; import crypto from 'crypto'; import jwt from 'jsonwebtoken'; -import Domain from './domain'; import { Team } from './team'; import { notifyAcCreation, notifyAcDeletion } from '../external/switcher-api-facade'; @@ -180,11 +179,12 @@ adminSchema.statics.createThirdPartyAccount = async ( _avatar: userInfo.avatar, password: hash }); + await admin.save(); - } else { - admin._avatar = userInfo.avatar; } - + + admin.name = userInfo.name; + admin._avatar = userInfo.avatar; return admin; }; @@ -210,17 +210,8 @@ adminSchema.post('save', function(error, _doc, next) { next(error); }); -adminSchema.pre('remove', async function (next) { - const ObjectId = (require('mongoose').Types.ObjectId); - +adminSchema.pre('deleteOne', { document: true, query: false }, async function (next) { const admin = this; - const domains = await Domain.find({ owner: new ObjectId(admin._id) }).exec(); - - if (domains) { - for (const domain of domains) - await domain.remove(); - } - const teams = await Team.find({ members: admin._id }).exec(); for (const team of teams) { let indexMmeber = team.members.indexOf(admin._id); diff --git a/src/models/component.js b/src/models/component.js index 67f4067..946086f 100644 --- a/src/models/component.js +++ b/src/models/component.js @@ -119,7 +119,7 @@ componentSchema.pre('validate', async function (next) { next(); }); -componentSchema.pre('remove', async function (next) { +componentSchema.pre('deleteOne', { document: true, query: false }, async function (next) { const component = this; const configsToRemoveFrom = await Config.find({ components: { $in: [component._id] } }).exec(); diff --git a/src/models/config-strategy.js b/src/models/config-strategy.js index efd2339..9493dfc 100644 --- a/src/models/config-strategy.js +++ b/src/models/config-strategy.js @@ -375,7 +375,7 @@ configStrategySchema.options.toJSON = { } }; -configStrategySchema.pre('remove', async function (next) { +configStrategySchema.pre('deleteOne', { document: true, query: false }, async function (next) { const strategyConfig = this; await History.deleteMany({ domainId: strategyConfig.domain, elementId: strategyConfig._id }).exec(); diff --git a/src/models/config.js b/src/models/config.js index 1435c87..576ff8b 100644 --- a/src/models/config.js +++ b/src/models/config.js @@ -154,13 +154,13 @@ configSchema.virtual('configStrategy', { foreignField: 'config' }); -configSchema.pre('remove', async function (next) { +configSchema.pre('deleteOne', { document: true, query: false }, async function (next) { const config = this; const strategies = await ConfigStrategy.find({ config: config._id }).exec(); if (strategies) { for (const strategy of strategies) { - await strategy.remove(); + await Promise.resolve(strategy.deleteOne()); } } diff --git a/src/models/domain.js b/src/models/domain.js index f9464ba..0e253d7 100644 --- a/src/models/domain.js +++ b/src/models/domain.js @@ -96,20 +96,20 @@ domainSchema.options.toJSON = { } }; -domainSchema.pre('remove', async function (next) { +domainSchema.pre('deleteOne', { document: true, query: false }, async function (next) { const domain = this; const groups = await GroupConfig.find({ domain: domain._id }).exec(); if (groups) { for (const group of groups) { - await group.remove(); + await Promise.resolve(group.deleteOne()); } } const teams = await Team.find({ domain: domain._id }).exec(); if (teams) { for (const team of teams) { - await team.remove(); + await Promise.resolve(team.deleteOne()); } } diff --git a/src/models/group-config.js b/src/models/group-config.js index b916eac..c0538d8 100644 --- a/src/models/group-config.js +++ b/src/models/group-config.js @@ -67,13 +67,13 @@ groupConfigSchema.virtual('config', { foreignField: 'group' }); -groupConfigSchema.pre('remove', async function (next) { +groupConfigSchema.pre('deleteOne', { document: true, query: false }, async function (next) { const group = this; const configs = await Config.find({ group: group._id }).exec(); if (configs) { for (const config of configs) { - await config.remove(); + await Promise.resolve(config.deleteOne()); } } diff --git a/src/models/team.js b/src/models/team.js index 8bc6174..9c9c680 100644 --- a/src/models/team.js +++ b/src/models/team.js @@ -78,7 +78,7 @@ teamSchema.pre('validate', async function (next) { next(); }); -teamSchema.pre('remove', async function (next) { +teamSchema.pre('deleteOne', { document: true, query: false }, async function (next) { const team = this; await Permission.deleteMany({ _id: { $in: team.permissions } }).exec(); diff --git a/src/routers/environment.js b/src/routers/environment.js index b534271..bb239bd 100644 --- a/src/routers/environment.js +++ b/src/routers/environment.js @@ -58,7 +58,7 @@ router.delete('/environment/:id', auth, [ ], validate, async (req, res) => { try { const environment = await Services.deleteEnvironment(req.params.id, req.admin); - await environment.remove(); + await environment.deleteOne(); res.send(environment); } catch (e) { responseException(res, e, 400); diff --git a/src/services/admin.js b/src/services/admin.js index 06e0836..48229bd 100644 --- a/src/services/admin.js +++ b/src/services/admin.js @@ -145,5 +145,5 @@ export async function deleteAccount(admin) { `This account has ${domains} Domain(s) that must be either deleted or transfered to another account.`); } - return admin.remove(); + return admin.deleteOne(); } \ No newline at end of file diff --git a/src/services/component.js b/src/services/component.js index 99f65b0..5d702b3 100644 --- a/src/services/component.js +++ b/src/services/component.js @@ -47,7 +47,7 @@ export async function deleteComponent(id, admin) { let component = await getComponentById(id); component = await verifyOwnership( admin, component, component.domain, ActionTypes.DELETE, RouterTypes.COMPONENT); - return component.remove(); + return component.deleteOne(); } export async function generateApiKey(id, admin) { diff --git a/src/services/config-strategy.js b/src/services/config-strategy.js index 1110316..ad21f6b 100644 --- a/src/services/config-strategy.js +++ b/src/services/config-strategy.js @@ -65,8 +65,8 @@ export async function createStrategy(args, admin) { export async function deleteStrategy(id, admin) { let configStrategy = await getStrategyById(id); configStrategy = await verifyOwnership(admin, configStrategy, configStrategy.domain, ActionTypes.DELETE, RouterTypes.STRATEGY); - - await configStrategy.remove(); + + await configStrategy.deleteOne(); updateDomainVersion(configStrategy.domain); return configStrategy; diff --git a/src/services/config.js b/src/services/config.js index b2ad347..a6c98da 100644 --- a/src/services/config.js +++ b/src/services/config.js @@ -86,7 +86,7 @@ export async function deleteConfig(id, admin) { let config = await getConfigById(id); config = await verifyOwnership(admin, config, config.domain, ActionTypes.DELETE, RouterTypes.CONFIG); - await config.remove(); + await config.deleteOne(); updateDomainVersion(config.domain); return config; @@ -224,7 +224,7 @@ export async function removeComponent(id, args, admin) { export async function updateComponent(id, args, admin) { const config = await verifyAddComponentInput(id, admin); - const componentIds = args.components.map(component => mongoose.Types.ObjectId(component)); + const componentIds = args.components.map(component => new mongoose.Types.ObjectId(component)); const components = await getComponents({ _id: { $in: componentIds } }); if (components.length != args.components.length) { diff --git a/src/services/domain.js b/src/services/domain.js index 0479216..887ec5c 100644 --- a/src/services/domain.js +++ b/src/services/domain.js @@ -66,7 +66,7 @@ export async function deleteDomainHistory(id, admin) { export async function deleteDomain(id, admin) { let domain = await getDomainById(id); domain = await verifyOwnership(admin, domain, domain._id, ActionTypes.DELETE, RouterTypes.DOMAIN); - return domain.remove(); + return domain.deleteOne(); } export async function transferDomain(args, admin) { diff --git a/src/services/environment.js b/src/services/environment.js index 4addc7a..e9f199b 100644 --- a/src/services/environment.js +++ b/src/services/environment.js @@ -84,7 +84,7 @@ export async function deleteEnvironment(id, admin) { environment = await verifyOwnership(admin, environment, environment.domain, ActionTypes.DELETE, RouterTypes.ENVIRONMENT); await removeEnvironmentFromElements(environment); - return environment.remove(); + return environment.deleteOne(); } export async function recoverEnvironment(id, admin) { diff --git a/src/services/group-config.js b/src/services/group-config.js index b48a469..07fbc22 100644 --- a/src/services/group-config.js +++ b/src/services/group-config.js @@ -72,7 +72,7 @@ export async function deleteGroup(id, admin) { let groupconfig = await getGroupConfigById(id); groupconfig = await verifyOwnership(admin, groupconfig, groupconfig.domain, ActionTypes.DELETE, RouterTypes.GROUP); - await groupconfig.remove(); + await groupconfig.deleteOne(); updateDomainVersion(groupconfig.domain); return groupconfig; diff --git a/src/services/permission.js b/src/services/permission.js index b9941e5..e06ac6d 100644 --- a/src/services/permission.js +++ b/src/services/permission.js @@ -72,7 +72,7 @@ export async function deletePermission(id, admin) { team.save(); }); - return permission.remove(); + return permission.deleteOne(); } export async function addValue(args, id, admin) { diff --git a/src/services/slack.js b/src/services/slack.js index 47ea109..0f564a1 100644 --- a/src/services/slack.js +++ b/src/services/slack.js @@ -48,7 +48,7 @@ async function deleteSlackInstallation(slack) { await domain.save(); } - return slack.remove(); + return slack.deleteOne(); } export async function getSlackOrError(where) { diff --git a/src/services/team.js b/src/services/team.js index 3991bbf..9b81f80 100644 --- a/src/services/team.js +++ b/src/services/team.js @@ -106,7 +106,7 @@ export async function updateTeam(args, id, admin) { export async function deleteTeam(id, admin) { const team = await verifyRequestedTeam(id, admin, ActionTypes.DELETE); - return team.remove(); + return team.deleteOne(); } export async function inviteMember(id, email, admin) { @@ -136,9 +136,9 @@ export async function acceptInvite(request_id, admin) { if (team.length) { await addMemberToTeam(admin, team[0]); - teamInvite.remove(); + teamInvite.deleteOne(); } else { - await teamInvite.remove(); + await teamInvite.deleteOne(); throw new BadRequestError('Team does not exist anymore'); } @@ -149,7 +149,7 @@ export async function removeInvite(request_id, id, admin) { await verifyRequestedTeam(id, admin, ActionTypes.UPDATE); const teamInvite = await getTeamInviteById(request_id); - return teamInvite.remove(); + return teamInvite.deleteOne(); } export async function addTeamMember(member, id, admin) { diff --git a/tests/config.test.js b/tests/config.test.js index 8c1ea9c..4cc953d 100644 --- a/tests/config.test.js +++ b/tests/config.test.js @@ -546,6 +546,7 @@ describe('Testing Environment status change', () => { describe('Testing component association', () => { beforeAll(async () => { + await setupDatabase(); await request(app) .post('/component/create') .set('Authorization', `Bearer ${adminMasterAccountToken}`)