Skip to content

Commit b92ac97

Browse files
authored
Merge pull request #95 from Ripe-Apples/seed-cleanup-and-chron-job
Seed cleanup and chron job
2 parents 0eacfed + ef992ae commit b92ac97

9 files changed

+126
-80
lines changed

script/foursquareTestScript.js script/createFoursquareRatings.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ const fs = require('fs')
66

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

9-
async function foursquareCreate(createDbRestaurantObj) {
9+
async function foursquareCreate(restaurantObj) {
1010
try {
1111
console.log('Creating Foursquare reviews...')
1212
const fsqData = JSON.parse(
1313
fs.readFileSync('script/finalFoursquareData.json', 'utf8')
1414
)
15-
const restaurantDbObj = await createDbRestaurantObj()
15+
1616
const bulkCreateArr = fsqData
1717
.filter(restaurant => {
18-
if (restaurantDbObj[restaurant.name]) {
18+
if (restaurantObj[restaurant.name]) {
1919
return true
2020
}
2121
})
2222
.map(restaurant => {
2323
return {
2424
source: 'Foursquare',
2525
rating: restaurant.rating,
26-
restaurantId: restaurantDbObj[restaurant.name].id,
26+
restaurantId: restaurantObj[restaurant.name].id,
2727
sourceLogo: iconUrl,
2828
reviewUrl: restaurant.reviewUrl
2929
}

script/googleTestScript.js script/createGoogleRatings.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const utf8 = require('utf8')
66

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

9-
async function googleCreate(createDbRestaurantObj) {
9+
async function googleCreate(restaurantObj) {
1010
try {
1111
console.log('Creating Google reviews...')
1212
const googleData = JSON.parse(
@@ -16,19 +16,17 @@ async function googleCreate(createDbRestaurantObj) {
1616
if (restaurant) return true
1717
})
1818

19-
const restaurantDbObj = await createDbRestaurantObj()
20-
2119
const bulkCreateArr = newGoogleData
2220
.filter(restaurant => {
23-
if (restaurantDbObj[restaurant.name]) {
21+
if (restaurantObj[restaurant.name]) {
2422
return true
2523
}
2624
})
2725
.map(restaurant => {
2826
return {
2927
source: 'Google',
3028
rating: restaurant.rating,
31-
restaurantId: restaurantDbObj[restaurant.name].id,
29+
restaurantId: restaurantObj[restaurant.name].id,
3230
sourceLogo: iconUrl
3331
}
3432
})

script/openTableTestScript.js script/createOpentableLinks.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ const getOpenTableArray = async () => {
2525
}
2626
}
2727

28-
const opentableCreate = async createDbRestaurantObj => {
28+
const opentableCreate = async restaurantObj => {
2929
try {
3030
console.log('Creating OpenTable links...')
31-
const restaurantDbObj = await createDbRestaurantObj()
3231
const opentableArray = await getOpenTableArray()
3332
const arrayForUpdate = opentableArray
34-
.filter(restaurant => (restaurantDbObj[restaurant.name] ? true : false))
33+
.filter(restaurant => (restaurantObj[restaurant.name] ? true : false))
3534
.map(restaurant => {
3635
return [
3736
{opentableUrl: restaurant.reserve_url},
38-
{where: {id: restaurantDbObj[restaurant.name].id}}
37+
{where: {id: restaurantObj[restaurant.name].id}}
3938
]
4039
})
4140
for (let i = 0; i < arrayForUpdate.length; i++) {

script/yelpTestScript.js script/createYelpRestaurants.js

+30-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function getYelp(offset) {
2121
return data.businesses
2222
}
2323

24-
async function getRestaurants() {
24+
async function getYelpRestaurants() {
2525
let restaurants = []
2626

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

3434
async function createYelpRestaurants() {
35-
const restaurants = await getRestaurants()
35+
const restaurants = await getYelpRestaurants()
3636
const restaurantsArr = restaurants.map(restaurant => {
3737
const location = restaurant.location.display_address.join(', ')
3838

@@ -49,19 +49,34 @@ async function createYelpRestaurants() {
4949
})
5050

5151
await Restaurant.bulkCreate(restaurantsArr)
52+
}
5253

53-
const ratingsArr = restaurants.map((restaurant, idx) => {
54-
let id = idx + 1
55-
return {
56-
source: 'Yelp',
57-
rating: restaurant.rating,
58-
restaurantId: id,
59-
sourceLogo: iconUrl,
60-
reviewUrl: restaurant.url
61-
}
62-
})
63-
64-
await Review.bulkCreate(ratingsArr)
54+
async function createYelpRatings(restaurantObj) {
55+
try {
56+
console.log('Creating Yelp reviews...')
57+
const restaurants = await getYelpRestaurants()
58+
let newObj = {}
59+
const ratingsArr = restaurants
60+
.filter(restaurant => {
61+
if (restaurantObj[restaurant.name] && !newObj[restaurant.name]) {
62+
newObj[restaurant.name] = true
63+
return true
64+
}
65+
})
66+
.map(restaurant => {
67+
return {
68+
source: 'Yelp',
69+
rating: restaurant.rating,
70+
restaurantId: restaurantObj[restaurant.name].id,
71+
sourceLogo: iconUrl,
72+
reviewUrl: restaurant.url
73+
}
74+
})
75+
await Review.bulkCreate(ratingsArr)
76+
console.log('Yelp reviews are done!')
77+
} catch (error) {
78+
console.error(error)
79+
}
6580
}
6681

6782
async function yelpCreate() {
@@ -74,4 +89,4 @@ async function yelpCreate() {
7489
}
7590
}
7691

77-
module.exports = yelpCreate
92+
module.exports = {yelpCreate, createYelpRatings}

script/zomatoTestScript.js script/createZomatoRatings.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,20 @@ const createZomatoRestaurantObj = async () => {
6666
}
6767
}
6868

69-
async function zomatoCreate(createDbRestaurantObj) {
69+
async function zomatoCreate(restaurantObj) {
7070
try {
7171
console.log('Creating Zomato reviews...')
7272
let zomatoRestaurants = await createZomatoRestaurantObj()
7373
zomatoRestaurants = zomatoRestaurants.filter(restaurant => {
7474
if (restaurant) return true
7575
})
76-
const dbRestaurants = await createDbRestaurantObj()
7776
let reviewsForCreate = []
7877
zomatoRestaurants.forEach(restaurant => {
79-
if (dbRestaurants[restaurant.name]) {
78+
if (restaurantObj[restaurant.name]) {
8079
reviewsForCreate.push({
8180
source: 'Zomato',
8281
rating: restaurant.rating,
83-
restaurantId: dbRestaurants[restaurant.name].id,
82+
restaurantId: restaurantObj[restaurant.name].id,
8483
sourceLogo: iconUrl,
8584
reviewUrl: restaurant.reviewUrl
8685
})

script/seed.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
const db = require('../server/db')
44
const {User} = require('../server/db/models')
5-
const yelpCreate = require('./yelpTestScript')
6-
const zomatoCreate = require('./zomatoTestScript')
7-
const googleCreate = require('./googleTestScript')
8-
const foursquareCreate = require('./foursquareTestScript')
9-
const openTableCreate = require('./openTableTestScript')
5+
const {yelpCreate, createYelpRatings} = require('./createYelpRestaurants')
6+
const zomatoCreate = require('./createZomatoRatings')
7+
const googleCreate = require('./createGoogleRatings')
8+
const foursquareCreate = require('./createFoursquareRatings')
9+
const openTableCreate = require('./createOpentableLinks')
1010
const createDbRestaurantObj = require('./helperFunctions')
1111

1212
async function seed() {
@@ -20,11 +20,18 @@ async function seed() {
2020
User.create({email: 'victor@email.com', password: '123'})
2121
])
2222
console.log(`seeded ${users.length} users`)
23+
//DB depends on Yelp to create restaurants before creating reviews
2324
await yelpCreate()
24-
await zomatoCreate(createDbRestaurantObj)
25-
await googleCreate(createDbRestaurantObj)
26-
await foursquareCreate(createDbRestaurantObj)
27-
await openTableCreate(createDbRestaurantObj)
25+
//Creates object of existing restaurants to use in other create functions
26+
const restaurantObj = await createDbRestaurantObj()
27+
//Runs all async functions for creating reviews and adding OpenTable links in parallel
28+
await Promise.all([
29+
createYelpRatings(restaurantObj),
30+
zomatoCreate(restaurantObj),
31+
googleCreate(restaurantObj),
32+
foursquareCreate(restaurantObj),
33+
openTableCreate(restaurantObj)
34+
])
2835
console.log(`Seeded successfully`)
2936
}
3037

script/updateScript.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const {createYelpRatings} = require('./createYelpRestaurants')
2+
const createGoogleRatings = require('./createGoogleRatings')
3+
const createZomatoRatings = require('./createZomatoRatings')
4+
const createFoursquareRatings = require('./createFoursquareRatings')
5+
const createRestaurantDbObj = require('./helperFunctions')
6+
const db = require('../server/db')
7+
const {Review} = require('../server/db/models')
8+
9+
const updateRatings = async () => {
10+
try {
11+
console.log('Updating restaurant ratings...')
12+
await Review.destroy({truncate: true, force: true})
13+
console.log('Existing reviews are deleted :(')
14+
const restaurantObj = await createRestaurantDbObj()
15+
await Promise.all([
16+
createYelpRatings(restaurantObj),
17+
createGoogleRatings(restaurantObj),
18+
createZomatoRatings(restaurantObj),
19+
createFoursquareRatings(restaurantObj)
20+
])
21+
db.close()
22+
console.log('Updating is done!')
23+
} catch (error) {
24+
console.error(error)
25+
}
26+
}
27+
28+
updateRatings()

server/api/users.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ const router = require('express').Router()
22
const {User} = require('../db/models')
33
module.exports = router
44

5-
router.get('/', async (req, res, next) => {
6-
try {
7-
const users = await User.findAll({
8-
// explicitly select only the id and email fields - even though
9-
// users' passwords are encrypted, it won't help if we just
10-
// send everything to anyone who asks!
11-
attributes: ['id', 'email']
12-
})
13-
res.json(users)
14-
} catch (err) {
15-
next(err)
16-
}
17-
})
5+
// router.get('/', async (req, res, next) => {
6+
// try {
7+
// const users = await User.findAll({
8+
// // explicitly select only the id and email fields - even though
9+
// // users' passwords are encrypted, it won't help if we just
10+
// // send everything to anyone who asks!
11+
// attributes: ['id', 'email']
12+
// })
13+
// res.json(users)
14+
// } catch (err) {
15+
// next(err)
16+
// }
17+
// })

server/api/users.spec.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
/* global describe beforeEach it */
1+
// /* global describe beforeEach it */
22

3-
const {expect} = require('chai')
4-
const request = require('supertest')
5-
const db = require('../db')
6-
const app = require('../index')
7-
const User = db.model('user')
3+
// const {expect} = require('chai')
4+
// const request = require('supertest')
5+
// const db = require('../db')
6+
// const app = require('../index')
7+
// const User = db.model('user')
88

9-
describe('User routes', () => {
10-
beforeEach(() => {
11-
return db.sync({force: true})
12-
})
9+
// describe('User routes', () => {
10+
// beforeEach(() => {
11+
// return db.sync({force: true})
12+
// })
1313

14-
describe('/api/users/', () => {
15-
const codysEmail = 'cody@puppybook.com'
14+
// describe('/api/users/', () => {
15+
// const codysEmail = 'cody@puppybook.com'
1616

17-
beforeEach(() => {
18-
return User.create({
19-
email: codysEmail
20-
})
21-
})
17+
// beforeEach(() => {
18+
// return User.create({
19+
// email: codysEmail
20+
// })
21+
// })
2222

23-
it('GET /api/users', async () => {
24-
const res = await request(app)
25-
.get('/api/users')
26-
.expect(200)
23+
// it('GET /api/users', async () => {
24+
// const res = await request(app)
25+
// .get('/api/users')
26+
// .expect(200)
2727

28-
expect(res.body).to.be.an('array')
29-
expect(res.body[0].email).to.be.equal(codysEmail)
30-
})
31-
}) // end describe('/api/users')
32-
}) // end describe('User routes')
28+
// expect(res.body).to.be.an('array')
29+
// expect(res.body[0].email).to.be.equal(codysEmail)
30+
// })
31+
// }) // end describe('/api/users')
32+
// }) // end describe('User routes')

0 commit comments

Comments
 (0)