Moves updateHelcimSubscription to the live-verified wrapped shape
(PATCH /subscriptions { subscriptions: [{ id, ...payload }] }), adds a prior-
status check so sendWelcomeEmail only fires on pending_payment to active
transitions, short-circuits get-or-create-customer when a valid
helcimCustomerId is already on file, and replaces member.save() Slack-status
writes with findByIdAndUpdate({ runValidators: false }) to avoid save-time
validator pitfalls.
284 lines
No EOL
9.2 KiB
JavaScript
284 lines
No EOL
9.2 KiB
JavaScript
// Create a Helcim subscription
|
|
import { getHelcimPlanId, requiresPayment, getContributionTierByValue } from '../../config/contributions.js'
|
|
import Member from '../../models/member.js'
|
|
import { connectDB } from '../../utils/mongoose.js'
|
|
import { getSlackService } from '../../utils/slack.ts'
|
|
import { requireAuth } from '../../utils/auth.js'
|
|
import { createHelcimSubscription, generateIdempotencyKey } from '../../utils/helcim.js'
|
|
import { sendWelcomeEmail } from '../../utils/resend.js'
|
|
|
|
// Function to invite member to Slack
|
|
async function inviteToSlack(member) {
|
|
try {
|
|
const slackService = getSlackService()
|
|
if (!slackService) {
|
|
console.warn('Slack service not configured, skipping invitation')
|
|
return
|
|
}
|
|
|
|
console.log(`Processing Slack invitation for ${member.email}...`)
|
|
|
|
const inviteResult = await slackService.inviteUserToSlack(
|
|
member.email,
|
|
member.name
|
|
)
|
|
|
|
if (inviteResult.success) {
|
|
const update = {}
|
|
if (inviteResult.status === 'existing_user_added_to_channel' ||
|
|
inviteResult.status === 'user_already_in_channel' ||
|
|
inviteResult.status === 'new_user_invited_to_workspace') {
|
|
update.slackInviteStatus = 'sent'
|
|
update.slackUserId = inviteResult.userId
|
|
update.slackInvited = true
|
|
} else {
|
|
update.slackInviteStatus = 'pending'
|
|
update.slackInvited = false
|
|
}
|
|
await Member.findByIdAndUpdate(
|
|
member._id,
|
|
{ $set: update },
|
|
{ runValidators: false }
|
|
)
|
|
|
|
// Send notification to vetting channel
|
|
await slackService.notifyNewMember(
|
|
member.name,
|
|
member.email,
|
|
member.circle,
|
|
member.contributionTier,
|
|
inviteResult.status
|
|
)
|
|
|
|
console.log(`Successfully processed Slack invitation for ${member.email}: ${inviteResult.status}`)
|
|
} else {
|
|
await Member.findByIdAndUpdate(
|
|
member._id,
|
|
{ $set: { slackInviteStatus: 'failed' } },
|
|
{ runValidators: false }
|
|
)
|
|
|
|
console.error(`Failed to process Slack invitation for ${member.email}: ${inviteResult.error}`)
|
|
// Don't throw error - subscription creation should still succeed
|
|
}
|
|
} catch (error) {
|
|
console.error('Error during Slack invitation process:', error)
|
|
|
|
try {
|
|
await Member.findByIdAndUpdate(
|
|
member._id,
|
|
{ $set: { slackInviteStatus: 'failed' } },
|
|
{ runValidators: false }
|
|
)
|
|
} catch (saveError) {
|
|
console.error('Failed to update member Slack status:', saveError)
|
|
}
|
|
|
|
// Don't throw error - subscription creation should still succeed
|
|
}
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
await requireAuth(event)
|
|
await connectDB()
|
|
const body = await validateBody(event, helcimSubscriptionSchema)
|
|
|
|
// Only send welcome email when a member transitions from pending_payment
|
|
// to active for the first time — not on tier upgrades (active → active).
|
|
const priorMember = await Member.findOne(
|
|
{ helcimCustomerId: body.customerId },
|
|
{ status: 1 }
|
|
)
|
|
const isFirstActivation = priorMember?.status === 'pending_payment'
|
|
|
|
// Check if payment is required
|
|
if (!requiresPayment(body.contributionTier)) {
|
|
// For free tier, just update member status
|
|
const member = await Member.findOneAndUpdate(
|
|
{ helcimCustomerId: body.customerId },
|
|
{
|
|
status: 'active',
|
|
contributionTier: body.contributionTier,
|
|
subscriptionStartDate: new Date()
|
|
},
|
|
{ new: true }
|
|
)
|
|
|
|
logActivity(member._id, 'subscription_created', { tier: body.contributionTier })
|
|
|
|
await inviteToSlack(member)
|
|
if (isFirstActivation) await sendWelcomeEmail(member)
|
|
|
|
return {
|
|
success: true,
|
|
subscription: null,
|
|
member: {
|
|
id: member._id,
|
|
email: member.email,
|
|
name: member.name,
|
|
circle: member.circle,
|
|
contributionTier: member.contributionTier,
|
|
status: member.status
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get the Helcim plan ID
|
|
const planId = getHelcimPlanId(body.contributionTier)
|
|
|
|
// Validate card token is provided
|
|
if (!body.cardToken) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Payment information is required for this contribution tier'
|
|
})
|
|
}
|
|
|
|
// Check if we have a configured plan for this tier
|
|
if (!planId) {
|
|
const member = await Member.findOneAndUpdate(
|
|
{ helcimCustomerId: body.customerId },
|
|
{
|
|
status: 'active',
|
|
contributionTier: body.contributionTier,
|
|
subscriptionStartDate: new Date(),
|
|
paymentMethod: 'card',
|
|
cardToken: body.cardToken,
|
|
notes: `Payment successful but no Helcim plan configured for tier ${body.contributionTier}`
|
|
},
|
|
{ new: true }
|
|
)
|
|
|
|
await inviteToSlack(member)
|
|
if (isFirstActivation) await sendWelcomeEmail(member)
|
|
|
|
return {
|
|
success: true,
|
|
subscription: {
|
|
subscriptionId: 'manual-' + Date.now(),
|
|
status: 'needs_plan_setup',
|
|
nextBillingDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
|
|
},
|
|
member: {
|
|
id: member._id,
|
|
email: member.email,
|
|
name: member.name,
|
|
circle: member.circle,
|
|
contributionTier: member.contributionTier,
|
|
status: member.status
|
|
},
|
|
warning: `Payment successful but recurring plan needs to be set up in Helcim for the ${body.contributionTier} tier`
|
|
}
|
|
}
|
|
|
|
// Try to create subscription in Helcim
|
|
const idempotencyKey = generateIdempotencyKey()
|
|
|
|
// Get contribution tier details to set recurring amount
|
|
const tierInfo = getContributionTierByValue(body.contributionTier)
|
|
|
|
const subscriptionPayload = {
|
|
dateActivated: new Date().toISOString().split('T')[0], // Today in YYYY-MM-DD format
|
|
paymentPlanId: parseInt(planId),
|
|
customerCode: body.customerCode,
|
|
recurringAmount: parseFloat(tierInfo.amount),
|
|
paymentMethod: 'card'
|
|
}
|
|
|
|
try {
|
|
const subscriptionData = await createHelcimSubscription(subscriptionPayload, idempotencyKey)
|
|
|
|
// Extract the first subscription from the response array
|
|
const subscription = subscriptionData.data?.[0]
|
|
if (!subscription) {
|
|
throw new Error('No subscription returned in response')
|
|
}
|
|
|
|
// Update member in database
|
|
const member = await Member.findOneAndUpdate(
|
|
{ helcimCustomerId: body.customerId },
|
|
{
|
|
status: 'active',
|
|
contributionTier: body.contributionTier,
|
|
helcimSubscriptionId: subscription.id,
|
|
subscriptionStartDate: new Date(),
|
|
paymentMethod: 'card'
|
|
},
|
|
{ new: true }
|
|
)
|
|
|
|
logActivity(member._id, 'subscription_created', { tier: body.contributionTier })
|
|
|
|
await inviteToSlack(member)
|
|
if (isFirstActivation) await sendWelcomeEmail(member)
|
|
|
|
return {
|
|
success: true,
|
|
subscription: {
|
|
subscriptionId: subscription.id,
|
|
status: subscription.status,
|
|
nextBillingDate: subscription.nextBillingDate
|
|
},
|
|
member: {
|
|
id: member._id,
|
|
email: member.email,
|
|
name: member.name,
|
|
circle: member.circle,
|
|
contributionTier: member.contributionTier,
|
|
status: member.status
|
|
}
|
|
}
|
|
} catch (helcimError) {
|
|
// The helper throws createError on non-OK responses (statusCode = upstream HTTP status)
|
|
// and lets network errors propagate. We treat 400/404 from upstream AND any network
|
|
// error as the "manual setup needed" fallback. Re-throw other upstream errors (e.g. 5xx).
|
|
if (helcimError.statusCode && helcimError.statusCode !== 400 && helcimError.statusCode !== 404) {
|
|
throw helcimError
|
|
}
|
|
console.error('Error during subscription creation:', helcimError)
|
|
|
|
// Still mark member as active since payment was successful
|
|
const member = await Member.findOneAndUpdate(
|
|
{ helcimCustomerId: body.customerId },
|
|
{
|
|
status: 'active',
|
|
contributionTier: body.contributionTier,
|
|
subscriptionStartDate: new Date(),
|
|
paymentMethod: 'card',
|
|
cardToken: body.cardToken,
|
|
notes: `Payment successful but subscription creation failed: ${helcimError.message || 'unknown error'}`
|
|
},
|
|
{ new: true }
|
|
)
|
|
|
|
await inviteToSlack(member)
|
|
if (isFirstActivation) await sendWelcomeEmail(member)
|
|
|
|
return {
|
|
success: true,
|
|
subscription: {
|
|
subscriptionId: 'manual-' + Date.now(),
|
|
status: 'needs_setup',
|
|
nextBillingDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
|
|
},
|
|
member: {
|
|
id: member._id,
|
|
email: member.email,
|
|
name: member.name,
|
|
circle: member.circle,
|
|
contributionTier: member.contributionTier,
|
|
status: member.status
|
|
},
|
|
warning: 'Payment successful but recurring subscription needs manual setup'
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (error.statusCode) throw error
|
|
console.error('Error creating Helcim subscription:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'An unexpected error occurred'
|
|
})
|
|
}
|
|
}) |