Skip to content

Commit 44f4e6b

Browse files
authored
fix(condo): DOMA-11246 split overcomplicated query for payments sum (#5915)
* fix(condo): DOMA-11246 split overcomplicated query for payments sum * fix(condo): DOMA-11246 remove env flag. believe in query
1 parent 37dc437 commit 44f4e6b

File tree

1 file changed

+19
-14
lines changed
  • apps/condo/domains/billing/utils/serverSchema

1 file changed

+19
-14
lines changed

apps/condo/domains/billing/utils/serverSchema/index.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ const getPaymentsSum = async (receiptId) => {
6767
*/
6868
const getNewPaymentsSum = async (receiptId) => {
6969
const receipt = await getById('BillingReceipt', receiptId)
70-
const billingContext = await getById('BillingIntegrationOrganizationContext', receipt.context)
71-
const account = await getById('BillingAccount', receipt.account)
7270
const defaultConditions = [
7371
{ status_in: [PAYMENT_DONE_STATUS, PAYMENT_WITHDRAWN_STATUS] },
7472
{ deletedAt: null },
@@ -86,20 +84,27 @@ const getNewPaymentsSum = async (receiptId) => {
8684
}
8785
const conditionsByReceipt = [
8886
{ receipt: { id: receiptId } },
87+
...defaultConditions,
8988
]
90-
const conditionsWithNoReceipt = [
91-
{ receipt_is_null: true },
92-
{ invoice_is_null: true },
93-
{ organization: { id: billingContext.organization } },
94-
{ period: receipt.period },
95-
{ accountNumber: account.number },
96-
]
97-
const payments = await find('Payment', {
98-
AND: [
99-
{ AND: defaultConditions },
100-
{ OR: [{ AND: conditionsByReceipt }, { AND: conditionsWithNoReceipt }] },
101-
],
89+
let payments = await find('Payment', {
90+
AND: conditionsByReceipt,
10291
})
92+
const billingContext = await getById('BillingIntegrationOrganizationContext', receipt.context)
93+
const account = await getById('BillingAccount', receipt.account)
94+
if (billingContext && account) {
95+
const conditionsWithNoReceipt = [
96+
{ receipt_is_null: true },
97+
{ invoice_is_null: true },
98+
{ organization: { id: billingContext.organization } },
99+
{ period: receipt.period },
100+
{ accountNumber: account.number },
101+
...defaultConditions,
102+
]
103+
const qrPayments = await find('Payment', {
104+
AND: conditionsWithNoReceipt,
105+
})
106+
payments = payments.concat(qrPayments)
107+
}
103108
return payments.reduce((total, current) => (Big(total).plus(current.amount)), 0).toFixed(8).toString()
104109
}
105110

0 commit comments

Comments
 (0)