remove item
This commit is contained in:
@@ -150,14 +150,29 @@ export class InvoiceService {
|
||||
return item;
|
||||
}
|
||||
|
||||
async update(id: string, updateInvoiceDto: UpdateInvoiceDto) {
|
||||
async update(id: string, dto: UpdateInvoiceDto) {
|
||||
const invoice = await this.findOneOrFail(id);
|
||||
if (updateInvoiceDto.enableTax !== undefined) invoice.enableTax = updateInvoiceDto.enableTax;
|
||||
if (updateInvoiceDto.approvalDeadline !== undefined) {
|
||||
invoice.approvalDeadline = new Date(updateInvoiceDto.approvalDeadline);
|
||||
if (dto.enableTax !== undefined) invoice.enableTax = dto.enableTax;
|
||||
if (dto.approvalDeadline !== undefined) {
|
||||
invoice.approvalDeadline = new Date(dto.approvalDeadline);
|
||||
}
|
||||
if (updateInvoiceDto.items !== undefined && updateInvoiceDto.items.length > 0) {
|
||||
for (const item of updateInvoiceDto.items) {
|
||||
if(dto.paymentMethod !== undefined) invoice.paymentMethod = dto.paymentMethod;
|
||||
if(dto.description !== undefined) invoice.description = dto.description;
|
||||
// if(updateInvoiceDto.attachments !== undefined) invoice.attachments = updateInvoiceDto.attachments;
|
||||
|
||||
if (dto.items !== undefined) {
|
||||
// Items in dto.items represent the full list: delete any existing items not in the list
|
||||
const idsToKeep = new Set(
|
||||
dto.items.filter((i) => i.id).map((i) => i.id),
|
||||
);
|
||||
const existingItems = invoice.items.getItems();
|
||||
for (const existingItem of existingItems) {
|
||||
if (!idsToKeep.has(existingItem.id)) {
|
||||
invoice.items.remove(existingItem);
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of dto.items) {
|
||||
if (item.id) {
|
||||
const existingItem = await this.em.findOne(InvoiceItem, {
|
||||
id: item.id,
|
||||
|
||||
Reference in New Issue
Block a user