|
| 1 | +import { Injectable, OnModuleInit, Inject } from "@nestjs/common"; |
| 2 | +import { Collections } from "@src/modules/common/enum/database.collection.enum"; |
| 3 | +import { Db, ObjectId } from "mongodb"; |
| 4 | + |
| 5 | +@Injectable() |
| 6 | +export class UpdateHubBillingMigration implements OnModuleInit { |
| 7 | + private hasRun = false; |
| 8 | + |
| 9 | + constructor(@Inject("DATABASE_CONNECTION") private readonly db: Db) {} |
| 10 | + |
| 11 | + async onModuleInit(): Promise<void> { |
| 12 | + if (this.hasRun) return; |
| 13 | + |
| 14 | + try { |
| 15 | + console.log( |
| 16 | + "\x1b[36m[Nest]\x1b[0m \x1b[36mStarting Updating Billing...\x1b[0m", |
| 17 | + ); |
| 18 | + // Add pricing details to pricing collection |
| 19 | + // const pricing = { |
| 20 | + // name: "Default Pricing", |
| 21 | + // currency: "usd", |
| 22 | + // plans: [ |
| 23 | + // { |
| 24 | + // tier: "Standard", |
| 25 | + // plan_name: "Standard", |
| 26 | + // billing: [ |
| 27 | + // { |
| 28 | + // interval: "monthly", |
| 29 | + // price: 9.99, |
| 30 | + // providers: { |
| 31 | + // stripe: "id1", |
| 32 | + // }, |
| 33 | + // }, |
| 34 | + // { |
| 35 | + // interval: "annual", |
| 36 | + // price: 99, |
| 37 | + // providers: { |
| 38 | + // stripe: "id2", |
| 39 | + // }, |
| 40 | + // }, |
| 41 | + // ], |
| 42 | + // }, |
| 43 | + // { |
| 44 | + // tier: "Professional", |
| 45 | + // plan_name: "Professional", |
| 46 | + // billing: [ |
| 47 | + // { |
| 48 | + // interval: "monthly", |
| 49 | + // price: 19.99, |
| 50 | + // providers: { |
| 51 | + // stripe: "id3", |
| 52 | + // }, |
| 53 | + // }, |
| 54 | + // { |
| 55 | + // interval: "annual", |
| 56 | + // price: 199, |
| 57 | + // providers: { |
| 58 | + // stripe: "oid4", |
| 59 | + // }, |
| 60 | + // }, |
| 61 | + // ], |
| 62 | + // }, |
| 63 | + // ], |
| 64 | + // version: 1, |
| 65 | + // created_at: "2025-07-09T11:00:00.000Z", |
| 66 | + // }; |
| 67 | + |
| 68 | + // await this.addPricing(pricing); |
| 69 | + const teamId = "team213123123"; |
| 70 | + const billing = { |
| 71 | + current_period_start: new Date("2025-07-08T13:04:24.000Z"), |
| 72 | + current_period_end: new Date("2025-08-08T13:04:24.000Z"), |
| 73 | + amount_billed: 119.88, |
| 74 | + currency: "usd", |
| 75 | + status: "active", |
| 76 | + collection_method: "charge_manually", |
| 77 | + paid_at: new Date("2025-07-08T13:04:28.000Z"), |
| 78 | + billingType: "paid", |
| 79 | + updatedBy: "system-admin", |
| 80 | + in_trial: false, |
| 81 | + paymentProviders: [ |
| 82 | + { |
| 83 | + id: "37069b1a-b266-4e9c-a010-31cd30075ac4", |
| 84 | + provider: "stripe", |
| 85 | + currentPaymentMethod: true, |
| 86 | + subscriptionId: "", |
| 87 | + payment_method: ["card"], |
| 88 | + customerId: "cus_SdRmVfW1qK0Ruw", |
| 89 | + updatedAt: new Date("2025-07-08T13:04:29.146Z"), |
| 90 | + }, |
| 91 | + ], |
| 92 | + }; |
| 93 | + await this.addBillingToTeam(teamId, billing); |
| 94 | + |
| 95 | + this.hasRun = true; |
| 96 | + } catch (error) { |
| 97 | + console.error( |
| 98 | + "\x1b[31m[Nest] Error during UpdateHubBillingMigration:\x1b[0m", |
| 99 | + error, |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + private async addPricing(pricing: any): Promise<void> { |
| 104 | + const pricingCollection = this.db.collection(Collections.PRICING); |
| 105 | + await pricingCollection.insertOne(pricing); |
| 106 | + console.log( |
| 107 | + "\x1b[32m[Nest]\x1b[0m Pricing details added to pricing collection", |
| 108 | + ); |
| 109 | + } |
| 110 | + private async addBillingToTeam(teamId: string, billing: any): Promise<void> { |
| 111 | + const teamCollection = this.db.collection(Collections.TEAM); |
| 112 | + await teamCollection.updateOne( |
| 113 | + { _id: new ObjectId(teamId) }, |
| 114 | + { |
| 115 | + $set: { |
| 116 | + billing: billing, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ); |
| 120 | + console.log(`\x1b[32m[Nest]\x1b[0m Billing info added to team ${teamId}`); |
| 121 | + } |
| 122 | +} |
0 commit comments