Correction de l'endpoint qui récupère les dernières géolocalisations

This commit is contained in:
Emmy D'Anello 2024-12-08 23:19:56 +01:00
parent 20b4f2e7e8
commit a0fd6ca6ab
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 27 additions and 23 deletions

View File

@ -42,6 +42,20 @@ export class GeolocationsController {
return paginateOutput<GeolocationEntity>(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<GeolocationEntity[]> {
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<GeolocationEntity> {
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
*

View File

@ -33,15 +33,21 @@ export class GeolocationsService {
return await this.prisma.geolocation.findUnique({ where: { id } })
}
async findLastLocation(playerId: number): Promise<Geolocation> {
return await this.prisma.geolocation.findFirst({
where: { playerId: playerId },
orderBy: { timestamp: Prisma.SortOrder.desc },
take: 1
})
async findLastLocations(): Promise<Geolocation[]> {
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<Geolocation> {
if (!this.findOne(id))
throw new NotFoundException(`Aucune géolocalisation n'existe avec l'identifiant ${id}`)