From ce1c28d3e1c529abdb48648a87b214f7f3ef29df Mon Sep 17 00:00:00 2001 From: DhanshCOSQ Date: Tue, 8 Apr 2025 16:01:36 +0530 Subject: [PATCH] added google api. --- functions/.env.example | 1 + functions/src/index.ts | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/functions/.env.example b/functions/.env.example index 381bfad..7f1bf60 100644 --- a/functions/.env.example +++ b/functions/.env.example @@ -7,4 +7,5 @@ TWILIO_PHONE_NUMBER=#{TWILIO_PHONE_NUMBER}# SERVICES_RGN=#{SERVICES_RGN}# CASHFREE_CLIENT_ID=#{CASHFREE_CLIENT_ID}# CASHFREE_CLIENT_SECRET=#{CASHFREE_CLIENT_SECRET}# +GOOGLE_PLACES_API_KEY=#{GOOGLE_PLACES_API_KEY}# diff --git a/functions/src/index.ts b/functions/src/index.ts index 4c98cab..b369f00 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -410,3 +410,61 @@ export const verifyCashfreePayment = onRequest({ } }); }); + +export const getPlacesAutocomplete = onRequest({ + region: '#{SERVICES_RGN}#' +}, async (request: Request, response: express.Response) => { + return corsHandler(request, response, async () => { + try { + const { input, location, radius, types, components, sessiontoken } = request.query; + + if (!input) { + response.status(400).json({ + error: 'Input parameter is required for autocomplete' + }); + return; + } + + const apiKey = process.env.GOOGLE_PLACES_API_KEY; + if (!apiKey) { + logger.error('Google Places API key is not configured'); + response.status(500).json({ error: 'Server configuration error' }); + return; + } + + const url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json'; + const params: any = { + key: apiKey, + input: input + }; + + if (location && radius) { + params.location = location; + params.radius = radius; + } + + if (types) { + params.types = types; + } + + if (components) { + params.components = components; + } + + if (sessiontoken) { + params.sessiontoken = sessiontoken; + } + + const result = await axios.get(url, { params }); + + logger.info('Google Places Autocomplete API request completed successfully'); + response.json(result.data); + } catch (error) { + logger.error('Error fetching place autocomplete suggestions:', error); + response.status(500).json({ + success: false, + error: error instanceof Error ? error.message : String(error) + }); + } + }); +});