Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created August 17, 2020 22:24
Show Gist options
  • Save jcuffe/6e763d9f4b955d7c6109f68fdb7284e7 to your computer and use it in GitHub Desktop.
Save jcuffe/6e763d9f4b955d7c6109f68fdb7284e7 to your computer and use it in GitHub Desktop.
Venue data migration
require("dotenv").config()
const Knex = require("knex")
const knexConfig = require("../config/knexfile")
const { Model } = require("objection")
const Event = require("../database/models/Event")
const Venue = require("../database/models/Venue")
const knex = Knex(knexConfig["development"])
const axios = require("axios")
const mapService = require("../services/maps")
Model.knex(knex)
const googleKey = process.env.GOOGLE_MAPS_KEY
const geocode = (address) => {
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${googleKey}`
return axios.get(url)
}
const failedVenues = []
const noEventVenues = []
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
const processVenue = async (venue) => {
const address = encodeURIComponent(
[
venue.addressOne,
venue.addressTwo,
venue.city,
venue.state,
venue.zip,
].join(" ")
)
if (address.includes("Phone%2FOnline%20Event")) {
console.log("Skipping online event")
return
}
const venueName = venue.name
try {
const response = await geocode(address)
const result = response.data.results[0]
const event = await Event.query().where("venueId", "=", venue.id).first()
if (!event) {
noEventVenues.push(venue.id)
return
}
if (!result) {
if (response.data.status === "ZERO_RESULTS") {
failedVenues.push(venue.id)
return
}
console.log(response)
return
}
const location = result.formatted_address
const longitude = result.geometry.location.lng
const latitude = result.geometry.location.lat
const { exactMap, areaMap, publicMap } = mapService.generateVenueMapUrls(
location
)
await event.$query().patch({
venueName,
location,
longitude,
latitude,
exactMap,
areaMap,
publicMap,
})
} catch (err) {
failedVenues.push(venue.id)
console.log(err)
throw err
}
}
const execute = async () => {
const venues = await Venue.query()
const promises = []
for (let i = 0; i < venues.length; i++) {
const venue = venues[i]
promises.push(processVenue(venue))
await sleep(1000)
}
await Promise.all(promises)
console.log("No result venues:")
console.log(failedVenues)
console.log("No event venues:")
console.log(noEventVenues)
}
execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment