@@ -7,6 +7,87 @@ import gcm from 'node-gcm';
7
7
import apnProvider from './apn' ;
8
8
import gcmProvider from './gcm' ;
9
9
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 } ;
10
91
11
92
export default async ( { message, user } ) => {
12
93
let userId ;
0 commit comments