Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed cleanup and chron job #95

Merged
merged 7 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ const fs = require('fs')

const iconUrl = 'https://image.flaticon.com/icons/svg/174/174850.svg'

async function foursquareCreate(createDbRestaurantObj) {
async function foursquareCreate(restaurantObj) {
try {
console.log('Creating Foursquare reviews...')
const fsqData = JSON.parse(
fs.readFileSync('script/finalFoursquareData.json', 'utf8')
)
const restaurantDbObj = await createDbRestaurantObj()

const bulkCreateArr = fsqData
.filter(restaurant => {
if (restaurantDbObj[restaurant.name]) {
if (restaurantObj[restaurant.name]) {
return true
}
})
.map(restaurant => {
return {
source: 'Foursquare',
rating: restaurant.rating,
restaurantId: restaurantDbObj[restaurant.name].id,
restaurantId: restaurantObj[restaurant.name].id,
sourceLogo: iconUrl,
reviewUrl: restaurant.reviewUrl
}
Expand Down
8 changes: 3 additions & 5 deletions script/googleTestScript.js → script/createGoogleRatings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const utf8 = require('utf8')

const iconUrl = 'https://image.flaticon.com/icons/svg/281/281781.svg'

async function googleCreate(createDbRestaurantObj) {
async function googleCreate(restaurantObj) {
try {
console.log('Creating Google reviews...')
const googleData = JSON.parse(
Expand All @@ -16,19 +16,17 @@ async function googleCreate(createDbRestaurantObj) {
if (restaurant) return true
})

const restaurantDbObj = await createDbRestaurantObj()

const bulkCreateArr = newGoogleData
.filter(restaurant => {
if (restaurantDbObj[restaurant.name]) {
if (restaurantObj[restaurant.name]) {
return true
}
})
.map(restaurant => {
return {
source: 'Google',
rating: restaurant.rating,
restaurantId: restaurantDbObj[restaurant.name].id,
restaurantId: restaurantObj[restaurant.name].id,
sourceLogo: iconUrl
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ const getOpenTableArray = async () => {
}
}

const opentableCreate = async createDbRestaurantObj => {
const opentableCreate = async restaurantObj => {
try {
console.log('Creating OpenTable links...')
const restaurantDbObj = await createDbRestaurantObj()
const opentableArray = await getOpenTableArray()
const arrayForUpdate = opentableArray
.filter(restaurant => (restaurantDbObj[restaurant.name] ? true : false))
.filter(restaurant => (restaurantObj[restaurant.name] ? true : false))
.map(restaurant => {
return [
{opentableUrl: restaurant.reserve_url},
{where: {id: restaurantDbObj[restaurant.name].id}}
{where: {id: restaurantObj[restaurant.name].id}}
]
})
for (let i = 0; i < arrayForUpdate.length; i++) {
Expand Down
45 changes: 30 additions & 15 deletions script/yelpTestScript.js → script/createYelpRestaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function getYelp(offset) {
return data.businesses
}

async function getRestaurants() {
async function getYelpRestaurants() {
let restaurants = []

for (let i = 0; i < 20; i++) {
Expand All @@ -32,7 +32,7 @@ async function getRestaurants() {
}

async function createYelpRestaurants() {
const restaurants = await getRestaurants()
const restaurants = await getYelpRestaurants()
const restaurantsArr = restaurants.map(restaurant => {
const location = restaurant.location.display_address.join(', ')

Expand All @@ -49,19 +49,34 @@ async function createYelpRestaurants() {
})

await Restaurant.bulkCreate(restaurantsArr)
}

const ratingsArr = restaurants.map((restaurant, idx) => {
let id = idx + 1
return {
source: 'Yelp',
rating: restaurant.rating,
restaurantId: id,
sourceLogo: iconUrl,
reviewUrl: restaurant.url
}
})

await Review.bulkCreate(ratingsArr)
async function createYelpRatings(restaurantObj) {
try {
console.log('Creating Yelp reviews...')
const restaurants = await getYelpRestaurants()
let newObj = {}
const ratingsArr = restaurants
.filter(restaurant => {
if (restaurantObj[restaurant.name] && !newObj[restaurant.name]) {
newObj[restaurant.name] = true
return true
}
})
.map(restaurant => {
return {
source: 'Yelp',
rating: restaurant.rating,
restaurantId: restaurantObj[restaurant.name].id,
sourceLogo: iconUrl,
reviewUrl: restaurant.url
}
})
await Review.bulkCreate(ratingsArr)
console.log('Yelp reviews are done!')
} catch (error) {
console.error(error)
}
}

async function yelpCreate() {
Expand All @@ -74,4 +89,4 @@ async function yelpCreate() {
}
}

module.exports = yelpCreate
module.exports = {yelpCreate, createYelpRatings}
7 changes: 3 additions & 4 deletions script/zomatoTestScript.js → script/createZomatoRatings.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,20 @@ const createZomatoRestaurantObj = async () => {
}
}

async function zomatoCreate(createDbRestaurantObj) {
async function zomatoCreate(restaurantObj) {
try {
console.log('Creating Zomato reviews...')
let zomatoRestaurants = await createZomatoRestaurantObj()
zomatoRestaurants = zomatoRestaurants.filter(restaurant => {
if (restaurant) return true
})
const dbRestaurants = await createDbRestaurantObj()
let reviewsForCreate = []
zomatoRestaurants.forEach(restaurant => {
if (dbRestaurants[restaurant.name]) {
if (restaurantObj[restaurant.name]) {
reviewsForCreate.push({
source: 'Zomato',
rating: restaurant.rating,
restaurantId: dbRestaurants[restaurant.name].id,
restaurantId: restaurantObj[restaurant.name].id,
sourceLogo: iconUrl,
reviewUrl: restaurant.reviewUrl
})
Expand Down
25 changes: 16 additions & 9 deletions script/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const db = require('../server/db')
const {User} = require('../server/db/models')
const yelpCreate = require('./yelpTestScript')
const zomatoCreate = require('./zomatoTestScript')
const googleCreate = require('./googleTestScript')
const foursquareCreate = require('./foursquareTestScript')
const openTableCreate = require('./openTableTestScript')
const {yelpCreate, createYelpRatings} = require('./createYelpRestaurants')
const zomatoCreate = require('./createZomatoRatings')
const googleCreate = require('./createGoogleRatings')
const foursquareCreate = require('./createFoursquareRatings')
const openTableCreate = require('./createOpentableLinks')
const createDbRestaurantObj = require('./helperFunctions')

async function seed() {
Expand All @@ -20,11 +20,18 @@ async function seed() {
User.create({email: 'victor@email.com', password: '123'})
])
console.log(`seeded ${users.length} users`)
//DB depends on Yelp to create restaurants before creating reviews
await yelpCreate()
await zomatoCreate(createDbRestaurantObj)
await googleCreate(createDbRestaurantObj)
await foursquareCreate(createDbRestaurantObj)
await openTableCreate(createDbRestaurantObj)
//Creates object of existing restaurants to use in other create functions
const restaurantObj = await createDbRestaurantObj()
//Runs all async functions for creating reviews and adding OpenTable links in parallel
await Promise.all([
createYelpRatings(restaurantObj),
zomatoCreate(restaurantObj),
googleCreate(restaurantObj),
foursquareCreate(restaurantObj),
openTableCreate(restaurantObj)
])
console.log(`Seeded successfully`)
}

Expand Down
28 changes: 28 additions & 0 deletions script/updateScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {createYelpRatings} = require('./createYelpRestaurants')
const createGoogleRatings = require('./createGoogleRatings')
const createZomatoRatings = require('./createZomatoRatings')
const createFoursquareRatings = require('./createFoursquareRatings')
const createRestaurantDbObj = require('./helperFunctions')
const db = require('../server/db')
const {Review} = require('../server/db/models')

const updateRatings = async () => {
try {
console.log('Updating restaurant ratings...')
await Review.destroy({truncate: true, force: true})
console.log('Existing reviews are deleted :(')
const restaurantObj = await createRestaurantDbObj()
await Promise.all([
createYelpRatings(restaurantObj),
createGoogleRatings(restaurantObj),
createZomatoRatings(restaurantObj),
createFoursquareRatings(restaurantObj)
])
db.close()
console.log('Updating is done!')
} catch (error) {
console.error(error)
}
}

updateRatings()
9 changes: 0 additions & 9 deletions server/api/restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,3 @@ router.get('/:restaurantId', async (req, res, next) => {
next(err)
}
})

router.post('/', async (req, res, next) => {
try {
const newRestaurant = await Restaurant.create(req.body)
res.json(newRestaurant)
} catch (err) {
next(err)
}
})
60 changes: 60 additions & 0 deletions server/api/restaurants.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const {expect} = require('chai')
const request = require('supertest')
const db = require('../db')
const app = require('../index')
const Restaurant = db.model('restaurants')

describe('Restaurant routes', () => {
beforeEach(() => {
return db.sync({force: true})
})

const newRestaurant = {
name: 'test rest',
cuisineType: ['chinese'],
expenseRating: 2,
location: 'New York, NY',
latitude: 34.4,
longitude: 34.4
}

describe('Gets all restaurants', () => {
beforeEach(() => {
return Restaurant.create(newRestaurant)
})

it('GET /api/restaurant', async () => {
const res = await request(app)
.get('/api/restaurant')
.expect(200)

expect(res.body).to.be.an('array')
expect(res.body[0].name).to.be.equal('test rest')
expect(res.body[0].cuisineType[0]).to.be.equal('chinese')
expect(res.body[0].expenseRating).to.be.equal(2)
expect(res.body[0].location).to.be.equal('New York, NY')
expect(res.body[0].latitude).to.be.equal(34.4)
expect(res.body[0].longitude).to.be.equal(34.4)
})
})

describe('Gets a single restaurant', () => {
beforeEach(() => {
return Restaurant.create(newRestaurant)
})

it('GET api/restaurant/restaurantId', async () => {
const res = await request(app)
.get('/api/restaurant/1')
.expect(200)

expect(res.body).to.be.an('object')
expect(res.body.name).to.be.equal('test rest')
expect(res.body.cuisineType[0]).to.be.equal('chinese')
expect(res.body.expenseRating).to.be.equal(2)
expect(res.body.location).to.be.equal('New York, NY')
expect(res.body.latitude).to.be.equal(34.4)
expect(res.body.longitude).to.be.equal(34.4)
})
})
})
14 changes: 7 additions & 7 deletions server/api/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const {Review} = require('../db/models')
module.exports = router

router.get('/', async (req, res, next) => {
try {
const reviews = await Review.findAll()
res.json(reviews)
} catch (err) {
next(err)
}
})
try {
const reviews = await Review.findAll()
res.json(reviews)
} catch (err) {
next(err)
}
})
36 changes: 36 additions & 0 deletions server/api/review.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const {expect} = require('chai')
const request = require('supertest')
const db = require('../db')
const app = require('../index')
const Review = db.model('reviews')

describe('Review routes', () => {
beforeEach(() => {
return db.sync({force: true})
})

const newReview = {
source: 'Yelp',
sourceLogo: 'image!',
rating: 2.5,
reviewUrl: 'a review url'
}

describe('Gets all reviews', () => {
beforeEach(() => {
return Review.create(newReview)
})

it('GET /api/review', async () => {
const res = await request(app)
.get('/api/review')
.expect(200)

expect(res.body).to.be.an('array')
expect(res.body[0].source).to.be.equal('Yelp')
expect(res.body[0].sourceLogo).to.be.equal('image!')
expect(res.body[0].rating).to.be.equal(2.5)
expect(res.body[0].reviewUrl).to.be.equal('a review url')
})
})
})
Loading