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
16 changes: 16 additions & 0 deletions src/branch/branch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,20 @@ export class BranchService {
});
return deleted.affected === 1;
}

async findNearestBranch(lat: number, lng: number): Promise<Branch> {
const branch = await this.branchRepository
.createQueryBuilder('branch')
.orderBy(
`ST_DistanceSphere(ST_MakePoint(branch.longitude, branch.latitude), ST_MakePoint(:lng, :lat))`,
)
.setParameters({ lat, lng })
Comment thread
andres15alvarez marked this conversation as resolved.
.getOne();

if (!branch) {
throw new NotFoundException('No closer branch found');
}

return branch;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class EnablePostgisExtensionMigration1747014730112
implements MigrationInterface
{
name = 'EnablePostgisExtensionMigration1747014730112';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS postgis`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP EXTENSION IF EXISTS postgis`);
}
}
8 changes: 5 additions & 3 deletions src/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Loading