Skip to content

Commit f33927c

Browse files
committed
fire Push notifications
1 parent cb3ebf8 commit f33927c

File tree

3 files changed

+119
-14
lines changed

3 files changed

+119
-14
lines changed

src/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import webhook from './scripts/webhook';
1616
// import importAll from './scripts/importAll';
1717

1818
import auth from './express/auth';
19-
import pushNotify from './services/notifications';
19+
// import pushNotify from './services/notifications';
2020

2121
// Models
2222
import ProcedureModel from './models/Procedure';
@@ -87,18 +87,18 @@ app.post('/webhooks/bundestagio/update', async (req, res) => {
8787
}
8888
});
8989

90-
app.get('/push-test', async (req, res) => {
91-
const { message } = req.query;
92-
const users = await UserModel.find();
93-
users.forEach((user) => {
94-
pushNotify({
95-
title: 'test',
96-
message: message || 'Test push notification to all users',
97-
user,
98-
});
99-
});
100-
res.send("push's send");
101-
});
90+
// app.get('/push-test', async (req, res) => {
91+
// const { message } = req.query;
92+
// const users = await UserModel.find();
93+
// users.forEach((user) => {
94+
// pushNotify({
95+
// title: 'test',
96+
// message: message || 'Test push notification to all users',
97+
// user,
98+
// });
99+
// });
100+
// res.send("push's send");
101+
// });
102102

103103
// Darf in Production nicht ausführbar sein!
104104
// app.get('/webhooks/bundestagio/import-all', importAll);

src/scripts/import.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import createClient from '../graphql/client';
44
import Procedure from '../models/Procedure';
55
import getProcedures from '../graphql/queries/getProcedures';
66

7+
import { procedureUpdate, newPreperation, newVote } from '../services/notifications/index';
8+
79
const deputiesNumber = {
810
8: 518,
911
9: 519,
@@ -67,6 +69,12 @@ export default async (procedureIds) => {
6769

6870
newBIoProcedure.submissionDate = newBIoProcedure.history[0].date;
6971
}
72+
73+
const oldProcedure = await Procedure.find(
74+
{ procedureId: newBIoProcedure.procedureId },
75+
{ _id: 1 },
76+
).limit(1);
77+
7078
return Procedure.findOneAndUpdate(
7179
{ procedureId: newBIoProcedure.procedureId },
7280
_(newBIoProcedure)
@@ -75,8 +83,24 @@ export default async (procedureIds) => {
7583
{
7684
upsert: true,
7785
},
78-
);
86+
).then(() => {
87+
/**
88+
* PUSH NOTIFICATIONS
89+
*/
90+
// New Procedures
91+
if (!oldProcedure.length) {
92+
newPreperation({ procedureId: newBIoProcedure.procedureId });
93+
} else {
94+
// Updated Procedures
95+
procedureUpdate({ procedureId: newBIoProcedure.procedureId });
96+
if (newBIoProcedure.currentStatus === 'Beschlussempfehlung liegt vor') {
97+
// moved to Vote Procedures
98+
newVote({ procedureId: newBIoProcedure.procedureId });
99+
}
100+
}
101+
});
79102
});
103+
80104
const result = await Promise.all(promises);
81105

82106
return result.length;

src/services/notifications/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,87 @@ import gcm from 'node-gcm';
77
import apnProvider from './apn';
88
import gcmProvider from './gcm';
99
import UserModel from '../../models/User';
10+
import ProcedureModel from '../../models/Procedure';
11+
12+
const sendNotifications = ({ tokenObjects, message }) => {
13+
const androidNotificationTokens = [];
14+
tokenObjects.forEach(({ token, os }) => {
15+
switch (os) {
16+
case 'ios':
17+
{
18+
const note = new apn.Notification();
19+
20+
note.alert = message;
21+
// note.payload = { messageFrom: 'John Appleseed' };
22+
note.topic = 'de.democracy-deutschland.clientapp';
23+
24+
apnProvider.send(note, token).then((result) => {
25+
console.log('apnProvider.send', result);
26+
});
27+
}
28+
break;
29+
30+
case 'android':
31+
// Prepare android notifications
32+
androidNotificationTokens.push(token);
33+
break;
34+
35+
default:
36+
break;
37+
}
38+
});
39+
// send bulk send android notifications
40+
if (androidNotificationTokens.length > 0) {
41+
const gcmMessage = new gcm.Message({
42+
notification: {
43+
body: message,
44+
},
45+
});
46+
gcmProvider.send(
47+
gcmMessage,
48+
{ registrationTokens: androidNotificationTokens },
49+
(err, response) => {
50+
if (err) console.error('gcmProvider', err);
51+
else console.log('gcmProvider', response);
52+
},
53+
);
54+
}
55+
};
56+
57+
const newVote = async ({ procedureId }) => {
58+
const procedure = await ProcedureModel.findOne({ procedureId });
59+
const users = await UserModel.find({
60+
'notificationSettings.enabled': true,
61+
'notificationSettings.newVote': true,
62+
});
63+
const tokenObjects = users.reduce((array, { pushTokens }) => [...array, ...pushTokens], []);
64+
sendNotifications({ tokenObjects, message: `Jetzt Abstimmen!\n${procedure.title}` });
65+
};
66+
// newVote({ procedureId: 231079 });
67+
68+
const newPreperation = async ({ procedureId }) => {
69+
const procedure = await ProcedureModel.findOne({ procedureId });
70+
const users = await UserModel.find({
71+
'notificationSettings.enabled': true,
72+
'notificationSettings.newPreperation': true,
73+
});
74+
const tokenObjects = users.reduce((array, { pushTokens }) => [...array, ...pushTokens], []);
75+
sendNotifications({ tokenObjects, message: `Neue Gesetzesinitiative!\n${procedure.title}` });
76+
};
77+
// newPreperation({ procedureId: 231079 });
78+
79+
const procedureUpdate = async ({ procedureId }) => {
80+
const procedure = await ProcedureModel.findOne({ procedureId });
81+
const users = await UserModel.find({
82+
'notificationSettings.enabled': true,
83+
'notificationSettings.procedures': procedure._id,
84+
});
85+
const tokenObjects = users.reduce((array, { pushTokens }) => [...array, ...pushTokens], []);
86+
sendNotifications({ tokenObjects, message: `Update!\n${procedure.title}` });
87+
};
88+
// procedureUpdate({ procedureId: 231079 });
89+
90+
export { procedureUpdate, newVote, newPreperation };
1091

1192
export default async ({ message, user }) => {
1293
let userId;

0 commit comments

Comments
 (0)