Improved locale handling and moved MAPBOX_TOKEN to env

This commit is contained in:
Grzegorz Leoniec 2019-01-04 15:14:51 +01:00
parent 1f818116e4
commit e9bc848a33
No known key found for this signature in database
GPG Key ID: 3AA43686D4EB1377
4 changed files with 48 additions and 22 deletions

View File

@ -7,3 +7,4 @@ CLIENT_URI=http://localhost:3000
MOCK=false
JWT_SECRET=b/&&7b78BF&fv/Vd
MAPBOX_TOKEN="pk.eyJ1IjoiaHVtYW4tY29ubmVjdGlvbiIsImEiOiJjajl0cnBubGoweTVlM3VwZ2lzNTNud3ZtIn0.KZ8KK9l70omjXbEkkbHGsQ"

View File

@ -1,5 +1,11 @@
import request from 'request'
import zipObject from 'lodash/zipObject'
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const fetch = url => {
return new Promise((resolve, reject) => {
@ -13,15 +19,12 @@ const fetch = url => {
})
}
const createOrUpdateLocations = async (userId, locationId, driver) =>{
if (!locationId) {
const createOrUpdateLocations = async (userId, locationName, driver) =>{
if (!locationName) {
return
}
console.log('userId', userId)
console.log('locationId', locationId)
const mapboxToken = 'pk.eyJ1IjoiaHVtYW4tY29ubmVjdGlvbiIsImEiOiJjajl0cnBubGoweTVlM3VwZ2lzNTNud3ZtIn0.KZ8KK9l70omjXbEkkbHGsQ'
const res = await fetch(`https://api.mapbox.com/geocoding/v5/mapbox.places/${locationId}.json?access_token=${mapboxToken}&language=de`)
const mapboxToken = process.env.MAPBOX_TOKEN
const res = await fetch(`https://api.mapbox.com/geocoding/v5/mapbox.places/${locationName}.json?access_token=${mapboxToken}&language=de`)
// TODO: create location in db
@ -29,15 +32,35 @@ const createOrUpdateLocations = async (userId, locationId, driver) =>{
const data = res.features[0]
const session = driver.session()
const r = await session.run(`MERGE (l:Location {id: "${data.id}"}) SET l.name = "${data.place_name}", l.type = "${data.place_type[0]}", l.lat = "${data.center[0]}", l.lng = "${data.center[1]}" RETURN l.id, l.name, l.type, l.lat, l.lng`)
// let location = r.records[0]._fields ? zipObject([
// 'id',
// 'name',
// 'type',
// 'lat',
// 'lng'
// ], r.records[0]._fields) : null
await session.run(
`MERGE (l:Location {id: "${data.id}"}) ` +
`SET l.name = "${data.text}", ` +
`l.type = "${data.place_type[0].toLowerCase()}", ` +
`l.lat = "${data.center[0]}", ` +
`l.lng = "${data.center[1]}" ` +
'RETURN l.id, l.name, l.type, l.lat, l.lng'
)
let parent = data
if (data.context) {
await asyncForEach(data.context, async ctx => {
const type = ctx.id.split('.')[0].toLowerCase()
await session.run(
`MERGE (l:Location {id: "${ctx.id}"}) ` +
`SET l.name = "${ctx.text}", ` +
`l.type = "${type}", ` +
`l.shortCode = "${ctx.short_code}" ` +
'RETURN l.id, l.name, l.type'
)
await session.run(
`MATCH (parent:Location {id: "${parent.id}"}), (child:Location {id: "${ctx.id}"}) ` +
'MERGE (child)<-[:IS_IN]-(parent) ' +
'RETURN child.id, parent.id')
parent = ctx
})
}
// delete all current locations from user
await session.run(`MATCH (u:User {id: "${userId}"})-[r:IS_IN]->(l:Location) DETACH DELETE r`)
// connect user with location
@ -49,12 +72,12 @@ export default {
Mutation: {
CreateUser: async (resolve, root, args, context, info) => {
const result = await resolve(root, args, context, info)
await createOrUpdateLocations(context.user.id, args.locationId, context.driver)
await createOrUpdateLocations(context.user.id, args.locationName, context.driver)
return result
},
UpdateUser: async (resolve, root, args, context, info) => {
const result = await resolve(root, args, context, info)
await createOrUpdateLocations(context.user.id, args.locationId, context.driver)
await createOrUpdateLocations(context.user.id, args.locationName, context.driver)
return result
}
}

View File

@ -50,8 +50,9 @@ type Location {
id: ID!
name: String!
type: String!
lat: Float!
lng: Float!
lat: Float
lng: Float
parent: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
}
type User {
@ -65,8 +66,8 @@ type User {
disabled: Boolean
role: UserGroupEnum
location: [Location] @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
locationId: String
location: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
locationName: String
about: String
createdAt: String

View File

@ -51,6 +51,7 @@ const createServer = (options) => {
},
schema: schema,
tracing: true,
debug: process.env.NODE_ENV !== 'production',
middlewares: middleware(schema),
mocks: (process.env.MOCK === 'true') ? mocks : false
}