Procházet zdrojové kódy

[web] remap customer.metadata.recurlyAccountCode in migration scripts (#31097)

* remap customer.metadata.recurlyAccountCode to customer.metadata.userId
* verify recurlyAccountCode is what we expect it to be before setting

GitOrigin-RevId: 8ab1d5e311dba34bb8c15d01096f6c31273a7506
Kristina před 6 měsíci
rodič
revize
ac1a61cafa

+ 13 - 0
services/web/scripts/recurly/migrate_recurly_customers_to_stripe.mjs

@@ -1023,6 +1023,19 @@ async function processCustomer(
     if (account.createdAt) {
       metadata.recurlyCreatedAt = account.createdAt.toISOString()
     }
+    if (
+      existingCustomer.metadata != null &&
+      existingCustomer.metadata.recurlyAccountCode === recurlyAccountCode &&
+      existingCustomer.metadata.userId == null
+    ) {
+      metadata.recurlyAccountCode = ''
+      metadata.userId = recurlyAccountCode
+    } else {
+      logWarn('Stripe customer metadata cannot be remapped', {
+        ...context,
+        existingCustomerMetadata: existingCustomer.metadata,
+      })
+    }
 
     /** @type {Stripe.CustomerUpdateParams} */
     const customerParams = {

+ 23 - 2
services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs

@@ -314,7 +314,8 @@ async function processMigration(input, commit) {
       mongoSubscription,
       stripeSubscription,
       recurlySubscription,
-      stripeClient
+      stripeClient,
+      stripeCustomer
     )
     return {
       status: 'migrated',
@@ -403,7 +404,8 @@ async function performCutover(
   mongoSubscription,
   stripeSubscription,
   recurlySubscription,
-  stripeClient
+  stripeClient,
+  stripeCustomer
 ) {
   const adminUserId = mongoSubscription.admin_id.toString()
 
@@ -473,6 +475,25 @@ async function performCutover(
       `Successfully migrated to Stripe but failed to register analytics mapping: ${err.message}`
     )
   }
+
+  // Step 6. Remap customer metadata (if needed) in Stripe
+  if (
+    stripeCustomer.metadata != null &&
+    stripeCustomer.metadata.recurlyAccountCode != null &&
+    stripeCustomer.metadata.userId == null
+  ) {
+    try {
+      await stripeClient.updateCustomerMetadata(stripeCustomer.id, {
+        recurlyAccountCode: '',
+        userId: adminUserId,
+      })
+    } catch (err) {
+      throw new ReportError(
+        'customer-metadata-removal-failed',
+        `Successfully migrated to Stripe and registered analytics mapping but failed to remove customer metadata: ${err.message}`
+      )
+    }
+  }
 }
 
 function parseArgs() {