Skip to content

Commit

Permalink
fix: deleting products or containers causing queries to fail (#461) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSamuel authored Feb 12, 2025
1 parent b77b709 commit 6b677e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/service/invoice-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ export default class InvoiceService extends WithManager {

const transactions = await this.manager.find(Transaction, { where: { id: In(tIds),
subTransactions: { subTransactionRows: { invoice: false } } },
relations });
relations,
withDeleted: true });

const response: Promise<TransactionResponse>[] = [];
transactions.forEach((t) => response.push(transactionService.asTransactionResponse(t)));
Expand Down
32 changes: 12 additions & 20 deletions src/service/transaction-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
} from '../helpers/transaction-mapper';
import ProductCategoryService from './product-category-service';
import WithManager from '../database/with-manager';
import ProductService from './product-service';

export interface TransactionFilterParameters {
transactionId?: number | number[],
Expand Down Expand Up @@ -137,12 +138,9 @@ export default class TransactionService extends WithManager {
public async getTotalCost(rows: SubTransactionRowRequest[]): Promise<Dinero.Dinero> {
// get costs of individual rows
const rowCosts = await Promise.all(rows.map(async (row) => {
const rowCost = await this.manager.findOne(ProductRevision, {
where: {
revision: row.product.revision,
product: { id: row.product.id },
},
}).then((product) => product.priceInclVat.multiply(row.amount));
const options = await ProductService.getOptions({ productRevision: row.product.revision, productId: row.product.id });
const rowCost = await this.manager.findOne(ProductRevision, options)
.then((product) => product.priceInclVat.multiply(row.amount));

return rowCost;
}));
Expand Down Expand Up @@ -205,13 +203,9 @@ export default class TransactionService extends WithManager {
}

// check if product exists
const product = await this.manager.findOne(ProductRevision, {
where: {
revision: req.product.revision,
product: { id: req.product.id, deletedAt: IsNull() },
},
relations: ['product'],
});
const options = await ProductService.getOptions({ productRevision: req.product.revision, productId: req.product.id });
options.withDeleted = false;
const product = await this.manager.findOne(ProductRevision, options);
if (!product) {
return false;
}
Expand Down Expand Up @@ -443,6 +437,8 @@ export default class TransactionService extends WithManager {
revision: req.container.revision,
container: { id: req.container.id },
},
withDeleted: true,
relations: ['container'],
});

// sub transaction rows
Expand Down Expand Up @@ -504,13 +500,9 @@ export default class TransactionService extends WithManager {
if (!req) {
return undefined;
}
const product = await this.manager.findOne(ProductRevision, {
where: {
revision: req.product.revision,
product: { id: req.product.id },
},
relations: ['vat'],
});

const options = await ProductService.getOptions({ productRevision: req.product.revision, productId: req.product.id });
const product = await this.manager.findOne(ProductRevision, options);
return { product, amount: req.amount, subTransaction } as SubTransactionRow;
}

Expand Down

0 comments on commit 6b677e6

Please sign in to comment.