merge: catch up with feature/helcim-plan-consolidation base
# Conflicts: # server/api/auth/member.get.js # server/api/members/update-contribution.post.js # tests/server/api/update-contribution.test.js
This commit is contained in:
commit
7704557f16
10 changed files with 160 additions and 9 deletions
56
server/api/helcim/subscription.get.js
Normal file
56
server/api/helcim/subscription.get.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Refresh the authenticated member's cached nextBillingDate from Helcim.
|
||||
// The account page calls this only when the stored date is stale (missing,
|
||||
// past, or within ~24h). On success, writes the fresh date back to the member
|
||||
// record so subsequent loads can render instantly from /api/auth/member.
|
||||
//
|
||||
// On Helcim errors, returns { subscription: null, error: 'unavailable' } (HTTP 200)
|
||||
// so the client can fall back to the cached value (if any) without crashing.
|
||||
import { requireAuth } from '../../utils/auth.js'
|
||||
import { getHelcimSubscription } from '../../utils/helcim.js'
|
||||
import Member from '../../models/member.js'
|
||||
import { connectDB } from '../../utils/mongoose.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const member = await requireAuth(event)
|
||||
|
||||
if (!member.helcimSubscriptionId) {
|
||||
return { subscription: null }
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await getHelcimSubscription(member.helcimSubscriptionId)
|
||||
const subscription = Array.isArray(response)
|
||||
? response[0]
|
||||
: (response?.data?.[0] || response)
|
||||
|
||||
const nextBillingDate = subscription?.nextBillingDate || null
|
||||
|
||||
if (nextBillingDate) {
|
||||
const parsed = new Date(nextBillingDate)
|
||||
if (!Number.isNaN(parsed.getTime())) {
|
||||
await connectDB()
|
||||
await Member.findByIdAndUpdate(
|
||||
member._id,
|
||||
{ $set: { nextBillingDate: parsed } },
|
||||
{ runValidators: false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
subscription: subscription
|
||||
? {
|
||||
id: String(subscription.id ?? ''),
|
||||
status: subscription.status || '',
|
||||
nextBillingDate: nextBillingDate || '',
|
||||
}
|
||||
: null,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[subscription.get] Helcim lookup failed', {
|
||||
helcimSubscriptionId: member.helcimSubscriptionId,
|
||||
error: error?.message || error,
|
||||
})
|
||||
return { subscription: null, error: 'unavailable' }
|
||||
}
|
||||
})
|
||||
|
|
@ -165,6 +165,10 @@ export default defineEventHandler(async (event) => {
|
|||
throw createError({ statusCode: 500, statusMessage: 'Subscription creation failed' })
|
||||
}
|
||||
|
||||
const nextBillingDate = subscription.nextBillingDate
|
||||
? new Date(subscription.nextBillingDate)
|
||||
: null
|
||||
|
||||
// Update member in database
|
||||
const member = await Member.findOneAndUpdate(
|
||||
{ helcimCustomerId: body.customerId },
|
||||
|
|
@ -176,6 +180,9 @@ export default defineEventHandler(async (event) => {
|
|||
billingCadence: cadence,
|
||||
subscriptionStartDate: new Date(),
|
||||
status: 'active',
|
||||
...(nextBillingDate && !Number.isNaN(nextBillingDate.getTime())
|
||||
? { nextBillingDate }
|
||||
: {}),
|
||||
} },
|
||||
{ new: true, runValidators: false }
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue