import { DeviceModel , UserModel , ProcedureModel , Device , VoteModel , PushNotificationModel , PUSH_TYPE , PUSH_CATEGORY , } from "../models" ; export const queuePushs = async ( { type , category , title , message , procedureIds , tokens , time = new Date ( ) , } ) => { const docs = tokens . map ( ( { token , os } ) => { return { type , category , title , message , procedureIds , token , os , time , } ; } ) ; await PushNotificationModel . insertMany ( docs ) ; return true ; } ; export const queuePushsOutcome = async ( procedureId ) => { const procedure = await ProcedureModel . findOne ( { procedureId } ) ; if ( ! procedure ) { console . error ( `[PUSH] Unknown Procedure ${ procedureId }` ) ; return ; } const devices = await DeviceModel . find ( { "notificationSettings.enabled" : true , "notificationSettings.outcomePushs" : true , "notificationSettings.procedures" : procedure . _id , pushTokens : { $gt : [ ] } , } ) ; for ( let i = 0 ; i < devices . length ; i += 1 ) { const device = devices [ i ] ; let voted = null ; const user = await UserModel . findOne ( { device : device . _id , verified : true , } ) ; if ( user ) { voted = await VoteModel . findOne ( { procedure : procedure . _id , type : "Phone" , voters : { $elemMatch : { voter : user . phone , } , } , } ) ; } const title = voted ? "Offizielles Ergebnis zu Deiner Abstimmung" : "Offizielles Ergebnis zur Abstimmung" ; const message = procedure . title ; await queuePushs ( { type : PUSH_TYPE . PROCEDURE , category : PUSH_CATEGORY . OUTCOME , title , message , procedureIds : [ procedureId ] , tokens : device . pushTokens , } ) ; } } ;