|
| 1 | +import {slugify} from '@tryghost/string'; |
| 2 | + |
| 3 | +const extractName = (customFields: Array<{name: string; value: string}>): string | null => { |
| 4 | + const firstNameField = customFields.find(f => f.name.toLowerCase() === 'first_name' || f.name.toLowerCase() === 'firstname'); |
| 5 | + const lastNameField = customFields.find(f => f.name.toLowerCase() === 'last_name' || f.name.toLowerCase() === 'lastname'); |
| 6 | + |
| 7 | + const firstName = firstNameField?.value?.trim() || ''; |
| 8 | + const lastName = lastNameField?.value?.trim() || ''; |
| 9 | + |
| 10 | + const combinedName = [firstName, lastName].filter(name => name.length > 0).join(' '); |
| 11 | + |
| 12 | + return combinedName.length > 0 ? combinedName : null; |
| 13 | +}; |
| 14 | + |
| 15 | +const mapSubscription = (subscription: BeehiivSubscription): GhostMemberObject => { |
| 16 | + const labels: string[] = []; |
| 17 | + |
| 18 | + // Add status label |
| 19 | + labels.push(`beehiiv-status-${subscription.status}`); |
| 20 | + |
| 21 | + // Add tier label |
| 22 | + labels.push(`beehiiv-tier-${subscription.subscription_tier}`); |
| 23 | + |
| 24 | + // Add premium tier names as labels |
| 25 | + if (subscription.subscription_premium_tier_names && subscription.subscription_premium_tier_names.length > 0) { |
| 26 | + subscription.subscription_premium_tier_names.forEach((tierName: string) => { |
| 27 | + const slugifiedTier = slugify(tierName); |
| 28 | + labels.push(`beehiiv-premium-${slugifiedTier}`); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + // Add tags as labels |
| 33 | + if (subscription.tags && subscription.tags.length > 0) { |
| 34 | + subscription.tags.forEach((tag: string) => { |
| 35 | + const slugifiedTag = slugify(tag); |
| 36 | + labels.push(`beehiiv-tag-${slugifiedTag}`); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + // Determine if this is a complimentary plan |
| 41 | + // A member is on a complimentary plan if they have premium access but no Stripe customer ID |
| 42 | + const isPremium = subscription.subscription_tier === 'premium'; |
| 43 | + const hasStripeId = Boolean(subscription.stripe_customer_id); |
| 44 | + const complimentaryPlan = isPremium && !hasStripeId; |
| 45 | + |
| 46 | + return { |
| 47 | + email: subscription.email, |
| 48 | + name: extractName(subscription.custom_fields || []), |
| 49 | + note: null, |
| 50 | + subscribed_to_emails: subscription.status === 'active', |
| 51 | + stripe_customer_id: subscription.stripe_customer_id || '', |
| 52 | + complimentary_plan: complimentaryPlan, |
| 53 | + labels, |
| 54 | + created_at: new Date(subscription.created * 1000) |
| 55 | + }; |
| 56 | +}; |
| 57 | + |
| 58 | +const mapSubscriptions = (subscriptions: BeehiivSubscription[]): MappedMembers => { |
| 59 | + const result: MappedMembers = { |
| 60 | + free: [], |
| 61 | + paid: [] |
| 62 | + }; |
| 63 | + |
| 64 | + subscriptions.forEach((subscription) => { |
| 65 | + const member = mapSubscription(subscription); |
| 66 | + |
| 67 | + if (member.stripe_customer_id) { |
| 68 | + result.paid.push(member); |
| 69 | + } else { |
| 70 | + result.free.push(member); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + return result; |
| 75 | +}; |
| 76 | + |
| 77 | +export const mapMembersTasks = (_options: any, ctx: any) => { |
| 78 | + const tasks = [ |
| 79 | + { |
| 80 | + title: 'Mapping subscriptions to Ghost member format', |
| 81 | + task: async (_: any, task: any) => { |
| 82 | + try { |
| 83 | + const subscriptions: BeehiivSubscription[] = ctx.result.subscriptions || []; |
| 84 | + ctx.result.members = mapSubscriptions(subscriptions); |
| 85 | + |
| 86 | + const freeCount = ctx.result.members.free.length; |
| 87 | + const paidCount = ctx.result.members.paid.length; |
| 88 | + |
| 89 | + task.output = `Mapped ${freeCount} free and ${paidCount} paid members`; |
| 90 | + } catch (error) { |
| 91 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 92 | + task.output = errorMessage; |
| 93 | + throw error; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + ]; |
| 98 | + |
| 99 | + return tasks; |
| 100 | +}; |
| 101 | + |
| 102 | +export { |
| 103 | + extractName, |
| 104 | + mapSubscription, |
| 105 | + mapSubscriptions |
| 106 | +}; |
0 commit comments