-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get the list of updatable passes (#6)
Return `serialNumbers`
- Loading branch information
1 parent
9949666
commit 9b71e6e
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
describe('Get the List of Updatable Passes', () => { | ||
|
||
it('error when wrong request method (POST instead of GET)', () => { | ||
cy.request({ | ||
method: 'POST', | ||
url: '/api/apple/v1/devices/b33e3a3dccb3030333e3333da33333a3/registrations/pass.org.passport.nation3', | ||
failOnStatusCode: false | ||
}).then((response) => { | ||
expect(response.status).to.eq(400) | ||
expect(JSON.stringify(response.body)).to.contain('Request Not Authorized: Wrong request method') | ||
}) | ||
}) | ||
|
||
it('error when missing passesUpdatedSince parameter', () => { | ||
cy.request({ | ||
method: 'GET', | ||
url: '/api/apple/v1/devices/b33e3a3dccb3030333e3333da33333a3/registrations/pass.org.passport.nation3', | ||
failOnStatusCode: false | ||
}).then((response) => { | ||
expect(response.status).to.eq(400) | ||
expect(JSON.stringify(response.body)).to.contain('Missing/empty parameter: passesUpdatedSince') | ||
}) | ||
}) | ||
|
||
it('204 when unknown deviceLibraryIdentifier', () => { | ||
cy.request({ | ||
method: 'GET', | ||
url: '/api/apple/v1/devices/b00e3a3dccb3030333e3333da33333a3/registrations/pass.org.passport.nation3?passesUpdatedSince={previousLastUpdated}', | ||
failOnStatusCode: false | ||
}).then((response) => { | ||
expect(response.status).to.eq(204) | ||
}) | ||
}) | ||
|
||
it('200 when existing deviceLibraryIdentifier', () => { | ||
cy.request({ | ||
method: 'GET', | ||
url: '/api/apple/v1/devices/b33e3a3dccb3030333e3333da33333a3/registrations/pass.org.passport.nation3?passesUpdatedSince={previousLastUpdated}', | ||
failOnStatusCode: false | ||
}).then((response) => { | ||
expect(response.status).to.eq(200) | ||
expect(JSON.stringify(response.body)).to.contain('serialNumbers') | ||
expect(JSON.stringify(response.body)).to.contain('lastUpdated') | ||
// TODO: verify serial number values | ||
}) | ||
}) | ||
}) | ||
|
||
export {} |
71 changes: 71 additions & 0 deletions
71
...ages/api/apple/v1/devices/[deviceLibraryIdentifier]/registrations/[passTypeIdentifier].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { supabase } from '../../../../../../../utils/SupabaseClient' | ||
|
||
/** | ||
* Get the List of Updatable Passes. Implementation of | ||
* https://developer.apple.com/documentation/walletpasses/get_the_list_of_updatable_passes | ||
*/ | ||
export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
console.log('[passTypeIdentifier].ts') | ||
|
||
// Expected URL format: | ||
// /api/apple/v1/devices/[deviceLibraryIdentifier]/registrations/[passTypeIdentifier]?passesUpdatedSince=[passesUpdatedSince] | ||
// /api/apple/v1/devices/b33e3a3dccb3030333e3333da33333a3/registrations/pass.org.passport.nation3?passesUpdatedSince=0 | ||
console.log('req.url:', req.url) | ||
|
||
try { | ||
// Expected request method: GET | ||
console.log('req.method:', req.method) | ||
if (req.method != 'GET') { | ||
throw new Error('Wrong request method: ' + req.method) | ||
} | ||
|
||
// Extract deviceLibraryIdentifier and passesUpdatedSince from the request query | ||
console.log('req.query:', req.query) | ||
const { deviceLibraryIdentifier, passesUpdatedSince } = req.query | ||
console.log('deviceLibraryIdentifier:', deviceLibraryIdentifier) | ||
console.log('passesUpdatedSince:', passesUpdatedSince) | ||
|
||
// Validate the passesUpdatedSince parameter | ||
if (!passesUpdatedSince || String(passesUpdatedSince).trim().length == 0) { | ||
throw new Error('Missing/empty parameter: passesUpdatedSince') | ||
} | ||
|
||
// Lookup the serial numbers for the given device | ||
supabase | ||
.from('registrations') | ||
.select('serial_number').eq('device_library_identifier', deviceLibraryIdentifier) | ||
.then((result: any) => { | ||
console.log('result:', result) | ||
if (result.error) { | ||
res.status(500).json({ | ||
error: 'Internal Server Error: ' + result.error.message | ||
}) | ||
} else { | ||
// Convert from [{serial_number:333},{serial_number:444}] to ["333","444"] | ||
let serialNumbers : string[] = [] | ||
for (const index in result.data) { | ||
const serialNumber : string = result.data[index]['serial_number'] | ||
serialNumbers[Number(index)] = String(serialNumber) | ||
} | ||
console.log('serialNumbers:\n', serialNumbers) | ||
|
||
if (serialNumbers.length == 0) { | ||
// There are no matching passes | ||
res.status(204).end() | ||
} else { | ||
// Return matching passes (serial numbers) | ||
res.status(200).json({ | ||
serialNumbers: serialNumbers, | ||
lastUpdated: 0 // TODO | ||
}) | ||
} | ||
} | ||
}) | ||
} catch (err: any) { | ||
console.error('[passTypeIdentifier].ts err:\n', err) | ||
res.status(400).json({ | ||
error: 'Request Not Authorized: ' + err.message | ||
}) | ||
} | ||
} |