Skip to content

Commit

Permalink
Merge pull request #55 from connectattoo/feat/add-verification-to-sta…
Browse files Browse the repository at this point in the history
…tus-location

CT-162/CT-194
  • Loading branch information
natanaelsc authored Sep 16, 2024
2 parents 7b4f86d + bcd0e5c commit dd36aab
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.18.2
v20.12.1
5 changes: 3 additions & 2 deletions src/modules/user/interfaces/address-coordinates.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Nullable } from '../../../shared/interface/nullable.type';
import { IAddress } from './address.interface';

export interface IAddressCoordinates extends IAddress {
latitude: number;
longitude: number;
latitude: Nullable<number>;
longitude: Nullable<number>;
}
14 changes: 11 additions & 3 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IGetConfirmed } from './interfaces/get-confirmed.interface';
import { IGetUserAndProfileByEmail } from './interfaces/get-user-profile-by-email.interface';
import { IAddressCoordinates } from './interfaces/address-coordinates.interface';
import { MapsService } from '../../shared/adapters/maps/maps.service';
import { IGeocode } from '../../shared/adapters/maps/interface/geocode.interface';

@Injectable()
export class UserService {
Expand Down Expand Up @@ -53,12 +54,19 @@ export class UserService {
}

async createArtist(userId: string, address: IAddress): Promise<void> {
const { geometry } = await this.mapsService.geocode(address);
let geocode: IGeocode | null = {
address: null,
geometry: { lat: null, lng: null },
};

if (process.env.GOOGLE_MAPS_API_KEY) {
geocode = await this.mapsService.geocode(address);
}

const addressCoordinates: IAddressCoordinates = {
...address,
latitude: geometry.lat,
longitude: geometry.lng,
latitude: geocode?.geometry?.lat,
longitude: geocode?.geometry?.lng,
};

await this.userRepository.createArtist(userId, addressCoordinates);
Expand Down
4 changes: 4 additions & 0 deletions src/shared/adapters/maps/interface/geocode.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IGeocode {
address: null;
geometry: { lat: null; lng: null };
}
29 changes: 25 additions & 4 deletions src/shared/adapters/maps/maps.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import {
Client,
DistanceMatrixRequest,
GeocodeRequest,
Status,
TravelMode,
UnitSystem,
} from '@googlemaps/google-maps-services-js';
import { IDistanceMatrix } from './interface/distance-matrix.interface';
import { IAddress } from '../../../modules/user/interfaces/address.interface';
import { IGeocode } from './interface/geocode.interface';

@Injectable()
export class MapsService {
constructor(private mapsClient: Client) {}

async geocode(address: IAddress) {
async geocode(address: IAddress): Promise<IGeocode> {
const parsedAddress = `${address.street}, ${address.number} - ${address.city} - ${address.state}, ${address.zipCode}, ${address.country}`;

const geo = await this.mapsClient.geocode({
Expand All @@ -22,9 +24,28 @@ export class MapsService {
},
} as GeocodeRequest);

const statusObject = {
[Status.ZERO_RESULTS]: new BadRequestException('Invalid address'),

[Status.NOT_FOUND]: new BadRequestException('Invalid address'),

[Status.OK]: {
address: geo.data.results[0].formatted_address,
geometry: geo.data.results[0].geometry.location,
},
};

const statusObjectValue = statusObject[geo.data.status];
if (statusObjectValue) {
if (statusObjectValue instanceof BadRequestException)
throw statusObjectValue;

return statusObjectValue;
}

return {
address: geo.data.results[0].formatted_address,
geometry: geo.data.results[0].geometry.location,
address: null,
geometry: { lat: null, lng: null },
};
}

Expand Down

0 comments on commit dd36aab

Please sign in to comment.