Skip to content

Instantly share code, notes, and snippets.

@artyom-kurnikov-doit
Last active February 6, 2019 15:33
Show Gist options
  • Save artyom-kurnikov-doit/c2079c3cdd311833698845a60da8397a to your computer and use it in GitHub Desktop.
Save artyom-kurnikov-doit/c2079c3cdd311833698845a60da8397a to your computer and use it in GitHub Desktop.
import * as queries from '../../db/queries/main-queries'
import * as pgQueries from '../../db/queries/pg-queries'
import * as helpers from '../../shared/helpers'
import { wrapper } from '../../utils/wrapper'
import { logRequest } from '../../db/queries/logging'
interface IWispResponse {
success: boolean,
data: { count: number, providers: string[], type: string } | null
errorMessage?: string
}
type WispResponse = Promise<IWispResponse>
export const getWispDataWithCoords = async (lat: string | number, lon: string | number): Promise<WispResponse> => {
const [censusData] = await pgQueries.getCensusBlock(lat, lon)
if (!censusData) {
return { success: false, data: null, errorMessage: 'Unable to get census block by provided coords' }
}
const wispData = await queries.getWISPCensus(censusData.geoid10)
if (!wispData.length) {
return { success: false, data: null, errorMessage: 'Unable to get census data' }
}
return {
success: true,
data: {
count: wispData.length,
providers: wispData.map(p => p.c2f_wisp_name),
type: 'Wireless Service Providers'
}
}
}
export const getWispDataWithAddresskey = async (addresskey: string): WispResponse => {
const data = await queries.getWISP(addresskey)
if (!data.length) {
return { success: false, data: null, errorMessage: 'Unable to get wisp data by provided address' }
}
return {
success: true,
data: {
count: data.length,
providers: data.map(p => p.c2f_wisp_name),
type: 'Wireless Service Providers'
}
}
}
export const wisp = wrapper(async (req, res, next) => {
const locationData = await helpers.getLocationData({ req, checkValidation: true })
const wispData = req.query.lat
? await getWispDataWithCoords(req.query.lat, req.query.lon)
: await getWispDataWithAddresskey(locationData.addresskey)
if (!wispData.success) {
return next({ httpCode: 400, message: wispData.errorMessage, logToDb: true })
}
res.json({ status: 'success', body: { ...wispData.data, location: locationData.location } })
logRequest({ response: 'success', req })
})
import * as pg from '../../db/queries/pg-queries'
import * as queries from '../../db/queries/main-queries'
import * as helpers from '../../shared/helpers'
import { wrapper } from '../../utils/wrapper'
import { logRequest } from '../../db/queries/logging'
interface IGetCablecoData {
success: boolean
data: { cableProvider: string, phone: string, type: string, website: string },
errorMessage: string
}
export const getCablecoData = async (lat: string | number, lon: string | number): Promise<IGetCablecoData> => {
const [censusData] = await pg.getCensusBlock(lat, lon)
if (!censusData) {
return { success: false, data: null, errorMessage: 'Unable to get census block by provided coords' }
}
const [cablecoData] = await queries.getCablecoCensus(censusData.geoid10)
if (!cablecoData) {
return { success: false, data: null, errorMessage: 'Unable to get census data' }
}
return {
success: true,
data: {
cableProvider: cablecoData.c2f_mso_name,
phone: cablecoData.provider_phone,
type: 'Cableco',
website: cablecoData.dns
},
errorMessage: ''
}
}
export const cableco = wrapper(async (req, res, next) => {
const locationData = await helpers.getLocationData({ req })
const cablecoData = await getCablecoData(locationData.location.latitude, locationData.location.longitude)
if (!cablecoData.success) {
return next({ httpCode: 400, message: cablecoData.errorMessage, logToDb: true })
}
res.json({ status: 'success', body: { ...cablecoData.data, location: locationData.location } })
logRequest({ response: 'success', req })
})
import * as pg from '../../db/queries/pg-queries'
import { wrapper } from '../../utils/wrapper'
import * as helpers from '../../shared/helpers'
import { logRequest } from '../../db/queries/logging'
export const getTelcoData = async (lat: string | number, lon: string | number) => {
const [data] = await pg.getCLLIData(lat, lon)
if (!data) {
return { success: false, data: null, errorMessage: 'Unable to get CLLI data by provided coords' }
}
const { v, h, c2f_name, ...restTelcoData } = data
return { success: true, data: { type: 'Telco', ...restTelcoData, lec: c2f_name }, errorMessage: '' }
}
export const telco = wrapper(async (req, res, next) => {
const locationData = await helpers.getLocationData({ req })
const telcoData = await getTelcoData(locationData.location.latitude, locationData.location.longitude)
if (!telcoData.success) {
return next({ httpCode: 400, message: telcoData.errorMessage, logToDb: true })
}
res.json({
status: 'success',
body: { ...telcoData.data, location: locationData.location }
})
logRequest({ response: 'success', req })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment