|
| 1 | +import { MPHelper } from "@/lib/providers/ministry-platform"; |
1 | 2 | import { GooglePlacesProvider } from "@/lib/providers/google-places"; |
2 | 3 | import type { PlacePrediction, PlaceDetails } from "@/lib/providers/google-places"; |
3 | 4 |
|
4 | 5 | export class GooglePlacesService { |
5 | 6 | private static instance: GooglePlacesService; |
| 7 | + private mp: MPHelper | null = null; |
6 | 8 | private provider: GooglePlacesProvider | null = null; |
| 9 | + // undefined = not yet resolved; null = resolved with no key (feature disabled) |
| 10 | + private resolvedKey: string | null | undefined = undefined; |
7 | 11 |
|
8 | 12 | private constructor() {} |
9 | 13 |
|
10 | 14 | public static async getInstance(): Promise<GooglePlacesService> { |
11 | 15 | if (!GooglePlacesService.instance) { |
12 | 16 | GooglePlacesService.instance = new GooglePlacesService(); |
| 17 | + GooglePlacesService.instance.mp = new MPHelper(); |
13 | 18 | } |
14 | 19 | return GooglePlacesService.instance; |
15 | 20 | } |
16 | 21 |
|
17 | | - public isEnabled(): boolean { |
18 | | - return Boolean(process.env.GOOGLE_PLACES_API_KEY); |
| 22 | + /** |
| 23 | + * Resolves the Google Places API key with this precedence: |
| 24 | + * 1. MP dp_Configuration_Settings (Application_Code='COMMON', Key_Name='GoogleMapsAPIKey') |
| 25 | + * 2. GOOGLE_PLACES_API_KEY environment variable |
| 26 | + * 3. null (feature disabled) |
| 27 | + * |
| 28 | + * Cached on the singleton so MP is queried at most once per process lifetime. |
| 29 | + */ |
| 30 | + private async resolveApiKey(): Promise<string | null> { |
| 31 | + if (this.resolvedKey !== undefined) return this.resolvedKey; |
| 32 | + |
| 33 | + try { |
| 34 | + const rows = await this.mp!.getTableRecords<{ Value: string | null }>({ |
| 35 | + table: "dp_Configuration_Settings", |
| 36 | + select: "Value", |
| 37 | + filter: "Application_Code='COMMON' AND Key_Name='GoogleMapsAPIKey'", |
| 38 | + top: 1, |
| 39 | + }); |
| 40 | + const mpKey = rows[0]?.Value?.trim(); |
| 41 | + if (mpKey) { |
| 42 | + this.resolvedKey = mpKey; |
| 43 | + return this.resolvedKey; |
| 44 | + } |
| 45 | + } catch { |
| 46 | + // Swallow lookup failures (e.g. table permissions) and fall through to env var. |
| 47 | + } |
| 48 | + |
| 49 | + const envKey = process.env.GOOGLE_PLACES_API_KEY?.trim(); |
| 50 | + this.resolvedKey = envKey ? envKey : null; |
| 51 | + return this.resolvedKey; |
| 52 | + } |
| 53 | + |
| 54 | + public async isEnabled(): Promise<boolean> { |
| 55 | + return (await this.resolveApiKey()) !== null; |
19 | 56 | } |
20 | 57 |
|
21 | | - private getProvider(): GooglePlacesProvider { |
| 58 | + private async getProvider(): Promise<GooglePlacesProvider> { |
22 | 59 | if (this.provider) return this.provider; |
23 | | - const apiKey = process.env.GOOGLE_PLACES_API_KEY; |
| 60 | + const apiKey = await this.resolveApiKey(); |
24 | 61 | if (!apiKey) { |
25 | 62 | throw new Error( |
26 | | - "GOOGLE_PLACES_API_KEY is not configured. Add it to .env.local to enable address autocomplete.", |
| 63 | + "Google Places API key is not configured. Set the 'GoogleMapsAPIKey' setting in MinistryPlatform (Application_Code='COMMON') or define GOOGLE_PLACES_API_KEY in .env.local.", |
27 | 64 | ); |
28 | 65 | } |
29 | 66 | this.provider = new GooglePlacesProvider(apiKey); |
30 | 67 | return this.provider; |
31 | 68 | } |
32 | 69 |
|
33 | 70 | async autocomplete(input: string, sessionToken: string): Promise<PlacePrediction[]> { |
34 | | - return this.getProvider().autocomplete(input, sessionToken); |
| 71 | + const provider = await this.getProvider(); |
| 72 | + return provider.autocomplete(input, sessionToken); |
35 | 73 | } |
36 | 74 |
|
37 | 75 | async getPlaceDetails(placeId: string, sessionToken: string): Promise<PlaceDetails> { |
38 | | - return this.getProvider().getPlaceDetails(placeId, sessionToken); |
| 76 | + const provider = await this.getProvider(); |
| 77 | + return provider.getPlaceDetails(placeId, sessionToken); |
39 | 78 | } |
40 | 79 | } |
0 commit comments