Skip to content

Commit

Permalink
refactor(apple): separate apn provider code (nation3#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
aahna-ashina committed Oct 25, 2022
1 parent 59c28e6 commit bbfa591
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
6 changes: 3 additions & 3 deletions server/pages/api/pushLastUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
17 changes: 16 additions & 1 deletion server/utils/APNProvider.ts
Original file line number Diff line number Diff line change
@@ -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<Responses> {
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)
}
}
24 changes: 8 additions & 16 deletions server/utils/Passes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -190,13 +190,13 @@ export class Passes {
* @returns Promise
*/
static async notifyPassesAboutLastUpdate(platform: Platform): Promise<undefined | string> {
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<string>((reject) => {
Expand All @@ -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
}
Expand Down

0 comments on commit bbfa591

Please sign in to comment.