This commit is contained in:
2026-02-15 12:51:26 +01:00
parent 29a84b899d
commit f63e793c88
5 changed files with 87 additions and 33 deletions

View File

@@ -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)