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