Files
bewell-in-ua/app/api/nova-post/route.ts
2025-03-11 02:54:09 +02:00

151 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use server'
import {NextRequest} from 'next/server'
import {json} from 'node:stream/consumers'
import {type Warehouse} from '@/lib/nova-post-helper'
// , res: Response
export async function GET(req: NextRequest) {
const searchParams = req.nextUrl.searchParams
const scope = searchParams.get('scope')
let response: any = []
switch (scope) {
case 'cities':
response = await getCities(searchParams.get('q') || '...')
break
case 'warehouses':
response = await getWarehouses(searchParams.get('q') || '...')
break
case 'warehouse':
response = await getWarehouse(searchParams.get('q') || '...')
break
case 'streets':
response = await getStreet(searchParams.get('q') || '...')
break
}
return new Response(
JSON.stringify(response.success ? response.data : [], null, 2),
{
status: 200,
headers: {'Content-Type': 'application/json; charset=utf-8'}
}
)
}
async function fetchApi(init: RequestInit): Promise<Response> {
return await fetch(process.env.NOVA_POST_API_EP || '', init)
}
async function getWarehouses(CityRef: string, Page: number = 1) {
// const branches = []
let c = 0
// let n = 0
const Limit = 500
/*do {
const response = await fetchApi({
method: 'POST',
body: JSON.stringify({
apiKey: process.env.NOVA_POST_API_KEY || '',
modelName: 'AddressGeneral',
calledMethod: 'getWarehouses',
methodProperties: {
CityRef,
Page: ++c,
Limit,
FindByString: 'відд'
}
})
})
list = await response.json()
n = Math.ceil(list.info.totalCount / Limit)
for (const i in list.data) {
if (list.data[i].Description.trim().match(/^відді/iu)) {
branches.push(list.data[i])
}
}
} while (c < n)
list.data = branches*/
const response = await fetchApi({
method: 'POST',
body: JSON.stringify({
apiKey: process.env.NOVA_POST_API_KEY || '',
modelName: 'AddressGeneral',
calledMethod: 'getWarehouses',
methodProperties: {
CityRef,
Page: ++c,
Limit,
FindByString: 'відд'
}
})
})
const list = await response.json()
list.data = list.data.filter((item: Warehouse) =>
item.Description.trim().match(/^відді/iu)
)
//console.log(Math.ceil(list.info.totalCount / Limit), list.data.length)
return list
}
async function getWarehouse(Ref: string) {
const response = await fetch(process.env.NOVA_POST_API_EP || '', {
method: 'POST',
body: JSON.stringify({
apiKey: process.env.NOVA_POST_API_KEY || '',
modelName: 'AddressGeneral',
calledMethod: 'getWarehouses',
methodProperties: {
Ref,
Limit: 1
}
})
})
return await response.json()
}
async function getCities(searchString: string) {
const response = await fetch(process.env.NOVA_POST_API_EP || '', {
method: 'POST',
body: JSON.stringify({
apiKey: process.env.NOVA_POST_API_KEY || '',
modelName: 'AddressGeneral',
calledMethod: 'getCities',
methodProperties: {
FindByString: searchString,
Limit: 500
}
})
})
//console.log(searchString)
return await response.json()
}
async function getStreet(StreetName: string) {
const response = await fetch(process.env.NOVA_POST_API_EP || '', {
method: 'POST',
body: JSON.stringify({
apiKey: process.env.NOVA_POST_API_KEY || '',
modelName: 'AddressGeneral',
calledMethod: 'searchSettlementStreets',
methodProperties: {
SettlementRef: 'e718a680-4b33-11e4-ab6d-005056801329',
Page: 1,
Limit: 50,
StreetName
}
})
})
return await response.json()
}