Skip to content

Commit

Permalink
Support no placeId with timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
morajabi committed Jul 2, 2019
1 parent cd65fd7 commit df5cab3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
16 changes: 13 additions & 3 deletions helpers/google/getTzAndLoc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { client as mapsClient } from './maps'

export default async placeId => {
const { json: { result } } = await mapsClient
if (!placeId) {
return {}
}

const {
json: { result },
} = await mapsClient
.place({
placeid: placeId,
language: 'en',
Expand All @@ -12,11 +18,15 @@ export default async placeId => {
const {
name: city,
formatted_address: fullLocation,
geometry: { location: { lat, lng } },
geometry: {
location: { lat, lng },
},
} = result

// Fetch timezone string based on geometry (Lat/Lng)
const { json: { timeZoneId: timezone } } = await mapsClient
const {
json: { timeZoneId: timezone },
} = await mapsClient
.timezone({
location: [lat, lng],
timestamp: Date.now() / 1000,
Expand Down
18 changes: 16 additions & 2 deletions resolvers/addManualPerson.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ import followingList from './followingList'

export default async (
obj,
{ firstName, lastName, twitterHandle, placeId, photoUrl, photoCloudObject },
{
firstName,
lastName,
twitterHandle,
placeId,
timezone: inputTimezone,
photoUrl,
photoCloudObject,
},
ctx,
info,
) => {
// Find timezone and exact location based on placeId
const { city, fullLocation, timezone } = await getTzAndLoc(placeId)
let { city, fullLocation, timezone } = await getTzAndLoc(placeId)

// For UTC
if (!placeId && !city && inputTimezone) {
city = null
fullLocation = null
timezone = inputTimezone
}
// Create and save place
const savedPerson = await ManualPerson.create({
firstName,
Expand Down
17 changes: 15 additions & 2 deletions resolvers/addManualPlace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@ import getTzAndLoc from '../helpers/google/getTzAndLoc'
import followingList from './followingList'

export default async (obj, args, ctx, info) => {
const { name, placeId, photoUrl, photoCloudObject } = args
const {
name,
timezone: inputTimezone,
placeId,
photoUrl,
photoCloudObject,
} = args

// Find timezone and exact location based on placeId
const { city, fullLocation, timezone } = await getTzAndLoc(placeId)
let { city, fullLocation, timezone } = await getTzAndLoc(placeId)

// Used for UTC
if (!placeId && !city && inputTimezone) {
city = null
fullLocation = null
timezone = inputTimezone
}

// Create and save place
const savedPlace = await ManualPlace.create({
Expand Down
7 changes: 6 additions & 1 deletion resolvers/updateManualPerson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async (obj, args, ctx) => {
throw new Error('No ID was specified.')
}

const { id, placeId, ...newValues } = args
const { id, placeId, timezone: inputTimezone, ...newValues } = args

if (Boolean(placeId)) {
// Only update location if it has been changed
Expand All @@ -19,6 +19,11 @@ export default async (obj, args, ctx) => {
newValues.fullLocation = fullLocation
}

// UTC
if (Boolean(inputTimezone)) {
newValues.timezone = inputTimezone
}

const [affected] = await ManualPerson.update(newValues, {
where: { id },
})
Expand Down
7 changes: 6 additions & 1 deletion resolvers/updateManualPlace.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async (obj, args, ctx) => {
throw new Error('No ID was specified.')
}

const { id, placeId, ...newValues } = args
const { id, placeId, timezone: inputTimezone, ...newValues } = args

if (Boolean(placeId)) {
// Only update location if it has been changed
Expand All @@ -19,6 +19,11 @@ export default async (obj, args, ctx) => {
newValues.fullLocation = fullLocation
}

// UTC
if (Boolean(inputTimezone)) {
newValues.timezone = inputTimezone
}

const [affected] = await ManualPlace.update(newValues, {
where: { id },
})
Expand Down
8 changes: 6 additions & 2 deletions schema/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ const typeDefs = gql`
addManualPlace(
name: String!
placeId: ID!
placeId: ID
timezone: String
photoUrl: String
photoCloudObject: String
): ManualPlace!
updateManualPlace(
id: ID!
name: String!
placeId: ID # It's not required for update
timezone: String
photoUrl: String
photoCloudObject: String
): ManualPlace
Expand All @@ -64,7 +66,8 @@ const typeDefs = gql`
addManualPerson(
firstName: String!
lastName: String
placeId: ID!
placeId: ID
timezone: String
twitterHandle: String
photoUrl: String
photoCloudObject: String
Expand All @@ -74,6 +77,7 @@ const typeDefs = gql`
firstName: String!
lastName: String
placeId: ID # It's not required for update
timezone: String # used for UTC
twitterHandle: String
photoUrl: String
photoCloudObject: String
Expand Down

0 comments on commit df5cab3

Please sign in to comment.