All files / src/custom-variables custom-variables.service.ts

36% Statements 9/25
0% Branches 0/1
22.22% Functions 2/9
31.81% Lines 7/22

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 7622x 22x 22x 22x             22x     6x                                     7x                                                                                      
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CustomVariable } from '../core-entities';
import {
  CreateCustomVariableDto,
  UpdateCustomVariableDto,
} from './dto/custom-variable.dto';
 
@Injectable()
export class CustomVariablesService {
  constructor(
    @InjectRepository(CustomVariable)
    private customVariablesRepository: Repository<CustomVariable>,
  ) {}
 
  async create(
    createCustomVariableDto: CreateCustomVariableDto,
  ): Promise<CustomVariable> {
    const variable = this.customVariablesRepository.create(
      createCustomVariableDto,
    );
    return this.customVariablesRepository.save(variable);
  }
 
  async findAll(): Promise<CustomVariable[]> {
    return this.customVariablesRepository.find({
      order: { order_number: 'ASC', key: 'ASC' },
    });
  }
 
  async findAllEnabled(): Promise<CustomVariable[]> {
    return this.customVariablesRepository.find({
      where: { enabled: true },
      order: { order_number: 'ASC', key: 'ASC' },
    });
  }
 
  async findOne(id: string): Promise<CustomVariable> {
    const variable = await this.customVariablesRepository.findOneBy({ id });
    Iif (!variable) {
      throw new NotFoundException(`CustomVariable with ID "${id}" not found`);
    }
    return variable;
  }
 
  async update(
    id: string,
    updateCustomVariableDto: UpdateCustomVariableDto,
  ): Promise<CustomVariable> {
    const variable = await this.findOne(id);
    this.customVariablesRepository.merge(variable, updateCustomVariableDto);
    return this.customVariablesRepository.save(variable);
  }
 
  async remove(id: string): Promise<void> {
    const variable = await this.findOne(id);
    await this.customVariablesRepository.remove(variable);
  }
 
  async reorder(orderedIds: string[]): Promise<void> {
    // We use a transaction to ensure all updates are consistent
    await this.customVariablesRepository.manager.transaction(
      async (transactionalEntityManager) => {
        for (let i = 0; i < orderedIds.length; i++) {
          await transactionalEntityManager.update(
            CustomVariable,
            orderedIds[i],
            { order_number: i },
          );
        }
      },
    );
  }
}