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 | import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Tenant } from './schemas/tenant.schema'; import { User } from '../user/schemas/user.schema'; import { Subscription } from '../subscriptions/schemas/subscription.schema'; import { TenantModuleLicense } from '../auth/schemas/module.schema'; import { Contact } from '../../domains/crm/schemas/account-contact.schema'; import { Opportunity } from '../../domains/crm/schemas/opportunity.schema'; import { CrmProduct } from '../../domains/crm/schemas/product-pricing.schema'; import { BillingInvoice } from '../billing/schemas/billing.schema'; import { Project } from '../../domains/projects/schemas/project.schema'; import { Employee } from '../../domains/hr/employee/schemas/employee.schema'; @Injectable() export class TenantService { constructor( @InjectModel(Tenant.name) private readonly tenantModel: Model<Tenant>, @InjectModel(User.name) private readonly userModel: Model<User>, @InjectModel(Subscription.name) private readonly subscriptionModel: Model<Subscription>, @InjectModel(TenantModuleLicense.name) private readonly moduleLicenseModel: Model<TenantModuleLicense>, @InjectModel(Contact.name) private readonly contactModel: Model<Contact>, @InjectModel(Opportunity.name) private readonly opportunityModel: Model<Opportunity>, @InjectModel(CrmProduct.name) private readonly crmProductModel: Model<CrmProduct>, @InjectModel(BillingInvoice.name) private readonly invoiceModel: Model<BillingInvoice>, @InjectModel(Project.name) private readonly projectModel: Model<Project>, @InjectModel(Employee.name) private readonly employeeModel: Model<Employee>, ) {} async findById(id: string): Promise<any> { const tenant = await this.tenantModel.findById(id).lean().exec(); if (!tenant) return null; const [subscription, modules] = await Promise.all([ this.subscriptionModel.findOne({ tenantId: id }).lean().exec(), this.moduleLicenseModel .find({ tenantId: id, isActive: true }) .lean() .exec(), ]); return { ...tenant, id: (tenant as any)._id.toString(), subscription, modules, }; } async findBySlug(slug: string): Promise<any> { const tenant = await this.tenantModel.findOne({ slug }).lean().exec(); if (!tenant) return null; return { ...tenant, id: (tenant as any)._id.toString() }; } async findByDomain(domain: string): Promise<any> { const tenant = await this.tenantModel.findOne({ domain }).lean().exec(); if (!tenant) return null; return { ...tenant, id: (tenant as any)._id.toString() }; } async update( id: string, data: Partial<{ name: string; domain: string; logo: string; favicon: string; primaryColor: string; accentColor: string; }>, ): Promise<any> { const updated = await this.tenantModel .findByIdAndUpdate(id, data, { new: true }) .lean() .exec(); if (!updated) return null; return { ...updated, id: (updated as any)._id.toString() }; } async getStats(tenantId: string): Promise<any> { const [ userCount, contactCount, dealCount, invoiceCount, projectCount, employeeCount, ] = await Promise.all([ this.userModel.countDocuments({ tenantId }).exec(), this.contactModel.countDocuments({ tenantId }).exec(), this.opportunityModel.countDocuments({ tenantId }).exec(), this.invoiceModel.countDocuments({ tenantId }).exec(), this.projectModel.countDocuments({ tenantId }).exec(), this.employeeModel.countDocuments({ tenantId }).exec(), ]); return { users: userCount, contacts: contactCount, deals: dealCount, invoices: invoiceCount, projects: projectCount, employees: employeeCount, tickets: 0, }; } // --- BRANDING METHODS --- async getBranding(tenantId: string): Promise<any> { const tenant = await this.tenantModel.findById(tenantId).lean().exec(); if (!tenant) return null; return { logo: tenant.logo, favicon: tenant.favicon, primaryColor: tenant.primaryColor, accentColor: tenant.accentColor, domain: tenant.domain, }; } async updateBranding( tenantId: string, branding: { logo?: string; favicon?: string; primaryColor?: string; accentColor?: string; domain?: string; }, ): Promise<any> { return this.tenantModel .findByIdAndUpdate(tenantId, { $set: branding }, { new: true }) .lean() .exec(); } } |