diff --git a/package.json b/package.json index df567782..33251cb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.1.26", + "version": "1.1.27", "main": "index.ts", "license": "UNLICENSED", "scripts": { diff --git a/src/models/project.ts b/src/models/project.ts index fb10cbb1..d6b7350a 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -426,15 +426,20 @@ export default class ProjectModel extends AbstractModel impleme /** * Toggles enabled state of the notifications rule * @param ruleId - rule id to update + * @param status - new isEnabled status of the rule */ - public async toggleNotificationsRuleEnabledState(ruleId: string): Promise { + public async toggleNotificationsRuleEnabledState(ruleId: string, status?: boolean): Promise { const rule = this.notifications.find(_rule => _rule._id.toString() === ruleId); if (!rule) { return null; } - rule.isEnabled = !rule.isEnabled; + if (status !== undefined) { + rule.isEnabled = status; + } else { + rule.isEnabled = !rule.isEnabled; + } const result = await this.collection.findOneAndUpdate( { diff --git a/src/resolvers/projectNotifications.ts b/src/resolvers/projectNotifications.ts index d9d67bc4..456bdece 100644 --- a/src/resolvers/projectNotifications.ts +++ b/src/resolvers/projectNotifications.ts @@ -242,5 +242,25 @@ export default { return project.toggleNotificationsRuleEnabledState(input.ruleId); }, + + /** + * Unsubscribes from notifications by disabling the rule + * @param _obj - parent object + * @param factories - factories for working with models + * @param input - input data for unsubscribing + */ + async unsubscribeFromNotifications( + _obj: undefined, + { input }: { input: ProjectNotificationsRulePointer }, + { factories }: ResolverContextWithUser + ): Promise { + const project = await factories.projectsFactory.findById(input.projectId); + + if (!project) { + throw new ApolloError('No project with such id'); + } + + return project.toggleNotificationsRuleEnabledState(input.ruleId, false); + }, }, }; diff --git a/src/typeDefs/projectNotificationsMutations.ts b/src/typeDefs/projectNotificationsMutations.ts index 61efca08..14750e07 100644 --- a/src/typeDefs/projectNotificationsMutations.ts +++ b/src/typeDefs/projectNotificationsMutations.ts @@ -143,5 +143,13 @@ export default gql` "Data for toggling" input: ProjectNotificationRulePointer ): ProjectNotificationsRule @requireAdmin + + """ + Unsubscribes from notifications by disabling the rule + """ + unsubscribeFromNotifications( + "Data for unsubscribing" + input: ProjectNotificationRulePointer! + ): ProjectNotificationsRule } `;