remove relation between request and invoice
This commit is contained in:
@@ -17,7 +17,6 @@ import { ulid } from 'ulid';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
import { InvoiceConfirmStatusEnum } from '../enum/invoice-confirm-status.enum';
|
||||
import { InvoiceItem } from './invoice-item.entity';
|
||||
import { Request } from 'src/modules/request/entities/request.entity';
|
||||
import { IAttachment } from 'src/modules/order/interface/order.interface';
|
||||
|
||||
|
||||
@@ -32,8 +31,8 @@ export class Invoice extends BaseEntity {
|
||||
@ManyToOne(() => User)
|
||||
user!: User;
|
||||
|
||||
@ManyToOne(() => Request, { nullable: true })
|
||||
request?: Request;
|
||||
@Property({ type: 'string', columnType: 'char(26)', nullable: true })
|
||||
requestId?: string;
|
||||
|
||||
@OneToMany(() => Payment, payment => payment.invoice, {
|
||||
cascade: [Cascade.ALL],
|
||||
|
||||
@@ -58,7 +58,6 @@ export class InvoiceService {
|
||||
|
||||
async create(dto: CreateInvoiceDto) {
|
||||
let user: User;
|
||||
let request: Request | undefined;
|
||||
|
||||
if (dto.requestId) {
|
||||
const requestEntity = await this.em.findOne(
|
||||
@@ -71,14 +70,13 @@ export class InvoiceService {
|
||||
}
|
||||
|
||||
const existingInvoice = await this.em.findOne(Invoice, {
|
||||
request: { id: dto.requestId },
|
||||
requestId: dto.requestId,
|
||||
});
|
||||
if (existingInvoice) {
|
||||
throw new BadRequestException('An invoice already exists for this request');
|
||||
}
|
||||
|
||||
user = requestEntity.user;
|
||||
request = requestEntity;
|
||||
} else if (dto.userId) {
|
||||
user = await this.em.findOneOrFail(User, { id: dto.userId });
|
||||
} else {
|
||||
@@ -95,7 +93,7 @@ export class InvoiceService {
|
||||
const invoice = await this.em.transactional(async (em) => {
|
||||
const invoice = em.create(Invoice, {
|
||||
user,
|
||||
...(request && { request }),
|
||||
...(dto.requestId && { requestId: dto.requestId }),
|
||||
enableTax,
|
||||
approvalDeadline,
|
||||
discount: 0,
|
||||
@@ -183,7 +181,7 @@ export class InvoiceService {
|
||||
const invoice = await this.em.findOne(
|
||||
Invoice,
|
||||
{ id, user: { id: userId } },
|
||||
{ populate: ['user', 'request', 'items', 'items.product'] },
|
||||
{ populate: ['user', 'items', 'items.product'] },
|
||||
);
|
||||
if (!invoice) {
|
||||
throw new NotFoundException('Invoice not found');
|
||||
@@ -195,7 +193,7 @@ export class InvoiceService {
|
||||
const invoice = await this.em.findOne(
|
||||
Invoice,
|
||||
{ id },
|
||||
{ populate: ['user', 'request', 'items', 'items.product'] },
|
||||
{ populate: ['user', 'items', 'items.product'] },
|
||||
);
|
||||
if (!invoice) {
|
||||
throw new NotFoundException('Invoice not found');
|
||||
@@ -351,7 +349,7 @@ export class InvoiceService {
|
||||
const invoice = await this.em.findOne(
|
||||
Invoice,
|
||||
{ id },
|
||||
{ populate: ['user', 'request', 'items', 'items.product'] },
|
||||
{ populate: ['user', 'items', 'items.product'] },
|
||||
);
|
||||
if (!invoice) {
|
||||
throw new NotFoundException('Invoice not found');
|
||||
|
||||
@@ -39,7 +39,7 @@ export class InvoiceRepository extends EntityRepository<Invoice> {
|
||||
}
|
||||
|
||||
if (requestId) {
|
||||
where.request = { id: requestId };
|
||||
where.requestId = requestId;
|
||||
}
|
||||
|
||||
if (typeof enableTax === 'boolean') {
|
||||
@@ -80,7 +80,7 @@ export class InvoiceRepository extends EntityRepository<Invoice> {
|
||||
limit,
|
||||
offset,
|
||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||
populate: ['user', 'request', 'items', 'items.product'],
|
||||
populate: ['user', 'items', 'items.product'],
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
@@ -13,7 +13,6 @@ import { User } from '../../user/entities/user.entity';
|
||||
import { ulid } from 'ulid';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
import { RequestItem } from './request-item.entity';
|
||||
import { Invoice } from 'src/modules/invoice/entities/invoice.entity';
|
||||
|
||||
@Entity({ tableName: 'requests' })
|
||||
@Index({ properties: ['user'] })
|
||||
@@ -27,9 +26,6 @@ export class Request extends BaseEntity {
|
||||
@ManyToOne(() => User)
|
||||
user!: User;
|
||||
|
||||
@OneToMany(()=>Invoice, invoice => invoice.request)
|
||||
invoices = new Collection<Invoice>(this);
|
||||
|
||||
@OneToMany(() => RequestItem, item => item.request, {
|
||||
cascade: [Cascade.ALL],
|
||||
orphanRemoval: true,
|
||||
|
||||
@@ -65,7 +65,7 @@ export class RequestRepository extends EntityRepository<Request> {
|
||||
limit,
|
||||
offset,
|
||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||
populate: ['items', 'items.product', 'user','invoices'],
|
||||
populate: ['items', 'items.product', 'user'],
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
@@ -221,7 +221,7 @@ export class RequestService {
|
||||
|
||||
private async hardDeleteRequest(id: string, em: EntityManager) {
|
||||
await this.chatService.deleteChatsByRefId(id, em);
|
||||
await em.nativeUpdate(Invoice, { request: { id } }, { request: null });
|
||||
await em.nativeUpdate(Invoice, { requestId: id }, { requestId: null });
|
||||
await em.nativeDelete(RequestItem, { request: { id } });
|
||||
await em.nativeDelete(Request, { id });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user