File size: 2,370 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
import { Request, Response, NextFunction } from 'express';
import { SupplierService } from '../services/supplier.service';
import { sukses } from '../utils/response';
import { AuthenticatedRequest } from '../middlewares/auth.middleware';

export class SupplierController {
  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 pencarian = req.query.pencarian as string || undefined;

      const suppliers = await SupplierService.getSuppliers(skip, limit, pencarian);
      return sukses(res, suppliers, 'Daftar supplier berhasil dimuat');
    } 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 supplier = await SupplierService.create(req.body, actorId, ip, userAgent);
      return sukses(res, supplier, 'Supplier berhasil ditambahkan');
    } catch (error) {
      next(error);
    }
  }

  static async update(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const supplierId = 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 supplier = await SupplierService.update(supplierId, req.body, actorId, ip, userAgent);
      return sukses(res, supplier, 'Supplier berhasil diperbarui');
    } catch (error) {
      next(error);
    }
  }

  static async delete(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const supplierId = 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 SupplierService.delete(supplierId, actorId, ip, userAgent);
      return sukses(res, null, 'Supplier berhasil dinonaktifkan');
    } catch (error) {
      next(error);
    }
  }
}