Skip to content

Commit

Permalink
Revert "Merge pull request #495 from BaseAdresseNationale/antoinelude…
Browse files Browse the repository at this point in the history
…au/clean-api-response"

This reverts commit aec1c6c, reversing
changes made to 7670269.
  • Loading branch information
antoineludeau committed Dec 10, 2024
1 parent aec1c6c commit 05fde3f
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 224 deletions.
168 changes: 118 additions & 50 deletions lib/api/address/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -19,138 +18,207 @@ const app = new express.Router()

app.route('/')
.post(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
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}
)
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})
.put(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
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}
)
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})
.patch(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
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}
)
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})

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

const address = await getAddress(addressID)

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

const addressFormatted = formatAddress(address)
handleAPIResponse(res, 200, 'Address successfully retrieved', addressFormatted)

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

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

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

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

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}
)
handleAPIResponse(res, 200, `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, {statusID})
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
console.error(error)
handleAPIResponse(res, 500, 'Internal server error', {})
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})

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

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

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

res.send(response)
})

export default app
28 changes: 22 additions & 6 deletions lib/api/ban-id/routes.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
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) {
handleAPIResponse(res, 400, 'Quantity must be less than 100 000', {})
res.status(400).send({
date: new Date(),
status: 'error',
message: 'Quantity must be less than 100 000',
response: {},
})
return
}

const ids = await uncollidUuids(getUuids(length))
handleAPIResponse(res, 200, 'Successfully generated UUIDs', ids)

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

res.send(response)
})

export default app
31 changes: 14 additions & 17 deletions lib/api/certificate/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -16,45 +15,43 @@ app.get('/:id', async (req, res) => {
const {id} = req.params
try {
const certificate = await getCertificate(id)
if (!certificate) {
handleAPIResponse(res, 404, 'Certificate not found', {})
return
if (certificate) {
res.status(200).json(certificate)
} else {
res.status(404).json({message: 'Certificate not found'})
}

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

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

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

const data = await getDataForCertificate(addressID)

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

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

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

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

Expand Down
Loading

0 comments on commit 05fde3f

Please sign in to comment.