stuff done
This commit is contained in:
150
app/api/nova-post/route.ts
Normal file
150
app/api/nova-post/route.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
'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()
|
||||
}
|
||||
Reference in New Issue
Block a user