Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved api response format and homogeneity #500

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 50 additions & 118 deletions lib/api/address/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import express from 'express'
import queue from '../../util/queue.cjs'
import auth from '../../middleware/auth.js'
import analyticsMiddleware from '../../middleware/analytics.js'
import {handleAPIResponse} from '../helper.js'
import {getAddress, deleteAddress} from './models.js'
import {getDeltaReport, formatAddress} from './utils.js'

Expand All @@ -18,207 +19,138 @@ const app = new express.Router()

app.route('/')
.post(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
const statusID = nanoid()
if (!Array.isArray(addresses)) {
handleAPIResponse(res, 400, 'Wrong request format', {})
return
}

const statusID = nanoid()
await apiQueue.add(
{dataType: 'address', jobType: 'insert', data: addresses, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})
.put(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
const statusID = nanoid()
if (!Array.isArray(addresses)) {
handleAPIResponse(res, 400, 'Wrong request format', {})
return
}

const statusID = nanoid()
await apiQueue.add(
{dataType: 'address', jobType: 'update', data: addresses, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})
.patch(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
const statusID = nanoid()
if (!Array.isArray(addresses)) {
handleAPIResponse(res, 400, 'Wrong request format', {})
return
}

const statusID = nanoid()
await apiQueue.add(
{dataType: 'address', jobType: 'patch', data: addresses, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})

app.route('/:addressID')
.get(analyticsMiddleware, async (req, res) => {
let response
try {
const {addressID} = req.params
const address = await getAddress(addressID)
if (!addressID) {
handleAPIResponse(res, 400, 'Wrong request format', {})
return
}

const address = await getAddress(addressID)
if (!address) {
res.status(404).send('Request ID unknown')
handleAPIResponse(res, 404, 'Request ID unknown', {})
return
}

const addressFormatted = formatAddress(address)

response = {
date: new Date(),
status: 'success',
response: addressFormatted,
}
handleAPIResponse(res, 200, 'Address successfully retrieved', addressFormatted)
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})
.delete(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const {addressID} = req.params
const address = await getAddress(addressID)

if (!address) {
res.status(404).send('Request ID unknown')
handleAPIResponse(res, 404, 'Request ID unknown', {})
return
}

await deleteAddress(addressID)
response = {
date: new Date(),
status: 'success',
response: {},
}
handleAPIResponse(res, 200, 'Address successfully deleted', {})
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})

app.post('/delete', auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addressIDs = req.body
if (!Array.isArray(addressIDs)) {
handleAPIResponse(res, 400, 'Wrong request format', {})
return
}

const statusID = nanoid()

await apiQueue.add(
{dataType: 'address', jobType: 'delete', data: addressIDs, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})

app.post('/delta-report', auth, analyticsMiddleware, async (req, res) => {
let response
try {
const {data, districtID} = req.body

if (!data || !districtID) {
res.status(404).send('Wrong request format')
handleAPIResponse(res, 400, 'Wrong request format', {})
return
}

const deltaReport = await getDeltaReport(data, districtID)
response = {
date: new Date(),
status: 'success',
response: deltaReport,
}
handleAPIResponse(res, 200, 'Delta report successfully generated', deltaReport)
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})

export default app
28 changes: 6 additions & 22 deletions lib/api/ban-id/routes.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import
import express from 'express'
import analyticsMiddleware from '../../middleware/analytics.js'

import {handleAPIResponse} from '../helper.js'
import {getUuids, uncollidUuids} from './helpers.js'

const app = new express.Router()

app.get('/', analyticsMiddleware, async (req, res) => {
let response
try {
const length = Number(req.query.quantity) || 1
if (length > 100_000) {
res.status(400).send({
date: new Date(),
status: 'error',
message: 'Quantity must be less than 100 000',
response: {},
})
handleAPIResponse(res, 400, 'Quantity must be less than 100 000', {})
return
}

const ids = await uncollidUuids(getUuids(length))

response = {
date: new Date(),
status: 'success',
response: ids,
}
handleAPIResponse(res, 200, 'Successfully generated UUIDs', ids)
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}

res.send(response)
})

export default app
31 changes: 17 additions & 14 deletions lib/api/certificate/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import
import express from 'express'
import auth from '../../middleware/auth.js'
import {handleAPIResponse} from '../helper.js'
import {
getCertificate,
setCertificate,
Expand All @@ -15,43 +16,45 @@ app.get('/:id', async (req, res) => {
const {id} = req.params
try {
const certificate = await getCertificate(id)
if (certificate) {
res.status(200).json(certificate)
} else {
res.status(404).json({message: 'Certificate not found'})
if (!certificate) {
handleAPIResponse(res, 404, 'Certificate not found', {})
return
}

handleAPIResponse(res, 200, 'Certificate retrieved', certificate)
} catch (error) {
console.error(`Error retrieving certificate: ${error.message}`)
res.status(500).json({message: 'Internal server error'})
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}
})

app.post('/', auth, async (req, res) => {
try {
const {addressID} = req.body

if (!addressID) {
return res.status(400).json({message: 'addressID is required'})
handleAPIResponse(res, 400, 'addressID is required', {})
return
}

const data = await getDataForCertificate(addressID)

if (!data) {
return res.status(400).json({message: 'Address is not certified, not active, or has no parcels.'})
handleAPIResponse(res, 403, 'Address is not certified, not active, or has no parcels.', {})
return
}

const {districtConfig} = data
if (!districtConfig.certificate) {
return res.status(400).json({message: 'District has not activated the certificate config.'})
handleAPIResponse(res, 403, 'District has not activated the certificate config.', {})
return
}

const certificate = await formatDataForCertificate(data)
const newCertificate = await setCertificate(certificate)

res.status(201).json(newCertificate)
handleAPIResponse(res, 201, 'Certificate created', newCertificate)
} catch (error) {
console.error(`Error creating certificate: ${error.message}`)
res.status(500).json({message: 'Internal server error'})
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
}
})

Expand Down
Loading
Loading