File size: 3,798 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
import { Request, Response, NextFunction } from 'express';
import { TransaksiService } from '../services/transaksi.service';
import { sukses, gagal } from '../utils/response';
import { AuthenticatedRequest } from '../middlewares/auth.middleware';
import prisma from '../utils/db';

export class TransaksiController {
  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 trans = await TransaksiService.prosesTransaksi({
        data: req.body,
        actorId,
        ip,
        userAgent,
      });

      return sukses(res, trans, 'Transaksi berhasil diproses');
    } catch (error) {
      next(error);
    }
  }

  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) || 20;
      const statusFilter = (req.query.status_filter as any) || undefined;
      const memberId = req.query.member_id ? parseInt(req.query.member_id as string) : undefined;

      const where: any = {};
      if (statusFilter) where.status = statusFilter;
      if (memberId) where.member_id = memberId;

      const total = await prisma.transaksi.count({ where });
      const trans = await prisma.transaksi.findMany({
        where,
        orderBy: { created_at: 'desc' },
        skip,
        take: limit,
        include: {
          transaksi_detail: true,
          pembayaran: true,
        },
      });

      const page = Math.floor(skip / limit) + 1;

      return sukses(res, trans, 'Daftar transaksi berhasil dimuat', {
        total,
        page,
        per_page: limit,
      });
    } catch (error) {
      next(error);
    }
  }

  static async get(req: Request, res: Response, next: NextFunction) {
    try {
      const transaksiId = parseInt(req.params.id);
      const trans = await prisma.transaksi.findUnique({
        where: { id: transaksiId },
        include: {
          transaksi_detail: true,
          pembayaran: true,
          user: {
            select: {
              id: true,
              nama: true,
              username: true,
            },
          },
          member: true,
        },
      });

      if (!trans) {
        return gagal(res, 404, 'Transaksi tidak ditemukan');
      }

      return sukses(res, trans, 'Detail transaksi berhasil dimuat');
    } catch (error) {
      next(error);
    }
  }

  static async void(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const transaksiId = 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 alasan = req.body.alasan || 'Pembatalan transaksi oleh kasir';

      const trans = await TransaksiService.prosesVoidTransaksi(transaksiId, alasan, actorId, ip, userAgent);
      return sukses(res, trans, 'Transaksi berhasil di-void');
    } catch (error) {
      next(error);
    }
  }

  static async retur(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 retur = await TransaksiService.prosesReturBarang(req.body, actorId, ip, userAgent);
      return sukses(res, retur, 'Retur barang berhasil dicatat');
    } catch (error) {
      next(error);
    }
  }
}