Spaces:
Sleeping
Sleeping
| import prisma from '../utils/db'; | |
| import bcrypt from 'bcryptjs'; | |
| import { catatAudit } from '../utils/audit'; | |
| import { APIError } from '../utils/errors'; | |
| export class PenggunaService { | |
| static async getUsers(skip = 0, limit = 10, aktif?: number) { | |
| const where: any = {}; | |
| if (aktif !== undefined) { | |
| where.aktif = aktif; | |
| } | |
| return await prisma.user.findMany({ | |
| where, | |
| skip, | |
| take: limit, | |
| }); | |
| } | |
| static async getUserById(userId: number) { | |
| const user = await prisma.user.findUnique({ | |
| where: { id: userId }, | |
| }); | |
| if (!user) { | |
| throw new APIError('Pengguna tidak ditemukan', 404); | |
| } | |
| return user; | |
| } | |
| static async create(data: any, actorId: number, ip: string, userAgent: string) { | |
| const existing = await prisma.user.findUnique({ | |
| where: { username: data.username }, | |
| }); | |
| if (existing) { | |
| throw new APIError('Username sudah digunakan', 400); | |
| } | |
| const hashedPassword = await bcrypt.hash(data.password, 10); | |
| const user = await prisma.user.create({ | |
| data: { | |
| nama: data.nama, | |
| username: data.username, | |
| password_hash: hashedPassword, | |
| role: data.role, | |
| nomor_hp: data.nomor_hp, | |
| foto_url: data.foto_url, | |
| aktif: data.aktif ?? 1, | |
| }, | |
| }); | |
| await catatAudit({ | |
| userId: actorId, | |
| aksi: 'create', | |
| entitas: 'user', | |
| entitasId: user.id, | |
| dataBaru: { username: user.username, nama: user.nama, role: user.role }, | |
| ipAddress: ip, | |
| userAgent, | |
| }); | |
| return user; | |
| } | |
| static async update(userId: number, data: any, actorId: number, ip: string, userAgent: string) { | |
| const user = await this.getUserById(userId); | |
| const dataLama = { | |
| nama: user.nama, | |
| role: user.role, | |
| nomor_hp: user.nomor_hp, | |
| foto_url: user.foto_url, | |
| aktif: user.aktif, | |
| }; | |
| const updateData: any = {}; | |
| if (data.nama !== undefined) updateData.nama = data.nama; | |
| if (data.role !== undefined) updateData.role = data.role; | |
| if (data.nomor_hp !== undefined) updateData.nomor_hp = data.nomor_hp; | |
| if (data.foto_url !== undefined) updateData.foto_url = data.foto_url; | |
| if (data.aktif !== undefined) updateData.aktif = data.aktif; | |
| if (data.password !== undefined) { | |
| updateData.password_hash = await bcrypt.hash(data.password, 10); | |
| } | |
| const updatedUser = await prisma.user.update({ | |
| where: { id: userId }, | |
| data: updateData, | |
| }); | |
| const dataBaru = { | |
| nama: updatedUser.nama, | |
| role: updatedUser.role, | |
| nomor_hp: updatedUser.nomor_hp, | |
| foto_url: updatedUser.foto_url, | |
| aktif: updatedUser.aktif, | |
| }; | |
| await catatAudit({ | |
| userId: actorId, | |
| aksi: 'update', | |
| entitas: 'user', | |
| entitasId: updatedUser.id, | |
| dataLama, | |
| dataBaru, | |
| ipAddress: ip, | |
| userAgent, | |
| }); | |
| return updatedUser; | |
| } | |
| static async delete(userId: number, actorId: number, ip: string, userAgent: string) { | |
| const user = await this.getUserById(userId); | |
| if (user.id === actorId) { | |
| throw new APIError('Anda tidak dapat menonaktifkan akun Anda sendiri', 400); | |
| } | |
| await prisma.user.update({ | |
| where: { id: userId }, | |
| data: { aktif: 0 }, | |
| }); | |
| await catatAudit({ | |
| userId: actorId, | |
| aksi: 'delete', | |
| entitas: 'user', | |
| entitasId: user.id, | |
| dataLama: { username: user.username, aktif: 1 }, | |
| dataBaru: { username: user.username, aktif: 0 }, | |
| ipAddress: ip, | |
| userAgent, | |
| }); | |
| return true; | |
| } | |
| } | |