import { Injectable } from '@nestjs/common'; import { TypeOrmCrudService } from '@nestjsx/crud-typeorm'; import { Unit } from '../entities/unit.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, Transaction, TransactionRepository } from 'typeorm'; import { UnitDto } from './unit.dto'; @Injectable() export class UnitsService extends TypeOrmCrudService { constructor( @InjectRepository(Unit) public unitsRepository: Repository, ) { super(unitsRepository); } async markDeleted(id: number) { await this.unitsRepository.softDelete({ id }); return; } @Transaction() async bulkUpdate( data: Array>, @TransactionRepository(Unit) fr?: Repository, ) { const hasUpdatesWithoutId = !!data.find(d => !('id' in d)); if (hasUpdatesWithoutId) return this.throwBadRequestException('id is required'); const ops = data .map(f => { const change = Object.keys(f).reduce((c, key) => { if (f[key] !== undefined) { c[key] = f[key]; } return c; }, {}); return fr.update(f.id, change); }) .filter(r => r instanceof Promise); return await Promise.all(ops); } }