Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MAPBOX_TOKEN=
BDC_KEY=
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { LoggerMiddleware } from './logger/logger.middleware';
import { PrayerTimesModule } from '@/prayer-times/prayer-times.module';
import { ResponseInterceptor } from '@/response/response.interceptor';
import { HealthModule } from './health/health.module';
import { ReverseGeocodeModule } from './reverse-geocode/reverse-geocode.module';

@Module({
imports: [
Expand Down Expand Up @@ -60,6 +61,7 @@ import { HealthModule } from './health/health.module';
}),
PrayerTimesModule,
HealthModule,
ReverseGeocodeModule,
],
controllers: [
AppController,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export interface BDCReverseGeocodeTimezoneResponse {
latitude: number;
longitude: number;
localityLanguageRequested: string;
continent: string;
continentCode: string;
countryName: string;
countryCode: string;
principalSubdivision: string;
principalSubdivisionCode: string;
city: string;
locality: string;
postcode: string;
plusCode: string;
localityInfo: {
administrative: Array<{
name: string;
description: string;
isoName?: string;
order: number;
adminLevel: number;
isoCode?: string;
wikidataId?: string;
geonameId?: number;
}>;
informative: Array<{
name: string;
description: string;
isoName?: string;
order: number;
isoCode?: string;
wikidataId?: string;
geonameId?: number;
}>;
};
timeZone: {
ianaTimeId: string;
displayName: string;
effectiveTimeZoneFull: string;
effectiveTimeZoneShort: string;
utcOffsetSeconds: number;
utcOffset: string;
isDaylightSavingTime: boolean;
localTime: string;
};
}

export interface ReverseGeocodeResponse {
countryName: string;
city: string;
timezone: string;
}
17 changes: 17 additions & 0 deletions src/reverse-geocode/reverse-geocode.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get, Query, Version } from '@nestjs/common';
import { ReverseGeocodeService } from '@/reverse-geocode/reverse-geocode.service';
import { ReverseGeocodeDto } from '@/reverse-geocode/reverse-geocode.dto';
import { Throttle } from '@nestjs/throttler';

@Controller('reverse-geocode')
export class ReverseGeocodeController {
constructor(private readonly geocodingService: ReverseGeocodeService) {}

// Allow only three request every 15m
@Throttle({ default: { limit: 3, ttl: 1000 * 60 * 15 } })
@Get()
@Version('2')
getTimezone(@Query() query: ReverseGeocodeDto) {
return this.geocodingService.reverseGeocodeWithTimezone(query);
}
}
18 changes: 18 additions & 0 deletions src/reverse-geocode/reverse-geocode.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Type } from 'class-transformer';
import { IsLatitude, IsLocale, IsLongitude, IsNotEmpty } from 'class-validator';

export class ReverseGeocodeDto {
@IsNotEmpty()
@IsLatitude()
@Type(() => Number)
latitude: number;

@IsNotEmpty()
@IsLongitude()
@Type(() => Number)
longitude: number;

@IsNotEmpty()
@IsLocale()
locale: string;
}
11 changes: 11 additions & 0 deletions src/reverse-geocode/reverse-geocode.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { ReverseGeocodeController } from '@/reverse-geocode/reverse-geocode.controller';
import { ReverseGeocodeService } from '@/reverse-geocode/reverse-geocode.service';
import { HttpModule } from '@nestjs/axios';

@Module({
imports: [HttpModule],
controllers: [ReverseGeocodeController],
providers: [ReverseGeocodeService],
})
export class ReverseGeocodeModule {}
46 changes: 46 additions & 0 deletions src/reverse-geocode/reverse-geocode.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Injectable } from '@nestjs/common';
import { ReverseGeocodeDto } from '@/reverse-geocode/reverse-geocode.dto';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';

// interfaces
import {
BDCReverseGeocodeTimezoneResponse,
ReverseGeocodeResponse,
} from '@/reverse-geocode/interfaces/reverse-geocode-response.interface';

@Injectable()
export class ReverseGeocodeService {
private url = 'https://api-bdc.net';
private apiKey = process.env.BDC_KEY;

constructor(private http: HttpService) {}
async reverseGeocodeWithTimezone(
query: ReverseGeocodeDto,
): Promise<ReverseGeocodeResponse> {
const latitude = query.latitude;
const longitude = query.longitude;
const locale = query.locale;

const endpoint = `/data/reverse-geocode-with-timezone`;

const response = await firstValueFrom(
this.http.get(`${this.url}${endpoint}`, {
params: {
latitude,
longitude,
localityLanguage: locale,
key: this.apiKey,
},
}),
);

const data = response.data as BDCReverseGeocodeTimezoneResponse;

return {
countryName: data.countryName,
city: data.city,
timezone: data.timeZone.ianaTimeId,
};
}
}