Fix #90
This commit is contained in:
@@ -100,20 +100,25 @@ export default async function functionRoutes(server: FastifyInstance) {
|
||||
|
||||
server.get('/functions/check-zip/:zip', async (req, reply) => {
|
||||
const { zip } = req.params as { zip: string }
|
||||
const normalizedZip = String(zip || "").replace(/\D/g, "")
|
||||
|
||||
if (!zip) {
|
||||
return reply.code(400).send({ error: 'ZIP is required' })
|
||||
if (normalizedZip.length !== 5) {
|
||||
return reply.code(400).send({ error: 'ZIP must contain exactly 5 digits' })
|
||||
}
|
||||
|
||||
try {
|
||||
//@ts-ignore
|
||||
const data = await server.db.select().from(citys).where(eq(citys.zip,zip))
|
||||
const data = await server.db
|
||||
.select()
|
||||
.from(citys)
|
||||
.where(eq(citys.zip, Number(normalizedZip)))
|
||||
|
||||
|
||||
if (!data) {
|
||||
if (!data.length) {
|
||||
return reply.code(404).send({ error: 'ZIP not found' })
|
||||
}
|
||||
|
||||
const city = data[0]
|
||||
|
||||
//districtMap
|
||||
const bundeslaender = [
|
||||
{ code: 'DE-BW', name: 'Baden-Württemberg' },
|
||||
@@ -137,9 +142,8 @@ export default async function functionRoutes(server: FastifyInstance) {
|
||||
|
||||
|
||||
return reply.send({
|
||||
...data,
|
||||
//@ts-ignore
|
||||
state_code: bundeslaender.find(i => i.name === data.countryName)
|
||||
...city,
|
||||
state_code: bundeslaender.find(i => i.name === city.countryName)?.code || null
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
|
||||
Reference in New Issue
Block a user