diff --git a/server/src/geolocations/geolocations.controller.ts b/server/src/geolocations/geolocations.controller.ts index 381675e..8f8586b 100644 --- a/server/src/geolocations/geolocations.controller.ts +++ b/server/src/geolocations/geolocations.controller.ts @@ -42,6 +42,20 @@ export class GeolocationsController { return paginateOutput(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination) } + /** + * Récupération des dernières posititons de tout le monde + * + * @throws {401} Non authentifié⋅e + * @throws {404} Aucune localisation envoyée + */ + @Get('/last-locations') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + async findLastLocations(): Promise { + const lastGeolocations = await this.geolocationsService.findLastLocations() + return lastGeolocations.map(geolocation => new GeolocationEntity(geolocation)) + } + /** * Recherche d'une géolocalisation par identifiant * @@ -58,22 +72,6 @@ export class GeolocationsController { return new GeolocationEntity(geolocation) } - /** - * Récupération de la dernière posititon - * - * @throws {401} Non authentifié⋅e - * @throws {404} Aucune localisation envoyée - */ - @Get('/last-location/:playerId') - @UseGuards(JwtAuthGuard) - @ApiBearerAuth() - async findLastLocation(@Param('playerId', ParseIntPipe) playerId: number): Promise { - const geolocation = await this.geolocationsService.findLastLocation(playerId) - if (!geolocation) - throw new NotFoundException(`Géolocalisation inexistante pour læ joueur⋅se ${playerId}`) - return new GeolocationEntity(geolocation) - } - /** * Suppression d'une localisation * diff --git a/server/src/geolocations/geolocations.service.ts b/server/src/geolocations/geolocations.service.ts index 3cde2d4..d402162 100644 --- a/server/src/geolocations/geolocations.service.ts +++ b/server/src/geolocations/geolocations.service.ts @@ -33,15 +33,21 @@ export class GeolocationsService { return await this.prisma.geolocation.findUnique({ where: { id } }) } - async findLastLocation(playerId: number): Promise { - return await this.prisma.geolocation.findFirst({ - where: { playerId: playerId }, - orderBy: { timestamp: Prisma.SortOrder.desc }, - take: 1 - }) + async findLastLocations(): Promise { + const players = await this.prisma.player.findMany() + const lastGeolocations = [] + for (const player of players) { + const lastGeolocationPlayer = await this.prisma.geolocation.findFirst({ + where: { playerId: player.id }, + orderBy: { timestamp: 'desc' }, + take: 1, + }) + if (lastGeolocationPlayer) + lastGeolocations.push(lastGeolocationPlayer) + } + return lastGeolocations } - async remove(id: number): Promise { if (!this.findOne(id)) throw new NotFoundException(`Aucune géolocalisation n'existe avec l'identifiant ${id}`)