Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Store geolocation in approved subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 30, 2020
1 parent d0bb693 commit d989922
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/modules/approved-subnets/approved-subnets.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { GeolocationModule } from '../geolocation/geolocation.module';
import { PrismaModule } from '../prisma/prisma.module';
import { ApprovedSubnetController } from './approved-subnets.controller';
import { ApprovedSubnetsService } from './approved-subnets.service';

@Module({
imports: [PrismaModule, ConfigModule],
imports: [PrismaModule, ConfigModule, GeolocationModule],
controllers: [ApprovedSubnetController],
providers: [ApprovedSubnetsService],
})
Expand Down
7 changes: 7 additions & 0 deletions src/modules/approved-subnets/approved-subnets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import { PrismaService } from '../prisma/prisma.service';
import anonymize from 'ip-anonymize';
import { hash } from 'bcrypt';
import { ConfigService } from '@nestjs/config';
import { GeolocationService } from '../geolocation/geolocation.service';

@Injectable()
export class ApprovedSubnetsService {
constructor(
private prisma: PrismaService,
private configService: ConfigService,
private geolocationService: GeolocationService,
) {}

async getApprovedSubnets(
Expand Down Expand Up @@ -82,10 +84,15 @@ export class ApprovedSubnetsService {
anonymize(ipAddress),
this.configService.get<number>('security.saltRounds'),
);
const location = await this.geolocationService.getLocation(ipAddress);
const approved = await this.prisma.approvedSubnets.create({
data: {
user: { connect: { id: userId } },
subnet,
city: location?.city?.names?.en,
region: location?.subdivisions.pop()?.names?.en,
timezone: location?.location?.time_zone,
countryCode: location?.country?.iso_code,
createdAt: new Date(),
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/modules/geolocation/geolocation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ConfigService } from '@nestjs/config';
export class GeolocationService implements OnModuleDestroy {
constructor(private configService: ConfigService) {}

lookup: Reader<CityResponse> | null = null;
lru = new QuickLRU<string, Partial<CityResponse>>({
private lookup: Reader<CityResponse> | null = null;
private lru = new QuickLRU<string, Partial<CityResponse>>({
maxSize: this.configService.get<number>('caching.geolocationLruSize'),
});

Expand Down

0 comments on commit d989922

Please sign in to comment.