|
| 1 | +import { Db, MongoClient } from "mongodb"; |
| 2 | +import { Collections } from "@src/modules/common/enum/database.collection.enum"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Migration to convert stripePromoCodeId to promoCodeProvider array structure |
| 6 | + * This migration updates existing promo codes from the old structure: |
| 7 | + * { stripePromoCodeId: "promo_123" } |
| 8 | + * |
| 9 | + * To the new structure: |
| 10 | + * { promoCodeProvider: [{ id: "promo_123", provider: "stripe" }] } |
| 11 | + */ |
| 12 | +export class MigratePromoCodeProviderMigration { |
| 13 | + constructor(private readonly db: Db) {} |
| 14 | + |
| 15 | + async up(): Promise<void> { |
| 16 | + console.log("Starting promo code provider migration..."); |
| 17 | + |
| 18 | + const collection = this.db.collection(Collections.PROMOCODES); |
| 19 | + |
| 20 | + // Find all documents with the old stripePromoCodeId field |
| 21 | + const promoCodes = await collection |
| 22 | + .find({ stripePromoCodeId: { $exists: true } }) |
| 23 | + .toArray(); |
| 24 | + |
| 25 | + console.log(`Found ${promoCodes.length} promo codes to migrate`); |
| 26 | + |
| 27 | + if (promoCodes.length === 0) { |
| 28 | + console.log("No promo codes found with old structure. Migration complete."); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Process each promo code |
| 33 | + for (const promoCode of promoCodes) { |
| 34 | + try { |
| 35 | + // Create the new promoCodeProvider array |
| 36 | + const promoCodeProvider = [ |
| 37 | + { |
| 38 | + id: promoCode.stripePromoCodeId, |
| 39 | + provider: "stripe" |
| 40 | + } |
| 41 | + ]; |
| 42 | + |
| 43 | + // Update the document |
| 44 | + await collection.updateOne( |
| 45 | + { _id: promoCode._id }, |
| 46 | + { |
| 47 | + $set: { promoCodeProvider }, |
| 48 | + $unset: { stripePromoCodeId: "" } |
| 49 | + } |
| 50 | + ); |
| 51 | + |
| 52 | + console.log(`Migrated promo code: ${promoCode.code} (${promoCode._id})`); |
| 53 | + } catch (error) { |
| 54 | + console.error(`Failed to migrate promo code ${promoCode.code}:`, error); |
| 55 | + throw error; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + console.log(`Successfully migrated ${promoCodes.length} promo codes`); |
| 60 | + } |
| 61 | + |
| 62 | + async down(): Promise<void> { |
| 63 | + console.log("Rolling back promo code provider migration..."); |
| 64 | + |
| 65 | + const collection = this.db.collection(Collections.PROMOCODES); |
| 66 | + |
| 67 | + // Find all documents with the new promoCodeProvider field |
| 68 | + const promoCodes = await collection |
| 69 | + .find({ promoCodeProvider: { $exists: true } }) |
| 70 | + .toArray(); |
| 71 | + |
| 72 | + console.log(`Found ${promoCodes.length} promo codes to rollback`); |
| 73 | + |
| 74 | + if (promoCodes.length === 0) { |
| 75 | + console.log("No promo codes found with new structure. Rollback complete."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Process each promo code |
| 80 | + for (const promoCode of promoCodes) { |
| 81 | + try { |
| 82 | + // Find the stripe provider entry |
| 83 | + const stripeProvider = promoCode.promoCodeProvider?.find( |
| 84 | + (provider: any) => provider.provider === "stripe" |
| 85 | + ); |
| 86 | + |
| 87 | + if (stripeProvider) { |
| 88 | + // Restore the old stripePromoCodeId field |
| 89 | + await collection.updateOne( |
| 90 | + { _id: promoCode._id }, |
| 91 | + { |
| 92 | + $set: { stripePromoCodeId: stripeProvider.id }, |
| 93 | + $unset: { promoCodeProvider: "" } |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + console.log(`Rolled back promo code: ${promoCode.code} (${promoCode._id})`); |
| 98 | + } else { |
| 99 | + console.log(`No stripe provider found for promo code: ${promoCode.code}`); |
| 100 | + } |
| 101 | + } catch (error) { |
| 102 | + console.error(`Failed to rollback promo code ${promoCode.code}:`, error); |
| 103 | + throw error; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + console.log(`Successfully rolled back ${promoCodes.length} promo codes`); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Migration runner function |
| 112 | +export async function runPromoCodeProviderMigration( |
| 113 | + mongoUrl: string, |
| 114 | + dbName: string, |
| 115 | + direction: "up" | "down" = "up" |
| 116 | +): Promise<void> { |
| 117 | + const client = new MongoClient(mongoUrl); |
| 118 | + |
| 119 | + try { |
| 120 | + await client.connect(); |
| 121 | + console.log("Connected to MongoDB"); |
| 122 | + |
| 123 | + const db = client.db(dbName); |
| 124 | + const migration = new MigratePromoCodeProviderMigration(db); |
| 125 | + |
| 126 | + if (direction === "up") { |
| 127 | + await migration.up(); |
| 128 | + } else { |
| 129 | + await migration.down(); |
| 130 | + } |
| 131 | + |
| 132 | + console.log("Migration completed successfully"); |
| 133 | + } catch (error) { |
| 134 | + console.error("Migration failed:", error); |
| 135 | + throw error; |
| 136 | + } finally { |
| 137 | + await client.close(); |
| 138 | + console.log("Disconnected from MongoDB"); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// If this file is run directly |
| 143 | +if (require.main === module) { |
| 144 | + const mongoUrl = process.env.MONGODB_URI || "mongodb://localhost:27017"; |
| 145 | + const dbName = process.env.DATABASE_NAME || "sparrow"; |
| 146 | + const direction = (process.argv[2] as "up" | "down") || "up"; |
| 147 | + |
| 148 | + runPromoCodeProviderMigration(mongoUrl, dbName, direction) |
| 149 | + .then(() => { |
| 150 | + console.log("Migration script completed"); |
| 151 | + process.exit(0); |
| 152 | + }) |
| 153 | + .catch((error) => { |
| 154 | + console.error("Migration script failed:", error); |
| 155 | + process.exit(1); |
| 156 | + }); |
| 157 | +} |
0 commit comments