Spaces:
Sleeping
Sleeping
File size: 3,346 Bytes
ef874e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import prisma from '../utils/db';
import { catatAudit } from '../utils/audit';
import { moveToTrash } from '../utils/trash';
import { APIError } from '../utils/errors';
export class SupplierService {
static async getSuppliers(skip = 0, limit = 10, pencarian?: string) {
const where: any = {
deleted_at: null,
};
if (pencarian) {
where.nama = {
contains: pencarian,
};
}
return await prisma.supplier.findMany({
where,
skip,
take: limit,
});
}
static async getSupplierById(id: number) {
const supplier = await prisma.supplier.findFirst({
where: { id, deleted_at: null },
});
if (!supplier) {
throw new APIError('Supplier tidak ditemukan', 404);
}
return supplier;
}
static async create(data: any, actorId: number, ip: string, userAgent: string) {
const supplier = await prisma.supplier.create({
data: {
nama: data.nama,
kontak: data.kontak ?? null,
nomor_hp: data.nomor_hp ?? null,
alamat: data.alamat ?? null,
hutang: data.hutang ?? 0,
aktif: data.aktif ?? 1,
},
});
await catatAudit({
userId: actorId,
aksi: 'create',
entitas: 'supplier',
entitasId: supplier.id,
dataBaru: { nama: supplier.nama, kontak: supplier.kontak, hutang: Number(supplier.hutang) },
ipAddress: ip,
userAgent,
});
return supplier;
}
static async update(id: number, data: any, actorId: number, ip: string, userAgent: string) {
const supplier = await this.getSupplierById(id);
const dataLama = {
nama: supplier.nama,
kontak: supplier.kontak,
nomor_hp: supplier.nomor_hp,
alamat: supplier.alamat,
hutang: Number(supplier.hutang),
aktif: supplier.aktif,
};
const updateData: any = {};
if (data.nama !== undefined) updateData.nama = data.nama;
if (data.kontak !== undefined) updateData.kontak = data.kontak;
if (data.nomor_hp !== undefined) updateData.nomor_hp = data.nomor_hp;
if (data.alamat !== undefined) updateData.alamat = data.alamat;
if (data.hutang !== undefined) updateData.hutang = data.hutang;
if (data.aktif !== undefined) updateData.aktif = data.aktif;
const updated = await prisma.supplier.update({
where: { id },
data: updateData,
});
const dataBaru = {
nama: updated.nama,
kontak: updated.kontak,
nomor_hp: updated.nomor_hp,
alamat: updated.alamat,
hutang: Number(updated.hutang),
aktif: updated.aktif,
};
await catatAudit({
userId: actorId,
aksi: 'update',
entitas: 'supplier',
entitasId: updated.id,
dataLama,
dataBaru,
ipAddress: ip,
userAgent,
});
return updated;
}
static async delete(id: number, actorId: number, ip: string, userAgent: string) {
const supplier = await this.getSupplierById(id);
await moveToTrash({
instance: supplier,
entityType: 'supplier',
actorId,
});
await catatAudit({
userId: actorId,
aksi: 'delete',
entitas: 'supplier',
entitasId: supplier.id,
dataLama: { nama: supplier.nama, aktif: 1 },
dataBaru: { nama: supplier.nama, aktif: 0 },
ipAddress: ip,
userAgent,
});
return true;
}
}
|