File

src/case-of-success/case-of-success.service.ts

Index

Methods

Constructor

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

Methods

Async deleteCaseOfSuccess
deleteCaseOfSuccess(id: any)
Parameters :
Name Type Optional
id any No
Returns : unknown
Async findAllCaseOfSuccess
findAllCaseOfSuccess()
Returns : unknown
Async saveCaseOfSuccess
saveCaseOfSuccess(caseofsuccess: any, resultOfService: any)
Parameters :
Name Type Optional
caseofsuccess any No
resultOfService any No
Returns : unknown
Async updateCaseOfSuccess
updateCaseOfSuccess(id: number, caseofsuccess: any, resultOfService: any)
Parameters :
Name Type Optional
id number No
caseofsuccess any No
resultOfService any No
Returns : unknown
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CaseOfSuccessEntity } from 'src/database/entity/case-of-success/case-of-success-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 CaseOfSuccessService {

    constructor(
        @InjectRepository(ServicesEntity) private readonly serviceRp:Repository<ServicesEntity>,
        @InjectRepository(ResultOfServiceEntity) private readonly resultofServiceRp:Repository<ResultOfServiceEntity>,
        @InjectRepository(CaseOfSuccessEntity) private readonly caseOfSuccessRp:Repository<CaseOfSuccessEntity>,
        private datasource:DataSource){
    }

    //Guardar Caso de exito
    async saveCaseOfSuccess(caseofsuccess:any,resultOfService:any){
       try {
            let resultTransaction;
            let response;
            await this.datasource.manager.transaction(async (transactionalEntityManager) => {
    
                resultTransaction = await transactionalEntityManager.withRepository(this.caseOfSuccessRp).save(caseofsuccess);
    
                resultOfService.forEach( async instance  => {
                    const currentResultOfSErvice = transformEntityAndAddId(instance, resultTransaction.id,"ResultOfServiceEntity")
                    await transactionalEntityManager.withRepository(this.resultofServiceRp).save(currentResultOfSErvice);
                });
    
                response = await transactionalEntityManager.withRepository(this.caseOfSuccessRp).findOneOrFail({where:{id:resultTransaction.id},relations:{resultOfService:true}});
    
            });
     
            return response;
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }

    //Actualizar Caso de exito
    async updateCaseOfSuccess(id:number, caseofsuccess:any,resultOfService:any){

        try {
            let resultTransaction;
            let response;

            await this.datasource.manager.transaction(async (transactionalEntityManager) => {

                await transactionalEntityManager.withRepository(this.resultofServiceRp).createQueryBuilder('resultofservice')
                .delete()
                .from("resultofservice")
                .where("caseOfSuccessId = :caseOfSuccessId", { caseOfSuccessId: id })
                .execute();

                resultTransaction = await transactionalEntityManager.withRepository(this.caseOfSuccessRp).update(id,caseofsuccess);

                resultOfService.forEach( async instance  => {
                    const currentResultOfSErvice = transformEntityAndAddId(instance, id,"ResultOfServiceEntity")
                    await transactionalEntityManager.withRepository(this.resultofServiceRp).save(currentResultOfSErvice);
                });

                response = await transactionalEntityManager.withRepository(this.caseOfSuccessRp).findOneOrFail({where:{id:id},relations:{resultOfService:true}});

            });

            return response;
            
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    
        
    }


    
    //Listar Caso de exitos
    async findAllCaseOfSuccess(){
        try {
            return await this.caseOfSuccessRp.find({order:{sort:'ASC'},relations:{resultOfService:true,service:true},select: {
                service: {
                  id: true,
                  name:true,
                
                }
              }});
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }

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


  


}


results matching ""

    No results matching ""