Skip to content

Commit

Permalink
refactor(apple):reduce the number of branches of code in [passTypeIde…
Browse files Browse the repository at this point in the history
…ntifier].ts (nation3#76)
  • Loading branch information
aahna-ashina committed Oct 17, 2022
1 parent e55ff6b commit ecf3d0c
Showing 1 changed file with 102 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,79 +28,115 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
console.log('passesUpdatedSince:', 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 {
// Lookup the latest update and its timestamp
supabase
.from('latest_updates')
.select('*')
.order('time', { ascending: false })
.limit(1)
.single()
.then((latest_updates_result: any) => {
console.log('latest_updates_result:', latest_updates_result)
if (latest_updates_result.error) {
res.status(500).json({
error: 'Internal Server Error: ' + latest_updates_result.error.message
})
} else {
// Convert from ISO string ('2022-09-30T12:12:17') to Date
const latestUpdateDate: Date = new Date(latest_updates_result.data['time'])
console.log('latestUpdateDate:', latestUpdateDate)

if (!passesUpdatedSince) {
// The passes on this device have not been updated previously, so return all passes.
res.status(200).json({
serialNumbers: serialNumbers,
lastUpdated: String(DateUtils.getTimeInSeconds(latestUpdateDate))
})
} else {
// The passes on this device have been updated previously, so only return passes that
// were updated before the most recent Nation3 update in the `latest_updates` database table.
lookupSerialNumbersFromDatabase(deviceLibraryIdentifier)
.then((serialNumbers: string[]) => {
console.log('then, serialNumbers:', serialNumbers)
if (serialNumbers.length == 0) {
// There are no matching passes
res.status(204).end()
} else {
// Lookup the latest update and its timestamp
lookupTimeOfLatestUpdate()
.then((latestUpdateDate: Date) => {
console.log('then, latestUpdateDate:', latestUpdateDate)
if (!passesUpdatedSince) {
// The passes on this device have not been updated previously, so return all passes.
res.status(200).json({
serialNumbers: serialNumbers,
lastUpdated: String(DateUtils.getTimeInSeconds(latestUpdateDate))
})
} else {
// The passes on this device have been updated previously, so only return passes that
// were updated before the most recent Nation3 update in the `latest_updates` database table.

// Convert from epoch timestamp string ('1662889385') to Date
const passesUpdatedSinceDate: Date = DateUtils.getDate(Number(passesUpdatedSince))
console.log('passesUpdatedSinceDate:', passesUpdatedSinceDate)

if (passesUpdatedSinceDate.getTime() < latestUpdateDate.getTime()) {
res.status(200).json({
serialNumbers: serialNumbers,
lastUpdated: String(DateUtils.getTimeInSeconds(latestUpdateDate))
})
} else {
res.status(204).end()
}
}
}
// Convert from epoch timestamp string ('1662889385') to Date
const passesUpdatedSinceDate: Date = DateUtils.getDate(Number(passesUpdatedSince))
console.log('passesUpdatedSinceDate:', passesUpdatedSinceDate)

if (passesUpdatedSinceDate.getTime() < latestUpdateDate.getTime()) {
res.status(200).json({
serialNumbers: serialNumbers,
lastUpdated: String(DateUtils.getTimeInSeconds(latestUpdateDate))
})
}
}
} else {
res.status(204).end()
}
}
})
.catch((error: any) => {
console.log('catch, error:', error)
res.status(500).json({
error: 'Internal Server Error: ' + error.message
})
})
}
})
.catch((error: any) => {
console.log('catch, error:', error)
res.status(500).json({
error: 'Internal Server Error: ' + error.message
})
})
} catch (err: any) {
console.error('[passTypeIdentifier].ts err:\n', err)
res.status(400).json({
error: 'Request Not Authorized: ' + err.message
})
}
}

/**
* Makes a database query for fetching the serial numbers of passes stored in a device.
*
* @param deviceLibraryIdentifier The identifier of the iOS device stored during pass registration
* @returns A Promise with a string array of serial numbers
*/
const lookupSerialNumbersFromDatabase = async (deviceLibraryIdentifier: any): Promise<string[]> => {
console.log('lookupSerialNumbersFromDatabase')

const { data, error } = await supabase.from('registrations').select('serial_number').eq('device_library_identifier', deviceLibraryIdentifier)
console.log('data:', data)
console.log('error:', error)

const promise: Promise<string[]> = new Promise((resolve, reject) => {
if (error) {
reject(error)
} else {
// Convert from [{serial_number:333},{serial_number:444}] to ["333","444"]
let serialNumbers : string[] = []
for (const index in data) {
const serialNumber : string = data[index]['serial_number']
serialNumbers[Number(index)] = String(serialNumber)
}

resolve(serialNumbers)
}
})
return promise
}

/**
* Makes a database query for fetching the most recent update, and then returns its timestamp (Date).
*
* @returns A Promise with a Date
*/
const lookupTimeOfLatestUpdate = async (): Promise<Date> => {
console.log('lookupTimeOfLatestUpdate')

const { data, error } = await supabase.from('latest_updates').select('*').order('time', { ascending: false }).limit(1).single()
console.log('data:', data)
console.log('error:', error)

const promise: Promise<Date> = new Promise((resolve, reject) => {
if (error) {
reject(error)
} else {
// Convert from ISO string ('2022-09-30T12:12:17') to Date
const latestUpdateDate: Date = new Date(data['time'])
console.log('latestUpdateDate:', latestUpdateDate)

resolve(latestUpdateDate)
}
})
return promise
}

0 comments on commit ecf3d0c

Please sign in to comment.