update invoice entity

This commit is contained in:
2026-02-19 11:38:44 +03:30
parent c05e9187df
commit 6836d34967
4 changed files with 33 additions and 15 deletions
+21 -9
View File
@@ -6,6 +6,7 @@ import { FindInvoicesDto } from './dto/find-invoices.dto';
import { Invoice } from './entities/invoice.entity';
import { InvoiceItem } from './entities/invoice-item.entity';
import { Request } from '../request/entities/request.entity';
import { User } from '../user/entities/user.entity';
import { ProductService } from '../product/providers/product.service';
import { InvoiceRepository } from './repositories/invoice.repository';
@@ -21,13 +22,24 @@ export class InvoiceService {
async create(dto: CreateInvoiceDto) {
const request = await this.em.findOne(
Request,
{ id: dto.requestId },
{ populate: ['user'] },
);
if (!request) {
throw new NotFoundException('Request not found');
let user: User;
let request: Request | undefined;
if (dto.requestId) {
const requestEntity = await this.em.findOne(
Request,
{ id: dto.requestId },
{ populate: ['user'] },
);
if (!requestEntity) {
throw new NotFoundException('Request not found');
}
user = requestEntity.user;
request = requestEntity;
} else if (dto.userId) {
user = await this.em.findOneOrFail(User, { id: dto.userId });
} else {
throw new BadRequestException('Either requestId or userId must be provided');
}
const enableTax = dto.enableTax ?? false;
@@ -39,8 +51,8 @@ export class InvoiceService {
return this.em.transactional(async (em) => {
const invoice = em.create(Invoice, {
user: request.user,
request,
user,
...(request && { request }),
enableTax,
approvalDeadline,
discount,