ourjapanlife / findadoc-server

The api / server for Find A Doc, Japan. Check out the board to see the latest effort https://github.com/orgs/ourjapanlife/projects/8
4 stars 1 forks source link

feat: implement function to procure facility submission details #498

Open NabbeunNabi opened 4 months ago

NabbeunNabi commented 4 months ago

Resolves issue #234

The documentation that describes what I did is: -Google Places API documentation -Pricing Pricing is only $2.83 per 1000 calls

Flow

I made comments in the code to help follow. But this is the flow of the file:

  1. Use puppeteer to open a headerless browser that will wait for the page to redirect to the desktop version of the link. This will get us a url with the coordinates.
  2. From this url regex is used to extract the coordinates that are in every url
  3. The coordinates are then used to do a google places search using a text query and location bias returning the closest relevant response to the query. This is because the coordinates should be as exact as possible. (We might need to think more if they are in the same building. Currently I am returning all the fields because we can implement our own filters later for other things if we scale like wheelchair accessibility.)
  4. The prior three steps are then wrapped up in a final function that can be used

Reasoning for flow

I tried multiple ways to get Tokyo Midtown Clinic will you will see is what I tested in the testing header. Following are the other things I tried and read about:

Testing Instructions (for now)

  1. Add a .env with a GOOGLE_MAPS_API_KEY
  2. If you have tsx installed run:
    tsx ./utils/submissionDataFromGoogleMaps.ts

    OR

In the terminal run:

npx tsc utils/submissionDataFromGoogleMaps.ts

And then change the generated file from .js to .cjs so the next command can run the common js generations and run:

node utils/submissionDataFromGoogleMaps.cjs

This was used to generate the following code twice for Tokyo Midtown Clinic using the google places API.

Try 1

  {
    name: 'places/ChIJ30G_l3iLGGARXEK75Rc-3Ww',
    id: 'ChIJ30G_l3iLGGARXEK75Rc-3Ww',
    types: [ 'doctor', 'health', 'point_of_interest', 'establishment' ],
    nationalPhoneNumber: '03-5413-0080',
    internationalPhoneNumber: '+81 3-5413-0080',
    formattedAddress: 'Japan, 〒107-6206 Tokyo, Minato City, Akasaka, 9-chōme−7−1, Midtown Tower, 6F',
    addressComponents: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ],
    plusCode: {
      globalCode: '8Q7XMP8J+CG',
      compoundCode: 'MP8J+CG Minato City, Tokyo, Japan'
    },
    location: { latitude: 35.6661201, longitude: 139.7312888 },
    viewport: { low: [Object], high: [Object] },
    rating: 2.7,
    googleMapsUri: 'https://maps.google.com/?cid=7844494398308696668',
    websiteUri: 'https://www.tokyomidtown-mc.jp/',
    regularOpeningHours: { openNow: true, periods: [Array], weekdayDescriptions: [Array] },
    utcOffsetMinutes: 540,
    adrFormatAddress: '<span class="country-name">Japan</span>, <span class="postal-code">〒107-6206</span> <span class="region">Tokyo</span>, <span class="street-addres
s">Minato City, Akasaka, 9-chōme−7−1, Midtown Tower</span>, 6F',
    businessStatus: 'OPERATIONAL',
    userRatingCount: 204,
    iconMaskBaseUri: 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet',
    iconBackgroundColor: '#7B9EB0',
    displayName: { text: 'Tokyo Midtown Clinic', languageCode: 'en' },
    currentOpeningHours: { openNow: true, periods: [Array], weekdayDescriptions: [Array] },
    shortFormattedAddress: 'Minato City, Akasaka, 9-chōme−7−1 6F Midtown Tower',
    reviews: [ [Object], [Object], [Object], [Object], [Object] ],
    photos: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    restroom: true,
    accessibilityOptions: {
      wheelchairAccessibleParking: true,
      wheelchairAccessibleEntrance: true,
      wheelchairAccessibleRestroom: true
    }
  }
]

Try 2

[
  {
    name: 'places/ChIJ30G_l3iLGGARXEK75Rc-3Ww',
    id: 'ChIJ30G_l3iLGGARXEK75Rc-3Ww',
    types: [ 'doctor', 'point_of_interest', 'health', 'establishment' ],
    nationalPhoneNumber: '03-5413-0080',
    internationalPhoneNumber: '+81 3-5413-0080',
    formattedAddress: 'Japan, 〒107-6206 Tokyo, Minato City, Akasaka, 9-chōme−7−1, Midtown Tower, 6F',
    addressComponents: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ],
    plusCode: {
      globalCode: '8Q7XMP8J+CG',
      compoundCode: 'MP8J+CG Minato City, Tokyo, Japan'
    },
    location: { latitude: 35.6661201, longitude: 139.7312888 },
    viewport: { low: [Object], high: [Object] },
    rating: 2.7,
    googleMapsUri: 'https://maps.google.com/?cid=7844494398308696668',
    websiteUri: 'https://www.tokyomidtown-mc.jp/',
    regularOpeningHours: { openNow: true, periods: [Array], weekdayDescriptions: [Array] },
    utcOffsetMinutes: 540,
    adrFormatAddress: '<span class="country-name">Japan</span>, <span class="postal-code">〒107-6206</span> <span class="region">Tokyo</span>, <span class="street-addres
s">Minato City, Akasaka, 9-chōme−7−1, Midtown Tower</span>, 6F',
    businessStatus: 'OPERATIONAL',
    userRatingCount: 204,
    iconMaskBaseUri: 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet',
    iconBackgroundColor: '#7B9EB0',
    displayName: { text: 'Tokyo Midtown Clinic', languageCode: 'en' },
    currentOpeningHours: { openNow: true, periods: [Array], weekdayDescriptions: [Array] },
    shortFormattedAddress: 'Minato City, Akasaka, 9-chōme−7−1 6F Midtown Tower',
    reviews: [ [Object], [Object], [Object], [Object], [Object] ],
    photos: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    restroom: true,
    accessibilityOptions: {
      wheelchairAccessibleParking: true,
      wheelchairAccessibleEntrance: true,
      wheelchairAccessibleRestroom: true
    }
  }
]

This is the frontend with the autofill data! It has the correct place. For each of these URLS

image

socket-security[bot] commented 4 months ago

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/googlemaps@3.43.3 None 0 232 kB types

View full report↗︎

theyokohamalife commented 3 months ago

This looks like a really good start! My only concern is about the cost. How can we prevent being charged from fake submissions, but still pre-fill the submission fields to satisfy the submission input? Let's discuss the pros and cons with doing this on the backend or frontend if we decide to implement a button for moderators to click to fetch the place data.

NabbeunNabi commented 3 months ago

This looks like a really good start! My only concern is about the cost. How can we prevent being charged from fake submissions, but still pre-fill the submission fields to satisfy the submission input? Let's discuss the pros and cons with doing this on the backend or frontend if we decide to implement a button for moderators to click to fetch the place data.

Based on the discussions I have had with @ermish . As long as the Regex checks are working it will at least be a valid google maps url that is a location due to the redirection and the finding of the coordinates and making sure it is a google maps URL. And at this point it would be easiest to autofill on submission. If we get to the point where people start trying to submit fake URLS with bots we can implement a captcha library to prevent this.

NabbeunNabi commented 3 months ago

This looks like a really good start! My only concern is about the cost. How can we prevent being charged from fake submissions, but still pre-fill the submission fields to satisfy the submission input? Let's discuss the pros and cons with doing this on the backend or frontend if we decide to implement a button for moderators to click to fetch the place data.

Based on the discussions I have had with @ermish . As long as the Regex checks are working it will at least be a valid google maps url that is a location due to the redirection and the finding of the coordinates and making sure it is a google maps URL. And at this point it would be easiest to autofill on submission. If we get to the point where people start trying to submit fake URLS with bots we can implement a captcha library to prevent this.

I actually refactored. I think overall it would be better as a button on the moderation panel. It does take some time. So we want the submission created as quickly as possible. The moderators can then autofill the database with the added mutation.

ermish commented 1 month ago

@NabbeunNabi bump in case you missed my review

ermish commented 1 month ago

@NabbeunNabi and I talked about this. Updated user flow:

We don't want to auto-run because not all submissions will need it plus it will prevent costs on spam submissions

NabbeunNabi commented 2 weeks ago

@ermish I am going to get to this now. Due to our discussion. With it becoming a mutation where the button will exist in the future. Should I still refactor the locations. Or just fix the mutation name with some comments that help people understand the logic?

ermish commented 1 week ago

@ermish I am going to get to this now. Due to our discussion. With it becoming a mutation where the button will exist in the future. Should I still refactor the locations. Or just fix the mutation name with some comments that help people understand the logic?

if I understand, yes, it should still be moved into the service and an updated mutation