# All files / modules/tenants tenants.service.ts

100% Statements 28/28
100% Branches 11/11
100% Functions 6/6
100% Lines 26/26

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
    852x

    2x
    2x
    2x

    2x

    16x

    8x

    7x
    3x

    4x

    10x

    5x
    5x
    2x

    3x

    3x

    4x

    3x
    2x

    3x
    1x

    3x
    2x

    3x

    2x
    2x

     import {
      Injectable,
      NotFoundException,
      ConflictException,
    } from '@nestjs/common';
    import { InjectRepository } from '@nestjs/typeorm';
    import { Repository } from 'typeorm';
    import { Tenant } from './entities/tenant.entity';
    import { CreateTenantDto, UpdateTenantDto } from './dto';

    @Injectable()
    export class TenantsService {
      constructor(
        @InjectRepository(Tenant)
        private readonly tenantRepository: Repository<Tenant>,
      ) {}

      async findOne(id: string): Promise<Tenant> {
        const tenant = await this.tenantRepository.findOne({
          where: { id },
        });

        if (!tenant) {
          throw new NotFoundException('Tenant no encontrado');
        }

        return tenant;
      }

      async findBySlug(slug: string): Promise<Tenant | null> {
        return this.tenantRepository.findOne({
          where: { slug },
        });
      }

      async create(dto: CreateTenantDto): Promise<Tenant> {
        // Check if slug already exists
        const existingTenant = await this.findBySlug(dto.slug);
        if (existingTenant) {
          throw new ConflictException('Ya existe un tenant con este slug');
        }

        // Create tenant with pending status (for onboarding flow)
        const tenant = this.tenantRepository.create({
          name: dto.name,
          slug: dto.slug,
          domain: dto.domain || null,
          logo_url: dto.logo_url || null,
          status: 'trial', // Default to trial for new tenants
          settings: dto.settings || {},
          trial_ends_at: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000), // 14 days trial
        });

        return this.tenantRepository.save(tenant);
      }

      async update(id: string, dto: UpdateTenantDto): Promise<Tenant> {
        const tenant = await this.findOne(id);

        // Update basic fields if provided
        if (dto.name !== undefined) {
          tenant.name = dto.name;
        }

        if (dto.logo_url !== undefined) {
          tenant.logo_url = dto.logo_url;
        }

        // Merge settings if provided (partial update)
        if (dto.settings !== undefined) {
          tenant.settings = {
            ...tenant.settings,
            ...dto.settings,
          };
        }

        return this.tenantRepository.save(tenant);
      }

      async slugExists(slug: string): Promise<boolean> {
        const tenant = await this.findBySlug(slug);
        return !!tenant;
      }
    }