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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | import { Injectable, BadRequestException, NotFoundException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { OnboardingDefinition, OnboardingStepDefinition, TenantOnboarding, TenantOnboardingStep, OnboardingChecklistItem, } from './schemas/onboarding.schema'; import { AuditLogService } from '../audit/audit-log.service'; import { EventBusService } from '../events/event-bus.service'; @Injectable() export class OnboardingService { constructor( @InjectModel(OnboardingDefinition.name) private readonly defModel: Model<OnboardingDefinition>, @InjectModel(OnboardingStepDefinition.name) private readonly stepDefModel: Model<OnboardingStepDefinition>, @InjectModel(TenantOnboarding.name) private readonly onboardingModel: Model<TenantOnboarding>, @InjectModel(TenantOnboardingStep.name) private readonly stepModel: Model<TenantOnboardingStep>, @InjectModel(OnboardingChecklistItem.name) private readonly checklistModel: Model<OnboardingChecklistItem>, private readonly auditLog: AuditLogService, private readonly eventBus: EventBusService, ) {} async seedDefinitions() { const existing = await this.defModel.findOne({ key: 'standard' }).exec(); if (existing) return; await this.defModel.create({ key: 'standard', name: 'Standard SaaS Onboarding', }); const steps = [ { stepKey: 'company_profile', title: 'Company Profile', sequence: 1, isRequired: true, }, { stepKey: 'branch_setup', title: 'Branch Setup', sequence: 2, isRequired: true, }, { stepKey: 'invite_employees', title: 'Invite Employees', sequence: 3, isRequired: false, }, { stepKey: 'module_selection', title: 'ERP Module Selection', sequence: 4, isRequired: true, }, { stepKey: 'branding_setup', title: 'White-Label Branding', sequence: 5, isRequired: false, }, ]; await this.stepDefModel.insertMany( steps.map((s) => ({ ...s, definitionKey: 'standard' })), ); } async getSteps(tenantId: string): Promise<any[]> { await this.seedDefinitions(); const steps = await this.stepDefModel .find({ definitionKey: 'standard' }) .sort({ sequence: 1 }) .lean() .exec(); const progressSteps = await this.stepModel.find({ tenantId }).lean().exec(); return steps.map((def) => { const prog = progressSteps.find((p) => p.stepKey === def.stepKey); return { ...def, status: prog?.status || 'PENDING', data: prog?.data || null, completedAt: prog?.completedAt || null, }; }); } async getProgress(tenantId: string): Promise<any> { let onboarding = await this.onboardingModel.findOne({ tenantId }).exec(); if (!onboarding) { onboarding = await this.onboardingModel.create({ tenantId, definitionKey: 'standard', currentStepKey: 'company_profile', }); } const steps = await this.getSteps(tenantId); const completedCount = steps.filter( (s) => s.status === 'COMPLETED' || s.status === 'SKIPPED', ).length; const completionPercentage = steps.length ? Math.round((completedCount / steps.length) * 100) : 0; onboarding.completionPercentage = completionPercentage; await onboarding.save(); return { currentStepKey: onboarding.currentStepKey, completionPercentage, status: onboarding.status, steps, }; } async saveStep( tenantId: string, stepKey: string, data: any, userId: string, ): Promise<any> { const stepDef = await this.stepDefModel .findOne({ definitionKey: 'standard', stepKey }) .exec(); if (!stepDef) throw new NotFoundException( `Onboarding step definition not found: ${stepKey}`, ); const step = await this.stepModel .findOneAndUpdate( { tenantId, stepKey }, { tenantId, stepKey, status: 'COMPLETED', data, completedAt: new Date(), }, { upsert: true, new: true }, ) .lean() .exec(); // Advance onboarding current step const steps = await this.stepDefModel .find({ definitionKey: 'standard' }) .sort({ sequence: 1 }) .lean() .exec(); const currentIndex = steps.findIndex((s) => s.stepKey === stepKey); const nextStep = steps[currentIndex + 1]; if (nextStep) { await this.onboardingModel .updateOne({ tenantId }, { $set: { currentStepKey: nextStep.stepKey } }) .exec(); } await this.eventBus.publish( 'platform.onboarding.step-completed.v1', { tenantId, stepKey, }, tenantId, ); return step; } async skipStep(tenantId: string, stepKey: string): Promise<any> { const stepDef = await this.stepDefModel .findOne({ definitionKey: 'standard', stepKey }) .exec(); if (!stepDef) throw new NotFoundException('Step definition not found'); if (stepDef.isRequired) throw new BadRequestException('Cannot skip a required onboarding step'); return this.stepModel .findOneAndUpdate( { tenantId, stepKey }, { tenantId, stepKey, status: 'SKIPPED', completedAt: new Date() }, { upsert: true, new: true }, ) .lean() .exec(); } async completeOnboarding(tenantId: string, userId: string): Promise<any> { const steps = await this.getSteps(tenantId); const incompleteRequired = steps.find( (s) => s.isRequired && s.status === 'PENDING', ); if (incompleteRequired) { throw new BadRequestException( `Cannot complete onboarding. Required step '${incompleteRequired.title}' is incomplete.`, ); } const onboarding = await this.onboardingModel .findOneAndUpdate( { tenantId }, { $set: { status: 'COMPLETED', completionPercentage: 100 } }, { new: true }, ) .lean() .exec(); await this.auditLog.log({ tenantId, userId, action: 'ONBOARDING_COMPLETE', resource: 'TenantOnboarding', resourceId: tenantId, }); await this.eventBus.publish( 'platform.onboarding.completed.v1', { tenantId, }, tenantId, ); return onboarding; } } |