feat(members): use contributionAmount in update-contribution route, inline ×12
This commit is contained in:
parent
613d077eaa
commit
7a2acd4628
2 changed files with 47 additions and 74 deletions
|
|
@ -2,8 +2,6 @@
|
|||
import {
|
||||
getHelcimPlanId,
|
||||
requiresPayment,
|
||||
getContributionTierByValue,
|
||||
getTierAmount,
|
||||
} from "../../config/contributions.js";
|
||||
import { connectDB } from "../../utils/mongoose.js";
|
||||
import Member from "../../models/member.js";
|
||||
|
|
@ -22,24 +20,24 @@ export default defineEventHandler(async (event) => {
|
|||
await connectDB();
|
||||
const body = await validateBody(event, updateContributionSchema);
|
||||
|
||||
const oldTier = member.contributionTier;
|
||||
const newTier = body.contributionTier;
|
||||
const oldAmount = member.contributionAmount;
|
||||
const newAmount = body.contributionAmount;
|
||||
|
||||
// If same tier, nothing to do
|
||||
if (oldTier === newTier) {
|
||||
// If same amount, nothing to do
|
||||
if (oldAmount === newAmount) {
|
||||
return {
|
||||
success: true,
|
||||
message: "Already on this tier",
|
||||
message: "Already contributing this amount",
|
||||
};
|
||||
}
|
||||
|
||||
// Log contribution change (fire-and-forget, at the top so it logs regardless of which case path executes)
|
||||
const logContributionChange = () => {
|
||||
logActivity(member._id, 'contribution_changed', { from: oldTier, to: newTier })
|
||||
logActivity(member._id, 'contribution_changed', { from: oldAmount, to: newAmount })
|
||||
}
|
||||
|
||||
const oldRequiresPayment = requiresPayment(oldTier);
|
||||
const newRequiresPayment = requiresPayment(newTier);
|
||||
const oldRequiresPayment = requiresPayment(oldAmount);
|
||||
const newRequiresPayment = requiresPayment(newAmount);
|
||||
|
||||
// Case 1: Moving from free to paid tier
|
||||
if (!oldRequiresPayment && newRequiresPayment) {
|
||||
|
|
@ -65,8 +63,6 @@ export default defineEventHandler(async (event) => {
|
|||
});
|
||||
}
|
||||
|
||||
const tierInfo = getContributionTierByValue(newTier);
|
||||
|
||||
try {
|
||||
const customerData = await getHelcimCustomer(member.helcimCustomerId);
|
||||
const customerCode = customerData.customerCode;
|
||||
|
|
@ -90,7 +86,7 @@ export default defineEventHandler(async (event) => {
|
|||
dateActivated: new Date().toISOString().split("T")[0],
|
||||
paymentPlanId: parseInt(paymentPlanId),
|
||||
customerCode,
|
||||
recurringAmount: getTierAmount(tierInfo, cadence),
|
||||
recurringAmount: cadence === 'annual' ? newAmount * 12 : newAmount,
|
||||
paymentMethod: "card",
|
||||
},
|
||||
idempotencyKey,
|
||||
|
|
@ -106,7 +102,7 @@ export default defineEventHandler(async (event) => {
|
|||
await Member.findByIdAndUpdate(
|
||||
member._id,
|
||||
{ $set: {
|
||||
contributionTier: newTier,
|
||||
contributionAmount: newAmount,
|
||||
helcimSubscriptionId: subscription.id,
|
||||
paymentMethod: "card",
|
||||
status: "active",
|
||||
|
|
@ -152,7 +148,7 @@ export default defineEventHandler(async (event) => {
|
|||
// Update member to free tier
|
||||
await Member.findByIdAndUpdate(
|
||||
member._id,
|
||||
{ $set: { contributionTier: newTier, helcimSubscriptionId: null, paymentMethod: "none", billingCadence: "monthly" } },
|
||||
{ $set: { contributionAmount: newAmount, helcimSubscriptionId: null, paymentMethod: "none", billingCadence: "monthly" } },
|
||||
{ runValidators: false }
|
||||
);
|
||||
|
||||
|
|
@ -182,20 +178,15 @@ export default defineEventHandler(async (event) => {
|
|||
});
|
||||
}
|
||||
|
||||
const newTierInfo = getContributionTierByValue(newTier);
|
||||
if (!newTierInfo) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid tier' });
|
||||
}
|
||||
|
||||
try {
|
||||
const subscriptionData = await updateHelcimSubscription(
|
||||
member.helcimSubscriptionId,
|
||||
{ recurringAmount: getTierAmount(newTierInfo, memberCadence) }
|
||||
{ recurringAmount: memberCadence === 'annual' ? newAmount * 12 : newAmount }
|
||||
);
|
||||
|
||||
await Member.findByIdAndUpdate(
|
||||
member._id,
|
||||
{ $set: { contributionTier: newTier } },
|
||||
{ $set: { contributionAmount: newAmount } },
|
||||
{ runValidators: false }
|
||||
);
|
||||
|
||||
|
|
@ -215,7 +206,7 @@ export default defineEventHandler(async (event) => {
|
|||
// Case 4: Moving between free tiers (shouldn't happen but handle it)
|
||||
await Member.findByIdAndUpdate(
|
||||
member._id,
|
||||
{ $set: { contributionTier: newTier } },
|
||||
{ $set: { contributionAmount: newAmount } },
|
||||
{ runValidators: false }
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue