Spaces:
Sleeping
Sleeping
| import { Request, Response, NextFunction } from 'express'; | |
| import { ProdukService } from '../services/produk.service'; | |
| import { sukses } from '../utils/response'; | |
| import { AuthenticatedRequest } from '../middlewares/auth.middleware'; | |
| export class ProdukController { | |
| static async list(req: Request, res: Response, next: NextFunction) { | |
| try { | |
| const skip = parseInt(req.query.skip as string) || 0; | |
| const limit = parseInt(req.query.limit as string) || 10; | |
| const kategoriId = req.query.kategori_id ? parseInt(req.query.kategori_id as string) : undefined; | |
| const supplierId = req.query.supplier_id ? parseInt(req.query.supplier_id as string) : undefined; | |
| const pencarian = req.query.pencarian as string || undefined; | |
| const { products, total } = await ProdukService.getProduk(skip, limit, kategoriId, supplierId, pencarian); | |
| const page = Math.floor(skip / limit) + 1; | |
| return sukses(res, products, 'Daftar produk berhasil dimuat', { | |
| total, | |
| page, | |
| per_page: limit, | |
| }); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async get(req: Request, res: Response, next: NextFunction) { | |
| try { | |
| const produkId = parseInt(req.params.id); | |
| const product = await ProdukService.getProdukById(produkId); | |
| return sukses(res, product, 'Detail produk berhasil dimuat'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async scan(req: Request, res: Response, next: NextFunction) { | |
| try { | |
| const { barcode_or_kode } = req.params; | |
| const product = await ProdukService.getProdukByBarcodeOrKode(barcode_or_kode); | |
| return sukses(res, product, 'Produk terdeteksi'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async create(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const actorId = req.user.id; | |
| const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); | |
| const userAgent = (req.headers['user-agent'] || 'unknown') as string; | |
| const product = await ProdukService.create(req.body, actorId, ip, userAgent); | |
| return sukses(res, product, 'Produk berhasil dibuat'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async update(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const produkId = parseInt(req.params.id); | |
| const actorId = req.user.id; | |
| const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); | |
| const userAgent = (req.headers['user-agent'] || 'unknown') as string; | |
| const product = await ProdukService.update(produkId, req.body, actorId, ip, userAgent); | |
| return sukses(res, product, 'Produk berhasil diperbarui'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async delete(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const produkId = parseInt(req.params.id); | |
| const actorId = req.user.id; | |
| const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); | |
| const userAgent = (req.headers['user-agent'] || 'unknown') as string; | |
| await ProdukService.delete(produkId, actorId, ip, userAgent); | |
| return sukses(res, null, 'Produk berhasil dihapus (soft-delete)'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| } | |