|
| 1 | +/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */ |
| 2 | +/* eslint no-param-reassign: 0 */ |
| 3 | + |
| 4 | +import _ from 'lodash'; |
| 5 | +import Activity from './Activity'; |
| 6 | + |
| 7 | +export default { |
| 8 | + Query: { |
| 9 | + notificationSettings: async (parent, args, { user }) => { |
| 10 | + if (!user) { |
| 11 | + throw new Error('no Auth'); |
| 12 | + } |
| 13 | + return user.notificationSettings; |
| 14 | + }, |
| 15 | + }, |
| 16 | + |
| 17 | + Mutation: { |
| 18 | + addToken: async (parent, { token, os }, { user }) => { |
| 19 | + if (!user) { |
| 20 | + throw new Error('no Auth'); |
| 21 | + } |
| 22 | + if (!user.pushTokens.some(t => t.token === token)) { |
| 23 | + user.pushTokens.push({ token, os }); |
| 24 | + user.save(); |
| 25 | + } |
| 26 | + return { |
| 27 | + succeeded: true, |
| 28 | + }; |
| 29 | + }, |
| 30 | + |
| 31 | + updateNotificationSettings: async ( |
| 32 | + parent, |
| 33 | + { |
| 34 | + enabled, disableUntil, procedures, tags, newVote, newPreperation, |
| 35 | + }, |
| 36 | + { user }, |
| 37 | + ) => { |
| 38 | + if (!user) { |
| 39 | + throw new Error('no Auth'); |
| 40 | + } |
| 41 | + user.notificationSettings = { |
| 42 | + ...user.notificationSettings, |
| 43 | + ..._.omitBy( |
| 44 | + { |
| 45 | + enabled, |
| 46 | + disableUntil, |
| 47 | + procedures, |
| 48 | + tags, |
| 49 | + newVote, |
| 50 | + newPreperation, |
| 51 | + }, |
| 52 | + _.isNil, |
| 53 | + ), |
| 54 | + }; |
| 55 | + await user.save(); |
| 56 | + return user.notificationSettings; |
| 57 | + }, |
| 58 | + |
| 59 | + toggleNotification: async ( |
| 60 | + parent, |
| 61 | + { procedureId }, |
| 62 | + { user, ProcedureModel, ActivityModel }, |
| 63 | + ) => { |
| 64 | + if (!user) { |
| 65 | + throw new Error('no Auth'); |
| 66 | + } |
| 67 | + const procedure = await ProcedureModel.findOne({ procedureId }); |
| 68 | + await Activity.Mutation.increaseActivity( |
| 69 | + parent, |
| 70 | + { procedureId }, |
| 71 | + { ProcedureModel, ActivityModel, user }, |
| 72 | + ); |
| 73 | + |
| 74 | + const index = user.notificationSettings.procedures.indexOf(procedure._id); |
| 75 | + let notify; |
| 76 | + if (index > -1) { |
| 77 | + notify = false; |
| 78 | + user.notificationSettings.procedures.splice(index, 1); |
| 79 | + } else { |
| 80 | + notify = true; |
| 81 | + user.notificationSettings.procedures.push(procedure._id); |
| 82 | + } |
| 83 | + await user.save(); |
| 84 | + return { ...procedure.toObject(), notify }; |
| 85 | + }, |
| 86 | + }, |
| 87 | +}; |
0 commit comments