48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { Injectable } from '@nestjs/common'
|
|
import { CreateGeolocationDto } from './dto/create-geolocation.dto'
|
|
import { PrismaService } from 'src/prisma/prisma.service'
|
|
import { Geolocation, User } from '@prisma/client'
|
|
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
|
import { paginate } from 'src/common/utils/pagination.utils'
|
|
import { UserFilterDto } from 'src/common/dto/user_filter.dto'
|
|
|
|
@Injectable()
|
|
export class GeolocationsService {
|
|
constructor(private prisma: PrismaService) { }
|
|
|
|
async create(authenticatedUser: User, createGeolocationDto: CreateGeolocationDto): Promise<Geolocation> {
|
|
const data = { ...createGeolocationDto, userId: authenticatedUser.id }
|
|
return await this.prisma.geolocation.create({ data: data })
|
|
}
|
|
|
|
async findAll(queryPagination?: QueryPaginationDto, userFilter?: UserFilterDto): Promise<[Geolocation[], number]> {
|
|
const filter = {
|
|
where: userFilter?.userId ? { userId: userFilter.userId } : {}
|
|
}
|
|
return [
|
|
await this.prisma.geolocation.findMany({
|
|
...filter,
|
|
...paginate(queryPagination),
|
|
}),
|
|
await this.prisma.geolocation.count(filter),
|
|
]
|
|
}
|
|
|
|
async findOne(id: number): Promise<Geolocation> {
|
|
return await this.prisma.geolocation.findUnique({ where: { id } })
|
|
}
|
|
|
|
async findLastLocation(userId: number): Promise<Geolocation> {
|
|
return await this.prisma.geolocation.findFirst({
|
|
where: { userId: userId },
|
|
orderBy: { timestamp: "desc" },
|
|
take: 1
|
|
})
|
|
}
|
|
|
|
|
|
async remove(id: number): Promise<Geolocation> {
|
|
return await this.prisma.geolocation.delete({ where: { id } })
|
|
}
|
|
}
|