2026-03-07 11:25:11 +03:30
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-03-11 20:42:34 +03:30
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import { CreateUserDto, UpdateUserDto } from './dto/user.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class AdminUsersService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
async findAll() {
|
|
|
|
|
const [users, count] = await this.prisma.$transaction([
|
2026-03-16 00:33:40 +03:30
|
|
|
this.prisma.admin.findMany(),
|
|
|
|
|
this.prisma.admin.count(),
|
2026-03-07 11:25:11 +03:30
|
|
|
])
|
|
|
|
|
return ResponseMapper.paginate(
|
|
|
|
|
users.map(user => ({ ...user, fullname: `${user.first_name} ${user.last_name}` })),
|
|
|
|
|
{ count },
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(id: string) {
|
2026-03-16 00:33:40 +03:30
|
|
|
const user = await this.prisma.admin.findUnique({
|
2026-03-07 11:25:11 +03:30
|
|
|
where: { id },
|
|
|
|
|
})
|
2026-03-11 20:42:34 +03:30
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException('کاربری یافت نشد')
|
|
|
|
|
}
|
2026-03-07 11:25:11 +03:30
|
|
|
return ResponseMapper.single({
|
|
|
|
|
...user,
|
|
|
|
|
fullname: `${user?.first_name} ${user?.last_name}`,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(data: CreateUserDto) {
|
2026-03-16 00:33:40 +03:30
|
|
|
return this.prisma.admin.create({ data })
|
2026-03-07 11:25:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(id: string, data: UpdateUserDto) {
|
2026-03-16 00:33:40 +03:30
|
|
|
return this.prisma.admin.update({ where: { id }, data })
|
2026-03-07 11:25:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(id: string) {
|
2026-03-16 00:33:40 +03:30
|
|
|
return this.prisma.admin.delete({ where: { id } })
|
2026-03-07 11:25:11 +03:30
|
|
|
}
|
|
|
|
|
}
|