find territories for a point
This commit is contained in:
		
							parent
							
								
									a743fefe94
								
							
						
					
					
						commit
						1da111bba9
					
				| 
						 | 
				
			
			@ -23,6 +23,7 @@
 | 
			
		|||
        "@nestjs/microservices": "^9.3.2",
 | 
			
		||||
        "@nestjs/platform-express": "^9.0.0",
 | 
			
		||||
        "@prisma/client": "^4.9.0",
 | 
			
		||||
        "cache-manager": "^5.1.5",
 | 
			
		||||
        "cache-manager-ioredis-yet": "^1.1.0",
 | 
			
		||||
        "class-transformer": "^0.5.1",
 | 
			
		||||
        "class-validator": "^0.14.0",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -41,6 +41,7 @@
 | 
			
		|||
    "@nestjs/microservices": "^9.3.2",
 | 
			
		||||
    "@nestjs/platform-express": "^9.0.0",
 | 
			
		||||
    "@prisma/client": "^4.9.0",
 | 
			
		||||
    "cache-manager": "^5.1.5",
 | 
			
		||||
    "cache-manager-ioredis-yet": "^1.1.0",
 | 
			
		||||
    "class-transformer": "^0.5.1",
 | 
			
		||||
    "class-validator": "^0.14.0",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
import { Injectable } from '@nestjs/common';
 | 
			
		||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
 | 
			
		||||
import { Point } from '../../domain/point.type';
 | 
			
		||||
import { DatabaseException } from '../../exceptions/database.exception';
 | 
			
		||||
import { ICollection } from '../../interfaces/collection.interface';
 | 
			
		||||
import { IRepository } from '../../interfaces/repository.interface';
 | 
			
		||||
| 
						 | 
				
			
			@ -182,4 +183,13 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
 | 
			
		|||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  async findForPoint(point: Point): Promise<ICollection<T>> {
 | 
			
		||||
    const strPoint = `SELECT uuid, name FROM ${this._model} WHERE ST_Intersects('POINT(${point.lon} ${point.lat})',shape) = true`;
 | 
			
		||||
    const territories: Array<T> = await this._prisma.$queryRawUnsafe(strPoint);
 | 
			
		||||
    return Promise.resolve({
 | 
			
		||||
      data: territories,
 | 
			
		||||
      total: territories.length,
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,4 @@
 | 
			
		|||
export type Point = {
 | 
			
		||||
  lon: number;
 | 
			
		||||
  lat: number;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +32,9 @@ export class TerritoriesController {
 | 
			
		|||
  @GrpcMethod('TerritoriesService', 'FindForPoint')
 | 
			
		||||
  @UseInterceptors(CacheInterceptor)
 | 
			
		||||
  @CacheKey('TerritoriesServiceFindForPoint')
 | 
			
		||||
  async findAll(data: FindForPointRequest): Promise<ICollection<Territory>> {
 | 
			
		||||
  async findForPoint(
 | 
			
		||||
    data: FindForPointRequest,
 | 
			
		||||
  ): Promise<ICollection<Territory>> {
 | 
			
		||||
    const territoryCollection = await this._queryBus.execute(
 | 
			
		||||
      new FindForPointQuery(data),
 | 
			
		||||
    );
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,6 +13,7 @@ message Point {
 | 
			
		|||
 | 
			
		||||
message Territories {
 | 
			
		||||
  repeated Territory data = 1;
 | 
			
		||||
  int32 total = 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
message Territory {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
export class Point {
 | 
			
		||||
  lon: number;
 | 
			
		||||
  lat: number;
 | 
			
		||||
 | 
			
		||||
  constructor(lon: number, lat: number) {
 | 
			
		||||
    this.lon = lon;
 | 
			
		||||
    this.lat = lat;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ import { ICollection } from 'src/modules/database/src/interfaces/collection.inte
 | 
			
		|||
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
 | 
			
		||||
import { FindForPointQuery } from '../../queries/find-for-point.query';
 | 
			
		||||
import { Territory } from '../entities/territory';
 | 
			
		||||
import { Point } from '../entities/point';
 | 
			
		||||
 | 
			
		||||
@QueryHandler(FindForPointQuery)
 | 
			
		||||
export class FindForPointUseCase {
 | 
			
		||||
| 
						 | 
				
			
			@ -11,9 +12,8 @@ export class FindForPointUseCase {
 | 
			
		|||
  async execute(
 | 
			
		||||
    findForPointQuery: FindForPointQuery,
 | 
			
		||||
  ): Promise<ICollection<Territory>> {
 | 
			
		||||
    return this._repository.findAll(1, 999999, {
 | 
			
		||||
      lon: findForPointQuery.lon,
 | 
			
		||||
      lat: findForPointQuery.lat,
 | 
			
		||||
    });
 | 
			
		||||
    return this._repository.findForPoint(
 | 
			
		||||
      new Point(findForPointQuery.point.lon, findForPointQuery.point.lat),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,10 @@
 | 
			
		|||
import { FindForPointRequest } from '../domain/dtos/find-for-point.request';
 | 
			
		||||
import { Point } from '../domain/entities/point';
 | 
			
		||||
 | 
			
		||||
export class FindForPointQuery {
 | 
			
		||||
  lon: number;
 | 
			
		||||
  lat: number;
 | 
			
		||||
  point: Point;
 | 
			
		||||
 | 
			
		||||
  constructor(findForPointRequest?: FindForPointRequest) {
 | 
			
		||||
    this.lon = findForPointRequest.lon;
 | 
			
		||||
    this.lat = findForPointRequest.lat;
 | 
			
		||||
    this.point = new Point(findForPointRequest.lon, findForPointRequest.lat);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,7 +25,7 @@ const mockTerritories = [
 | 
			
		|||
 | 
			
		||||
const mockTerritoriesRepository = {
 | 
			
		||||
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
  findAll: jest.fn().mockImplementation((query?: FindForPointQuery) => {
 | 
			
		||||
  findForPoint: jest.fn().mockImplementation((query?: FindForPointQuery) => {
 | 
			
		||||
    return Promise.resolve(mockTerritories);
 | 
			
		||||
  }),
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue