Skip to content

Commit 31e1009

Browse files
authored
Merge pull request #41 from demokratie-live/sprint#7/push-notifications
Sprint#7/push notifications
2 parents 57171b7 + 5e41eb1 commit 31e1009

File tree

20 files changed

+428
-12
lines changed

20 files changed

+428
-12
lines changed

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SECRET_KEY=
2+
AUTH_JWT_SECRET=
3+
ENGINE_API_KEY=
4+
APPLE_TEAMID=
5+
APPLE_APN_KEY_ID=
6+
APPLE_APN_KEY=
7+
NOTIFICATION_ANDROID_SERVER_KEY=

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
*.env
33
*.log
4-
/dist/*
4+
/dist/*
5+
AuthKey_*.p8

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"serve": "node dist/index.js"
1414
},
1515
"dependencies": {
16+
"apn": "^2.2.0",
1617
"apollo-cache-inmemory": "^1.1.7",
1718
"apollo-client": "^2.2.3",
1819
"apollo-engine": "^0.8.8",
@@ -31,6 +32,7 @@
3132
"merge-graphql-schemas": "^1.3.0",
3233
"mongoose": "^5.0.3",
3334
"node-fetch": "^2.0.0",
35+
"node-gcm": "^0.14.10",
3436
"passport": "^0.4.0",
3537
"passport-jwt": "^4.0.0",
3638
"prettier-eslint": "^8.2.0",

src/config/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export default {
66
GRAPHIQL_PATH: '/graphiql',
77
GRAPHQL_PATH: '/graphql',
88
BUNDESTAGIO_SERVER_URL: process.env.BUNDESTAGIO_SERVER_URL || 'http://localhost:3100/graphql',
9+
NOTIFICATION_ANDROID_SERVER_KEY: process.env.NOTIFICATION_ANDROID_SERVER_KEY || false,
910
};

src/graphql/queries/getAllProcedures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default gql`
1010
currentStatus
1111
abstract
1212
tags
13+
bioUpdateAt
1314
subjectGroups
1415
history {
1516
assignment

src/graphql/queries/getProcedureUpdates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default gql`
44
query procedureUpdates($period: [Int!], $type: [String!]) {
55
procedureUpdates(period: $period, type: $type) {
66
procedureId
7-
updatedAt
7+
bioUpdateAt
88
}
99
}
1010
`;

src/graphql/queries/getProcedures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default gql`
1111
abstract
1212
tags
1313
subjectGroups
14+
bioUpdateAt
1415
history {
1516
assignment
1617
initiator

src/graphql/resolvers/Notification.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
};

src/graphql/resolvers/Procedure.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ export default {
123123
.then(finishedVotings => [...activeVotings, ...finishedVotings]);
124124
},
125125

126-
procedure: async (parent, { id }, { ProcedureModel }) =>
127-
ProcedureModel.findOne({ procedureId: id }),
126+
procedure: async (parent, { id }, { user, ProcedureModel }) => {
127+
const procedure = await ProcedureModel.findOne({ procedureId: id });
128+
return {
129+
...procedure.toObject(),
130+
notify: !!(user && user.notificationSettings.procedures.indexOf(procedure._id) > -1),
131+
};
132+
},
128133

129134
searchProcedures: (parent, { term }, { ProcedureModel }) =>
130135
ProcedureModel.find(
@@ -140,6 +145,20 @@ export default {
140145
},
141146
{ score: { $meta: 'textScore' } },
142147
).sort({ score: { $meta: 'textScore' } }),
148+
149+
notifiedProcedures: async (parent, args, { user, ProcedureModel }) => {
150+
if (!user) {
151+
throw new Error('no Auth');
152+
}
153+
const procedures = await ProcedureModel.find({
154+
_id: { $in: user.notificationSettings.procedures },
155+
});
156+
157+
return procedures.map(procedure => ({
158+
...procedure.toObject(),
159+
notify: true,
160+
}));
161+
},
143162
},
144163

145164
Procedure: {

src/graphql/schemas/Notification.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default `
2+
3+
type TokenResult {
4+
succeeded: Boolean
5+
}
6+
7+
type NotificationSettings {
8+
enabled: Boolean
9+
newVote: Boolean
10+
newPreperation: Boolean
11+
disableUntil: Date
12+
procedures: [String]
13+
tags: [String]
14+
}
15+
16+
type Query {
17+
notificationSettings: NotificationSettings
18+
}
19+
20+
type Mutation {
21+
addToken(token: String!, os: String!): TokenResult
22+
23+
updateNotificationSettings(
24+
enabled: Boolean,
25+
newVote: Boolean,
26+
newPreperation: Boolean,
27+
disableUntil: Date,
28+
procedures: [String],
29+
tags: [String]
30+
): NotificationSettings
31+
32+
toggleNotification(procedureId: String!): Procedure
33+
}
34+
35+
`;

0 commit comments

Comments
 (0)