From e9bc848a334e30d4717c73325dd71f3d2bd39922 Mon Sep 17 00:00:00 2001 From: Grzegorz Leoniec Date: Fri, 4 Jan 2019 15:14:51 +0100 Subject: [PATCH] Improved locale handling and moved MAPBOX_TOKEN to env --- .env.template | 1 + src/middleware/userMiddleware.js | 59 ++++++++++++++++++++++---------- src/schema.graphql | 9 ++--- src/server.js | 1 + 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/.env.template b/.env.template index 3ec0cbce9..8f05f50c1 100644 --- a/.env.template +++ b/.env.template @@ -7,3 +7,4 @@ CLIENT_URI=http://localhost:3000 MOCK=false JWT_SECRET=b/&&7b78BF&fv/Vd +MAPBOX_TOKEN="pk.eyJ1IjoiaHVtYW4tY29ubmVjdGlvbiIsImEiOiJjajl0cnBubGoweTVlM3VwZ2lzNTNud3ZtIn0.KZ8KK9l70omjXbEkkbHGsQ" diff --git a/src/middleware/userMiddleware.js b/src/middleware/userMiddleware.js index 5e1f14493..56bb524c8 100644 --- a/src/middleware/userMiddleware.js +++ b/src/middleware/userMiddleware.js @@ -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 } } diff --git a/src/schema.graphql b/src/schema.graphql index 5ee0412a9..48b3c4302 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -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 diff --git a/src/server.js b/src/server.js index 3b0e0a561..1175509e7 100644 --- a/src/server.js +++ b/src/server.js @@ -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 }