diff --git a/server/src/geolocations/geolocations.controller.ts b/server/src/geolocations/geolocations.controller.ts index 0b6dfcf..2eefed2 100644 --- a/server/src/geolocations/geolocations.controller.ts +++ b/server/src/geolocations/geolocations.controller.ts @@ -47,12 +47,26 @@ export class GeolocationsController { @ApiForbiddenResponse({ description: "Permission refusée" }) @ApiNotFoundResponse({ description: "Objet non trouvé" }) async findOne(@Param('id', ParseIntPipe) id: number): Promise { - const geolocation = await this.geolocationsService.findOne(+id) + const geolocation = await this.geolocationsService.findOne(id) if (!geolocation) throw new NotFoundException(`Géolocalisation inexistante avec l'identifiant ${id}`) return new GeolocationEntity(geolocation) } + @Get('/last-location/:userId') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOkResponse({ type: GeolocationEntity }) + @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) + @ApiForbiddenResponse({ description: "Permission refusée" }) + @ApiNotFoundResponse({ description: "Aucune localisation trouvée" }) + async findLastLocation(@Param('userId', ParseIntPipe) userId: number): Promise { + const geolocation = await this.geolocationsService.findLastLocation(userId) + if (!geolocation) + throw new NotFoundException(`Géolocalisation inexistante pour l'utilisateur⋅rice ${userId}`) + return new GeolocationEntity(geolocation) + } + @Delete(':id') @HttpCode(204) @UseGuards(JwtAuthGuard) diff --git a/server/src/geolocations/geolocations.service.ts b/server/src/geolocations/geolocations.service.ts index ee9b7c4..9757a95 100644 --- a/server/src/geolocations/geolocations.service.ts +++ b/server/src/geolocations/geolocations.service.ts @@ -32,6 +32,14 @@ export class GeolocationsService { return await this.prisma.geolocation.findUnique({ where: { id } }) } + async findLastLocation(userId: number): Promise { + return await this.prisma.geolocation.findFirst({ + where: { userId: userId }, + orderBy: { timestamp: "desc" }, + take: 1 + }) + } + async remove(id: number): Promise { return await this.prisma.geolocation.delete({ where: { id } })