File size: 4,150 Bytes
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
import { ApiError } from '../../common/errors/ApiError.js';
import { subscriptionsRepository } from './subscriptions.repository.js';

const addDays = (date, days) => {
  const next = new Date(date);
  next.setDate(next.getDate() + days);
  return next;
};

const calculateDiscountedPrice = (plan, member) => {
  const planPrice = Number(plan.price);
  const percentDiscount = member.discountPercent ? (planPrice * Number(member.discountPercent)) / 100 : 0;
  const fixedDiscount = Number(member.discountAmount || 0);
  const discountAmount = Math.min(planPrice, percentDiscount + fixedDiscount);
  return {
    discountAmount,
    finalPrice: Math.max(planPrice - discountAmount, 0),
  };
};

export const subscriptionsService = {
  async assign(gymId, { memberId, planId, startDate = new Date() }) {
    const plan = await subscriptionsRepository.findPlan(gymId, planId);
    if (!plan) throw ApiError.notFound('Subscription plan not found');
    const member = await subscriptionsRepository.findMember(gymId, memberId);
    if (!member) throw ApiError.notFound('Member not found');

    const billing = calculateDiscountedPrice(plan, member);

    return subscriptionsRepository.createActiveWithPayment({
      gymId,
      memberId,
      planId,
      startDate,
      endDate: addDays(startDate, plan.durationDays),
      discountAmount: billing.discountAmount,
      finalPrice: billing.finalPrice,
      paymentMethod: 'cash',
      notes: billing.discountAmount > 0 ? `Subscription payment after EGP ${billing.discountAmount} discount` : 'Subscription payment',
    });
  },

  async purchaseForMember(gymId, memberId, { planId, paymentMethod, notes }) {
    const plan = await subscriptionsRepository.findPlan(gymId, planId);
    if (!plan) throw ApiError.notFound('Subscription plan not found');
    const member = await subscriptionsRepository.findMember(gymId, memberId);
    if (!member) throw ApiError.notFound('Member not found');

    const startDate = new Date();
    const billing = calculateDiscountedPrice(plan, member);
    const paymentLabels = {
      cash: 'Cash',
      card: 'Visa',
      wallet: 'Vodafone Cash',
    };

    return subscriptionsRepository.createMemberPurchase({
      gymId,
      memberId,
      planId,
      startDate,
      endDate: addDays(startDate, plan.durationDays),
      discountAmount: billing.discountAmount,
      finalPrice: billing.finalPrice,
      paymentMethod,
      notes: notes || `${paymentLabels[paymentMethod]} membership checkout pending admin confirmation`,
    });
  },

  async renew(gymId, subscriptionId, payload) {
    const subscription = await subscriptionsRepository.findById(gymId, subscriptionId);
    if (!subscription) throw ApiError.notFound('Subscription not found');

    const planId = payload.planId || subscription.planId;
    const plan = await subscriptionsRepository.findPlan(gymId, planId);
    if (!plan) throw ApiError.notFound('Subscription plan not found');
    const member = await subscriptionsRepository.findMember(gymId, subscription.memberId);
    if (!member) throw ApiError.notFound('Member not found');

    const startDate = payload.startDate || new Date();
    const billing = calculateDiscountedPrice(plan, member);
    return subscriptionsRepository.renewWithPayment(gymId, subscriptionId, {
      planId,
      startDate,
      endDate: addDays(startDate, plan.durationDays),
      status: 'active',
      freezeDays: 0,
      discountAmount: billing.discountAmount,
      finalPrice: billing.finalPrice,
    }, {
      amount: billing.finalPrice,
      paymentMethod: payload.paymentMethod || 'cash',
      notes: billing.discountAmount > 0 ? `Renewal payment after EGP ${billing.discountAmount} discount` : 'Renewal payment',
    });
  },

  async freeze(gymId, subscriptionId, freezeDays) {
    const subscription = await subscriptionsRepository.findById(gymId, subscriptionId);
    if (!subscription) throw ApiError.notFound('Subscription not found');

    return subscriptionsRepository.update(gymId, subscriptionId, {
      status: 'frozen',
      freezeDays,
      endDate: addDays(subscription.endDate, freezeDays),
    });
  },
};