src/services/services.service.ts
Methods |
|
constructor(serviceRp: Repository<ServicesEntity>, resultofServiceRp: Repository<ResultOfServiceEntity>, customerRp: Repository<CustomerEntity>, datasource: DataSource)
|
|||||||||||||||
Defined in src/services/services.service.ts:11
|
|||||||||||||||
Parameters :
|
Async deleteService | ||||||
deleteService(id: any)
|
||||||
Defined in src/services/services.service.ts:90
|
||||||
Parameters :
Returns :
unknown
|
Async findAllServices |
findAllServices()
|
Defined in src/services/services.service.ts:81
|
Returns :
unknown
|
Async findlBySlug | ||||||
findlBySlug(slug: any)
|
||||||
Defined in src/services/services.service.ts:106
|
||||||
Parameters :
Returns :
unknown
|
Async saveService |
saveService(service: any, customer: any)
|
Defined in src/services/services.service.ts:21
|
Returns :
unknown
|
Async updateService |
updateService(id: number, service: any, customer: any)
|
Defined in src/services/services.service.ts:48
|
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);
}
}
}