From 22cedfcb519bfbd9efaefefe6f424fff04eed885 Mon Sep 17 00:00:00 2001 From: aaronshiel Date: Wed, 7 Feb 2024 22:22:48 -0800 Subject: [PATCH] check if locked to subjects properly --- node/src/gql/mutation/me/mentor-subjects-update.ts | 14 +++++++++++--- node/test/fixtures/mongodb/data-default.js | 1 + .../mutation/me/mentor-update-subjects.spec.ts | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/node/src/gql/mutation/me/mentor-subjects-update.ts b/node/src/gql/mutation/me/mentor-subjects-update.ts index 8da2e4b..f22bf24 100644 --- a/node/src/gql/mutation/me/mentor-subjects-update.ts +++ b/node/src/gql/mutation/me/mentor-subjects-update.ts @@ -12,7 +12,10 @@ import { GraphQLInputObjectType, GraphQLList, } from 'graphql'; -import { Mentor as MentorModel } from '../../../models'; +import { + Mentor as MentorModel, + MentorConfig as MentorConfigModel, +} from '../../../models'; import { Subject } from '../../../models/Subject'; import { User } from '../../../models/User'; import { canEditMentor } from '../../../utils/check-permissions'; @@ -55,8 +58,13 @@ export const updateMentorSubjects = { if (!(await canEditMentor(mentor, context.user))) { throw new Error('you do not have permission to edit this mentor'); } - if (mentor.mentorConfig && mentor.lockedToConfig) { - throw new Error('Mentor is locked, please unlock them before editing'); + const mentorConfig = mentor.mentorConfig + ? await MentorConfigModel.findById(mentor.mentorConfig) + : null; + if (mentorConfig && mentorConfig.lockedToSubjects) { + throw new Error( + 'Mentor subjects are locked, please unlock them before editing' + ); } const updated = await MentorModel.findByIdAndUpdate( mentor._id, diff --git a/node/test/fixtures/mongodb/data-default.js b/node/test/fixtures/mongodb/data-default.js index dcd587c..b79969b 100644 --- a/node/test/fixtures/mongodb/data-default.js +++ b/node/test/fixtures/mongodb/data-default.js @@ -16,6 +16,7 @@ module.exports = { subjects: [ '5ffdf41a1ee2c62320b49eb3', //STEM ], + lockedToSubjects: true, publiclyVisible: true, mentorType: 'CHAT', orgPermissions: [ diff --git a/node/test/graphql/mutation/me/mentor-update-subjects.spec.ts b/node/test/graphql/mutation/me/mentor-update-subjects.spec.ts index 6cb5811..2e351d1 100644 --- a/node/test/graphql/mutation/me/mentor-update-subjects.spec.ts +++ b/node/test/graphql/mutation/me/mentor-update-subjects.spec.ts @@ -100,7 +100,7 @@ describe('updateMentorSubjects', () => { }); expect(response.body).to.have.deep.nested.property( 'errors[0].message', - 'Mentor is locked, please unlock them before editing' + 'Mentor subjects are locked, please unlock them before editing' ); });