Skip to content

Commit

Permalink
Fix : change route name
Browse files Browse the repository at this point in the history
  • Loading branch information
FLoreauIGN committed May 7, 2024
1 parent ba2a17c commit 2cfb46e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 64 deletions.
126 changes: 63 additions & 63 deletions lib/api/district/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 fetch from '../../util/fetch.cjs'
import {getDistrict, getDistrictsFromCog, deleteDistrict} from './models.js'
import {formatDistrict} from './utils.js'

Expand Down Expand Up @@ -100,6 +99,69 @@ app.route('/')
res.send(response)
})

app.route('/postal-codes-from-datanova-url', auth)
.put(async (req, res) => {
let response
try {
// On February 2024 the postal file url is :
// https://datanova.laposte.fr/data-fair/api/v1/datasets/laposte-hexasmal/raw
const {url} = req.query
const statusID = nanoid()

await apiQueue.add(
{dataType: 'district', jobType: 'updatePostalCodeFromUrl', data: url, 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},
}
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})

app.route('/postal-codes-from-datanova', auth)
.put(express.text(), async (req, res) => {
let response
try {
const postalFile = req.body
const statusID = nanoid()

await apiQueue.add(
{dataType: 'district', jobType: 'updatePostalCode', data: postalFile, 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},
}
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})

app.route('/:districtID')
.get(analyticsMiddleware, async (req, res) => {
let response
Expand Down Expand Up @@ -220,66 +282,4 @@ app.get('/cog/:cog', analyticsMiddleware, async (req, res) => {
res.send(response)
})

app.route('/codePostal', auth)
.get(async (req, res) => {
let response
try {
// On February 2024 the postal file url is :
// https://datanova.laposte.fr/data-fair/api/v1/datasets/laposte-hexasmal/raw
const {url} = req.query
const postalFile = await fetch(url)
const statusID = nanoid()

await apiQueue.add(
{dataType: 'district', jobType: 'updatePostalCode', data: postalFile, 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},
}
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})
.post(express.text(), async (req, res) => {
let response
try {
const postalFile = req.body
const statusID = nanoid()

await apiQueue.add(
{dataType: 'district', jobType: 'updatePostalCode', data: postalFile, 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},
}
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})

export default app
9 changes: 8 additions & 1 deletion lib/api/district/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Papa from 'papaparse'
import fetch from '../../util/fetch.cjs'
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema} from '../helper.js'
import {banID} from '../schema.js'
import {getDistricts, getDistrictsFromCogList} from './models.js'
Expand Down Expand Up @@ -100,7 +101,7 @@ export async function formatDataNova(postalFile) {
(acc, district) => ({
...acc,
...(district?.meta?.insee?.cog
? {[district.meta.insee.cog]: [...acc[district.meta.insee.cog], district.id]}
? {[district.meta.insee.cog]: [...(acc[district.meta.insee.cog] || []), district.id]}
: {}
),
}), {})
Expand Down Expand Up @@ -136,3 +137,9 @@ export async function formatDataNova(postalFile) {
}
}))
}

export async function formatDataNovaFromUrl(url) {
const postalFileResponse = await fetch(url)
const postalFile = await postalFileResponse.text()
return formatDataNova(postalFile)
}

0 comments on commit 2cfb46e

Please sign in to comment.