find territories for a point

This commit is contained in:
sbriat
2023-02-06 16:12:40 +01:00
parent a743fefe94
commit 1da111bba9
10 changed files with 37 additions and 10 deletions

View File

@@ -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,
});
}
}

View File

@@ -0,0 +1,4 @@
export type Point = {
lon: number;
lat: number;
};