Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | import { Injectable, Logger } from '@nestjs/common'; import { Connection, Types } from 'mongoose'; import { InjectConnection } from '@nestjs/mongoose'; import { SeedSafetyGuard } from './seed-safety-guard'; import { SeedContext, SeedScale, SeedEnv } from './seed-context'; import { SeedCleanupService } from './seed-cleanup.service'; import { SeedValidationService } from './seed-validation.service'; import { SeedIdRegistry } from './seed-id-registry'; import { PlatformSeed } from './core/platform.seed'; import { CountriesSeed } from './mdm/countries.seed'; import { StatesSeed } from './mdm/states.seed'; import { CitiesSeed } from './mdm/cities.seed'; import { CurrenciesSeed } from './mdm/currencies.seed'; import { LanguagesSeed } from './mdm/languages.seed'; import { TimezonesSeed } from './mdm/timezones.seed'; import { UnitsOfMeasureSeed } from './mdm/units-of-measure.seed'; import { IndustriesSeed } from './mdm/industries.seed'; import { TaxMastersSeed } from './mdm/tax-masters.seed'; import { StatusReasonSeed } from './mdm/status-reason.seed'; import { DocumentTypesSeed } from './mdm/document-types.seed'; import { NumberSeriesSeed } from './mdm/number-series.seed'; @Injectable() export class SeedRunner { private readonly logger = new Logger(SeedRunner.name); constructor( @InjectConnection() private readonly connection: Connection, private readonly cleanupService: SeedCleanupService, private readonly validationService: SeedValidationService, private readonly platformSeed: PlatformSeed, private readonly countriesSeed: CountriesSeed, private readonly statesSeed: StatesSeed, private readonly citiesSeed: CitiesSeed, private readonly currenciesSeed: CurrenciesSeed, private readonly languagesSeed: LanguagesSeed, private readonly timezonesSeed: TimezonesSeed, private readonly unitsSeed: UnitsOfMeasureSeed, private readonly industriesSeed: IndustriesSeed, private readonly taxSeed: TaxMastersSeed, private readonly statusReasonSeed: StatusReasonSeed, private readonly docTypesSeed: DocumentTypesSeed, private readonly numberSeriesSeed: NumberSeriesSeed, ) {} async runSeed( scale: SeedScale = 'small', env: SeedEnv = 'dev', dryRun = false, ): Promise<any> { SeedSafetyGuard.validateEnvironment(); const context: SeedContext = { scale, env, dryRun, tenantPrefix: 'DEMO_', }; this.logger.log( `Starting Enterprise Seeding in environment [${env}] at scale [${scale}] (dryRun=${dryRun})...`, ); if (!dryRun) { // 1. Database cleanups await this.cleanupService.clearDatabase(context.tenantPrefix); } // Clear registry SeedIdRegistry.clear(); // 2. Core Seeding await this.platformSeed.seed(this.connection); await this.countriesSeed.seed(); await this.statesSeed.seed(); await this.citiesSeed.seed(); await this.currenciesSeed.seed(); await this.languagesSeed.seed(); await this.timezonesSeed.seed(); await this.unitsSeed.seed(); await this.industriesSeed.seed(); await this.taxSeed.seed(); await this.statusReasonSeed.seed(); await this.docTypesSeed.seed(this.connection); await this.numberSeriesSeed.seed(); // 3. Demo Tenant Seeding if (!dryRun) { await this.seedDemoTenants(context); } // 4. Validate const validation = await this.validationService.validateSeededData(); this.logger.log('Seeding execution finished successfully.'); return { status: 'success', validation, }; } private async seedDemoTenants(context: SeedContext): Promise<void> { const tenantsColl = this.connection.collection('tenants'); const usersColl = this.connection.collection('users'); const employeesColl = this.connection.collection('employees'); const contactsColl = this.connection.collection('contacts'); const dealsColl = this.connection.collection('deals'); const invoicesColl = this.connection.collection('invoices'); // 3 Distinct Demo Tenants const demoTenants = [ { slug: 'bevision-hq', name: 'Be-Vision Headquarters', domain: 'bevision.hq', }, { slug: 'greenfield-corp', name: 'Greenfield Manufacturing Corp', domain: 'greenfield.corp', }, { slug: 'doorstep-srv', name: 'Doorstep Household Services', domain: 'doorstep.services', }, ]; for (const t of demoTenants) { const tenantRes = await tenantsColl.findOneAndUpdate( { slug: t.slug }, { $set: { name: t.name, slug: t.slug, domain: t.domain, isActive: true, }, }, { upsert: true, returnDocument: 'after' }, ); const tenantId = (tenantRes as any)._id?.toString() || new Types.ObjectId().toString(); // Create Admin User await usersColl.updateOne( { email: `admin@${t.domain}` }, { $set: { email: `admin@${t.domain}`, firstName: 'Admin', lastName: t.name.split(' ')[0], tenantId, roles: ['ADMIN'], isActive: true, isEmailVerified: true, }, }, { upsert: true }, ); // Seed core Employees const empRes1 = await employeesColl.insertOne({ tenantId, firstName: 'Jane', lastName: 'Doe', email: `jane.doe@${t.domain}`, }); const empRes2 = await employeesColl.insertOne({ tenantId, firstName: 'John', lastName: 'Smith', email: `john.smith@${t.domain}`, }); // Seed some contacts, deals, invoices await contactsColl.insertOne({ tenantId, firstName: 'Bob', lastName: 'Vance', status: 'LEAD', }); await dealsColl.insertOne({ tenantId, title: 'Q1 Expansion Deal', value: 1200000, // 12,000 USD in cents }); await invoicesColl.insertOne({ tenantId, invoiceNumber: `INV-${t.slug.toUpperCase()}-0001`, total: 500000, }); this.logger.log(`Seeded demo tenant '${t.name}' (tenantId: ${tenantId})`); } } } |