refactor: query into relation instead
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 6s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 6s
This commit is contained in:
parent
ae4488b76a
commit
422183b1c1
1 changed files with 23 additions and 43 deletions
|
|
@ -18,6 +18,7 @@ import { precisionRound } from "../utils/arithmetic";
|
|||
import dayjs from "dayjs";
|
||||
import { json2csv } from "json-2-csv";
|
||||
import { isSystem } from "../utils/keycloak";
|
||||
import { jsonObjectFrom } from "kysely/helpers/postgres";
|
||||
|
||||
const permissionCondCompany = createPermCondition((_) => true);
|
||||
const permissionQueryCondCompany = createQueryPermissionCondition((_) => true);
|
||||
|
|
@ -651,19 +652,21 @@ export class StatsController extends Controller {
|
|||
.leftJoin("Payment", "Invoice.id", "Payment.invoiceId")
|
||||
.leftJoin("CustomerBranch", "CustomerBranch.id", "Quotation.customerBranchId")
|
||||
.leftJoin("Customer", "Customer.id", "CustomerBranch.customerId")
|
||||
.select([
|
||||
"CustomerBranch.id as customerBranchId",
|
||||
"CustomerBranch.code as customerBranchCode",
|
||||
"CustomerBranch.registerName as customerBranchRegisterName",
|
||||
"CustomerBranch.registerNameEN as customerBranchRegisterNameEN",
|
||||
"CustomerBranch.firstName as customerBranchFirstName",
|
||||
"CustomerBranch.firstNameEN as customerBranchFirstNameEN",
|
||||
"CustomerBranch.lastName as customerBranchLastName",
|
||||
"CustomerBranch.lastNameEN as customerBranchLastNameEN",
|
||||
"Customer.customerType",
|
||||
"Quotation.id as quotationId",
|
||||
"Quotation.code as quotationCode",
|
||||
"Quotation.finalPrice as quotationValue",
|
||||
.select((eb) => [
|
||||
jsonObjectFrom(
|
||||
eb
|
||||
.selectFrom("CustomerBranch")
|
||||
.select((eb) => [
|
||||
jsonObjectFrom(
|
||||
eb
|
||||
.selectFrom("Customer")
|
||||
.selectAll("Customer")
|
||||
.whereRef("Customer.id", "=", "CustomerBranch.customerId"),
|
||||
).as("customer"),
|
||||
])
|
||||
.selectAll("CustomerBranch")
|
||||
.whereRef("CustomerBranch.id", "=", "Quotation.customerBranchId"),
|
||||
).as("customerBranch"),
|
||||
])
|
||||
.select(["Payment.paymentStatus"])
|
||||
.selectAll(["Invoice"])
|
||||
|
|
@ -674,54 +677,31 @@ export class StatsController extends Controller {
|
|||
}
|
||||
|
||||
const ret = await query.execute();
|
||||
const arr = ret.map((item) => {
|
||||
const data: Record<string, any> = {};
|
||||
for (const [key, val] of Object.entries(item)) {
|
||||
if (key.startsWith("customerBranch")) {
|
||||
if (!data["customerBranch"]) data["customerBranch"] = {};
|
||||
data["customerBranch"][key.slice(14).slice(0, 1).toLowerCase() + key.slice(14).slice(1)] =
|
||||
val;
|
||||
} else if (key.startsWith("customerType")) {
|
||||
data["customerBranch"]["customer"] = { customerType: val };
|
||||
} else {
|
||||
data[key as keyof typeof data] = val;
|
||||
}
|
||||
}
|
||||
return data as Invoice & {
|
||||
quotationId: string;
|
||||
quotationCode: string;
|
||||
quotationValue: number;
|
||||
paymentStatus: PaymentStatus;
|
||||
customerBranch: CustomerBranch & { customer: { customerType: CustomerType } };
|
||||
};
|
||||
});
|
||||
|
||||
return arr
|
||||
return ret
|
||||
.reduce<
|
||||
{
|
||||
paid: number;
|
||||
unpaid: number;
|
||||
customerBranch: CustomerBranch & { customer: { customerType: CustomerType } };
|
||||
customerBranch: CustomerBranch & { customer: Customer };
|
||||
}[]
|
||||
>((acc, item) => {
|
||||
const exists = acc.find((v) => v.customerBranch.id === item.customerBranch.id);
|
||||
const exists = acc.find((v) => v.customerBranch.id === item.customerBranch!.id);
|
||||
|
||||
if (!item.amount) return acc;
|
||||
|
||||
if (!exists) {
|
||||
return acc.concat({
|
||||
customerBranch: item.customerBranch,
|
||||
customerBranch: item.customerBranch as CustomerBranch & { customer: Customer },
|
||||
paid: item.paymentStatus === "PaymentSuccess" ? item.amount : 0,
|
||||
unpaid: item.paymentStatus !== "PaymentSuccess" ? item.amount : 0,
|
||||
});
|
||||
} else {
|
||||
exists[item.paymentStatus === "PaymentSuccess" ? "paid" : "unpaid"] += item.amount;
|
||||
}
|
||||
|
||||
exists[item.paymentStatus === "PaymentSuccess" ? "paid" : "unpaid"] += item.amount;
|
||||
|
||||
return acc;
|
||||
}, [])
|
||||
.map((v) => {
|
||||
return { ...v, _quotation: undefined };
|
||||
});
|
||||
.map((v) => ({ ...v, _quotation: undefined }));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue