From 9d3b88a5d0b3e6f6a926466df992110629e0e0da Mon Sep 17 00:00:00 2001 From: Abraham <1001.28021547.ucla@gmail.com> Date: Sun, 11 May 2025 22:08:45 -0400 Subject: [PATCH 1/3] add migration to enable PostGIS extension --- ...14730112-enable-postgis-extension-migration.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/database/migrations/1747014730112-enable-postgis-extension-migration.ts diff --git a/src/database/migrations/1747014730112-enable-postgis-extension-migration.ts b/src/database/migrations/1747014730112-enable-postgis-extension-migration.ts new file mode 100644 index 0000000..3df47ae --- /dev/null +++ b/src/database/migrations/1747014730112-enable-postgis-extension-migration.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class EnablePostgisExtensionMigration1747014730112 + implements MigrationInterface +{ + name = 'EnablePostgisExtensionMigration1747014730112'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS postgis`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP EXTENSION IF EXISTS postgis`); + } +} From 8e9a4d11316f1d7ab8d0c984c9e1e922154e69e2 Mon Sep 17 00:00:00 2001 From: Abraham <1001.28021547.ucla@gmail.com> Date: Sun, 11 May 2025 22:53:36 -0400 Subject: [PATCH 2/3] assign nearest branch on delivery using PostGIS --- src/branch/branch.service.ts | 16 ++++++++++++++++ src/order/order.service.ts | 8 +++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/branch/branch.service.ts b/src/branch/branch.service.ts index df8d5c0..d496727 100644 --- a/src/branch/branch.service.ts +++ b/src/branch/branch.service.ts @@ -93,4 +93,20 @@ export class BranchService { }); return deleted.affected === 1; } + + async findNearestBranch(lat: number, lng: number): Promise { + const branch = await this.branchRepository + .createQueryBuilder('branch') + .orderBy( + `ST_DistanceSphere(ST_MakePoint(branch.longitude, branch.latitude), ST_MakePoint(:lng, :lat))`, + ) + .setParameters({ lat, lng }) + .getOne(); + + if (!branch) { + throw new NotFoundException('No nearest branch found'); + } + + return branch; + } } diff --git a/src/order/order.service.ts b/src/order/order.service.ts index 868bc6a..da50598 100644 --- a/src/order/order.service.ts +++ b/src/order/order.service.ts @@ -66,13 +66,15 @@ export class OrderService { 'User address ID is required for delivery orders', ); } - // TODO: Find the closest branch to the address - const branches = await this.branchService.findAll(1, 10); - branch = branches[0]; userAddress = await this.userService.getAddress( user.id, createOrderDTO.userAddressId, ); + const nearestBranch = await this.branchService.findNearestBranch( + userAddress.latitude, + userAddress.longitude, + ); + branch = nearestBranch; } else { throw new BadRequestException('Invalid order type'); } From 86450b1c366ef7f49c6728fdf48065e2e5ecc007 Mon Sep 17 00:00:00 2001 From: Abraham <1001.28021547.ucla@gmail.com> Date: Sun, 11 May 2025 23:12:34 -0400 Subject: [PATCH 3/3] fix NotFoundException message --- src/branch/branch.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/branch/branch.service.ts b/src/branch/branch.service.ts index d496727..0fd9f56 100644 --- a/src/branch/branch.service.ts +++ b/src/branch/branch.service.ts @@ -104,7 +104,7 @@ export class BranchService { .getOne(); if (!branch) { - throw new NotFoundException('No nearest branch found'); + throw new NotFoundException('No closer branch found'); } return branch;