diff --git a/server/pages/api/pushLastUpdate.ts b/server/pages/api/pushLastUpdate.ts index 2aa0ae8..c735bc1 100644 --- a/server/pages/api/pushLastUpdate.ts +++ b/server/pages/api/pushLastUpdate.ts @@ -5,17 +5,17 @@ import { Passes } from '../../utils/Passes' // req = HTTP incoming message, res = HTTP server response export default function handler(req: NextApiRequest, res: NextApiResponse) { - console.log('/api/pushLastUpdate') + console.info('[pushLastUpdate.ts] handler') Passes.notifyPassesAboutLastUpdate(Platform.Apple) .then((result: any) => { - console.log('then, result:', result) + console.info('[pushLastUpdate.ts] then, result:', result) res.status(200).json( JSON.parse(result) ) }) .catch((result: any) => { - console.log('catch, result:', result) + console.error('[pushLastUpdate.ts] catch, result:', result) res.status(500).json({ error: 'Internal Server Error: ' + result }) diff --git a/server/utils/APNProvider.ts b/server/utils/APNProvider.ts index ab47905..daac99b 100644 --- a/server/utils/APNProvider.ts +++ b/server/utils/APNProvider.ts @@ -1,7 +1,22 @@ +import apn, { Responses } from 'apn' +import { config } from './Config'; + /** * Apple Push Notification (APN) provider. */ export class APNProvider { - + static async sendNotification(pushToken: string): Promise { + console.info('[APNProvider.ts] sendNotification') + + const apnProvider: apn.Provider = new apn.Provider({ + cert: `-----BEGIN CERTIFICATE-----\n${config.appleCertificatePEM}\n-----END CERTIFICATE-----`, + key: `-----BEGIN RSA PRIVATE KEY-----\n${config.appleCertificateKey}\n-----END RSA PRIVATE KEY-----`, + production: true + }) + const notification: apn.Notification = new apn.Notification(); + notification.topic = 'pass.org.passport.nation3' + console.info('[APNProvider.ts] Sending notification...') + return await apnProvider.send(notification, pushToken) + } } diff --git a/server/utils/Passes.ts b/server/utils/Passes.ts index ff6fcba..2aced12 100644 --- a/server/utils/Passes.ts +++ b/server/utils/Passes.ts @@ -6,8 +6,8 @@ import fs from 'fs' import AdmZip from 'adm-zip' import console from 'console' import { config } from './Config' -import apn from 'apn' import { supabase } from './SupabaseClient' +import { APNProvider } from './APNProvider' export class Passes { /** @@ -190,13 +190,13 @@ export class Passes { * @returns Promise */ static async notifyPassesAboutLastUpdate(platform: Platform): Promise { - console.log('notifyPassesAboutLastUpdate') + console.info('[Passes.ts] notifyPassesAboutLastUpdate') if (platform == Platform.Apple) { // Lookup the push tokens of registered passes const { data, error } = await supabase.from('distinct_push_token').select() - console.log('data:', data) - console.log('error:', error) + console.info('[Passes.ts] data:', data) + console.info('[Passes.ts] error:', error) if (error) { return new Promise((reject) => { @@ -207,20 +207,12 @@ export class Passes { let allFailed: any[] = [] for (let i = 0; i < data.length; i++) { const pushToken: string = data[i]['push_token'] - console.log('pushToken:', pushToken) + console.info('[Passes.ts] pushToken:', pushToken) // Send notification request to Apple Push Notification service (APNs) - const apnProvider: apn.Provider = new apn.Provider({ - cert: `-----BEGIN CERTIFICATE-----\n${config.appleCertificatePEM}\n-----END CERTIFICATE-----`, - key: `-----BEGIN RSA PRIVATE KEY-----\n${config.appleCertificateKey}\n-----END RSA PRIVATE KEY-----`, - production: true - }) - const notification: apn.Notification = new apn.Notification(); - notification.topic = 'pass.org.passport.nation3' - console.log('Sending notification...') - const { sent, failed } = await apnProvider.send(notification, pushToken) - console.log('sent:', sent) - console.log('failed:', failed) + const { sent, failed } = await APNProvider.sendNotification(pushToken) + console.info('[Passes.ts] sent:', sent) + console.info('[Passes.ts] failed:', failed) if (sent.length > 0) { allSent[allSent.length] = sent }