File size: 5,390 Bytes
2b109bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b4530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b109bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { prisma } from '../../config/prisma.js';

export const subscriptionsRepository = {
  listPlans(gymId) {
    return prisma.subscriptionPlan.findMany({ where: { gymId, deletedAt: null }, orderBy: { price: 'asc' } });
  },

  findPlan(gymId, id) {
    return prisma.subscriptionPlan.findFirst({ where: { id, gymId, deletedAt: null } });
  },

  findMember(gymId, id) {
    return prisma.member.findFirst({ where: { id, gymId, deletedAt: null } });
  },

  createPlan(gymId, data) {
    return prisma.subscriptionPlan.create({ data: { ...data, gymId } });
  },

  updatePlan(gymId, id, data) {
    return prisma.subscriptionPlan.update({ where: { id, gymId }, data });
  },

  deletePlan(gymId, id) {
    return prisma.subscriptionPlan.update({ where: { id, gymId }, data: { deletedAt: new Date() } });
  },

  list({ gymId, skip, take, search }) {
    const where = {
      gymId,
      deletedAt: null,
      ...(search
        ? {
            OR: [
              { member: { fullName: { contains: search } } },
              { plan: { name: { contains: search } } },
            ],
          }
        : {}),
    };
    return Promise.all([
      prisma.subscription.findMany({
        where,
        skip,
        take,
        orderBy: { createdAt: 'desc' },
        include: { member: true, plan: true },
      }),
      prisma.subscription.count({ where }),
    ]);
  },

  findById(gymId, id) {
    return prisma.subscription.findFirst({ where: { id, gymId, deletedAt: null }, include: { member: true, plan: true } });
  },

  findForMember(gymId, memberId) {
    return prisma.subscription.findMany({
      where: { gymId, memberId, deletedAt: null },
      include: { member: true, plan: true },
      orderBy: { endDate: 'desc' },
    });
  },

  expiring(gymId, days = 7) {
    const now = new Date();
    const until = new Date(now);
    until.setDate(until.getDate() + days);
    return prisma.subscription.findMany({
      where: {
        gymId,
        deletedAt: null,
        status: 'active',
        endDate: { gte: now, lte: until },
      },
      include: { member: true, plan: true },
      orderBy: { endDate: 'asc' },
    });
  },

  create(data) {
    return prisma.subscription.create({ data, include: { member: true, plan: true } });
  },

  createMemberPurchase({ gymId, memberId, planId, startDate, endDate, discountAmount, finalPrice, paymentMethod, notes }) {
    return prisma.$transaction(async (tx) => {
      const subscription = await tx.subscription.create({
        data: {
          gymId,
          memberId,
          planId,
          startDate,
          endDate,
          status: 'pending',
          discountAmount,
          finalPrice,
        },
        include: { member: true, plan: true },
      });

      const payment = await tx.payment.create({
        data: {
          gymId,
          memberId,
          subscriptionId: subscription.id,
          amount: finalPrice,
          paymentMethod,
          status: 'pending',
          notes,
        },
        include: { member: true, subscription: true },
      });

      await tx.notification.create({
        data: {
          gymId,
          memberId,
          type: 'payment',
          channel: 'email',
          message: `Your ${subscription.plan.name} membership request is waiting for gym admin approval.`,
          status: 'pending',
        },
      });

      return { subscription, payment };
    });
  },

  createActiveWithPayment({ gymId, memberId, planId, startDate, endDate, discountAmount, finalPrice, paymentMethod = 'cash', notes }) {
    return prisma.$transaction(async (tx) => {
      await tx.subscription.updateMany({
        where: { gymId, memberId, status: 'active', deletedAt: null },
        data: { status: 'canceled' },
      });

      const subscription = await tx.subscription.create({
        data: {
          gymId,
          memberId,
          planId,
          startDate,
          endDate,
          status: 'active',
          discountAmount,
          finalPrice,
        },
        include: { member: true, plan: true },
      });

      const payment = await tx.payment.create({
        data: {
          gymId,
          memberId,
          subscriptionId: subscription.id,
          amount: finalPrice,
          paymentMethod,
          status: 'success',
          notes,
        },
      });

      return { ...subscription, payment };
    });
  },

  renewWithPayment(gymId, id, data, payment) {
    return prisma.$transaction(async (tx) => {
      const subscription = await tx.subscription.update({
        where: { id, gymId },
        data,
        include: { member: true, plan: true },
      });

      await tx.payment.create({
        data: {
          gymId,
          memberId: subscription.memberId,
          subscriptionId: subscription.id,
          amount: payment.amount,
          paymentMethod: payment.paymentMethod || 'cash',
          status: 'success',
          notes: payment.notes,
        },
      });

      return subscription;
    });
  },

  update(gymId, id, data) {
    return prisma.subscription.update({ where: { id, gymId }, data, include: { member: true, plan: true } });
  },

  expireOld(gymId) {
    return prisma.subscription.updateMany({
      where: { gymId, status: 'active', endDate: { lt: new Date() }, deletedAt: null },
      data: { status: 'expired' },
    });
  },
};