File

src/services/services.service.ts

Index

Methods

Constructor

constructor(serviceRp: Repository<ServicesEntity>, resultofServiceRp: Repository<ResultOfServiceEntity>, customerRp: Repository<CustomerEntity>, datasource: DataSource)
Parameters :
Name Type Optional
serviceRp Repository<ServicesEntity> No
resultofServiceRp Repository<ResultOfServiceEntity> No
customerRp Repository<CustomerEntity> No
datasource DataSource No

Methods

Async deleteService
deleteService(id: any)
Parameters :
Name Type Optional
id any No
Returns : unknown
Async findAllServices
findAllServices()
Returns : unknown
Async findlBySlug
findlBySlug(slug: any)
Parameters :
Name Type Optional
slug any No
Returns : unknown
Async saveService
saveService(service: any, customer: any)
Parameters :
Name Type Optional
service any No
customer any No
Returns : unknown
Async updateService
updateService(id: number, service: any, customer: any)
Parameters :
Name Type Optional
id number No
service any No
customer any No
Returns : unknown
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CustomerEntity } from 'src/database/entity/customer-entity/customer-entity';
import { ResultOfServiceEntity } from 'src/database/entity/result-of-service-entity/result-of-service-entity';
import { ServicesEntity } from 'src/database/entity/services-entity/services-entity';
import { transformEntityAndAddId } from 'src/utils/utils';
import { ExceptionErrorMessage } from 'src/validation/exception-error';
import { DataSource, Repository } from 'typeorm';

@Injectable()
export class ServicesService {
    constructor(
        @InjectRepository(ServicesEntity) private readonly serviceRp:Repository<ServicesEntity>,
        @InjectRepository(ResultOfServiceEntity) private readonly resultofServiceRp:Repository<ResultOfServiceEntity>,
        @InjectRepository(CustomerEntity) private readonly customerRp:Repository<CustomerEntity>,
        private datasource:DataSource){
    }

    
    //Guardar Servicio
    async saveService(service:any,customer:any){
        try {
            let resultTransaction;
            let response;
            await this.datasource.manager.transaction(async (transactionalEntityManager) => {
    
                resultTransaction = await transactionalEntityManager.withRepository(this.serviceRp).save(service);
    
    
                customer.forEach( async instance  => {
                    const currentCustomer = transformEntityAndAddId(instance, resultTransaction.id,"CustomerEntity","service")
                    await transactionalEntityManager.withRepository(this.customerRp).save(currentCustomer);
                });
    
                response = await transactionalEntityManager.withRepository(this.serviceRp).findOneOrFail({where:{id:resultTransaction.id},relations:{customer:true}});
    
            });
     
            return response;
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }

  

    //Actualizar Servicio
    async updateService(id:number, service:any,customer:any){
        try {
            let resultTransaction;
            let response;
    
            await this.datasource.manager.transaction(async (transactionalEntityManager) => {
    
                await transactionalEntityManager.withRepository(this.customerRp).createQueryBuilder('customer')
                .delete()
                .from("customer")
                .where("serviceId = :serviceId", { serviceId: id })
                .execute();
    
                resultTransaction = await transactionalEntityManager.withRepository(this.serviceRp).update(id,service);
    
    
                customer.forEach( async instance  => {
                    const currentCustomer = transformEntityAndAddId(instance, id,"CustomerEntity","service")
                    await transactionalEntityManager.withRepository(this.customerRp).save(currentCustomer);
                });
    
                response = await transactionalEntityManager.withRepository(this.serviceRp).findOneOrFail({where:{id:id},relations:{caseofsuccess:true,customer:true}});
    
            });
    
            return response;
            
        } catch (error) {
            ExceptionErrorMessage(error);            
        }

    }
    //Listar Servicios
    async findAllServices(){
        try {
            return await this.serviceRp.find({order:{sort:'ASC'},relations:['customer','caseofsuccess','caseofsuccess.resultOfService']});
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }

    //Borrar Servicio
    async deleteService(id:any){
        try {
            await this.serviceRp.createQueryBuilder()
                .update(ServicesEntity)
                .set({ status: false})
                .where("id = :id", { id: id})
                .execute();
            return {message:"Se ha modificado el registro seleccionado"};
        }
        catch(error){
            ExceptionErrorMessage(error);            
        }
    }


    //Listado mediante slug 
    async findlBySlug(slug:any){
        try {
            return await this.serviceRp.findOne({relations:['customer','caseofsuccess','caseofsuccess.resultOfService'],where:{slug:slug}});
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }   

}

results matching ""

    No results matching ""