File size: 3,759 Bytes
ef874e6
 
 
 
48013ee
ef874e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48013ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { BonService } from '../services/bon.service';
import { sukses } from '../utils/response';
import { AuthenticatedRequest } from '../middlewares/auth.middleware';
import prisma from '../utils/db';

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

      const bonList = await BonService.getBon(skip, limit, statusFilter, memberId);
      return sukses(res, bonList, 'Daftar kasbon berhasil dimuat');
    } catch (error) {
      next(error);
    }
  }

  static async get(req: Request, res: Response, next: NextFunction) {
    try {
      const bonId = parseInt(req.params.id);
      const bon = await BonService.getBonById(bonId);
      return sukses(res, bon, 'Detail kasbon berhasil dimuat');
    } catch (error) {
      next(error);
    }
  }

  static async payInstallment(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const bonId = 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 { nominal, metode } = req.body;
      const bon = await BonService.bayarCicilan({
        bonId,
        nominal,
        metode,
        actorId,
        ip,
        userAgent,
      });

      return sukses(res, bon, 'Cicilan kasbon berhasil dibayarkan');
    } catch (error) {
      next(error);
    }
  }

  static async sendDebtReminder(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const bonId = parseInt(req.params.id);
      const waLink = await BonService.kirimReminderWA(bonId);
      return sukses(res, { wa_link: waLink }, 'WhatsApp reminder link generated successfully');
    } catch (error) {
      next(error);
    }
  }

  static async listInstallments(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 installments = await prisma.bon_cicilan.findMany({
        orderBy: { created_at: 'desc' },
        skip,
        take: limit,
        include: {
          bon: {
            include: {
              member: true,
            },
          },
        },
      });

      const formatted = installments.map((ins) => ({
        tanggal: ins.created_at.toISOString().replace('T', ' ').slice(0, 16),
        nama: ins.bon.member.nama,
        nominal: Number(ins.nominal),
        metode: ins.metode,
      }));

      return sukses(res, formatted, 'Daftar cicilan berhasil dimuat');
    } catch (error) {
      next(error);
    }
  }

  static async payByMemberId(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const memberId = parseInt(req.params.memberId);
      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 { nominal, metode } = req.body;
      const bon = await BonService.bayarCicilanByMemberId({
        memberId,
        nominal,
        metode,
        actorId,
        ip,
        userAgent,
      });

      return sukses(res, bon, 'Cicilan kasbon berhasil dibayarkan');
    } catch (error) {
      next(error);
    }
  }
}