async findOne(id: string) {
const budget = await this.budgetEntity
.findById(id)
.populate([
{ path: ‘seller’, select: ‘name phone’ },
{ path: ‘clients’, select: ‘name’ },
{
path: ‘packages.package’,
populate: [
{
path: ‘package’,
model: ‘Packages’,
select: ‘name showInPdf description photo url’,
},
{ path: ‘clients.client’, model: ‘Clients’, select: ‘name’ },
],
},
])
.lean();
// Verificacao inicial de seguranca
if (!budget || !budget.packages) {
return null;
}
// Geração de colunas da tabela
const tableHead = checkRulesForTable(budget.packages);
const tableHeaderArray = generateTableHeader(tableHead);
let total = 0;
// Calculo dos totais por item
const packagesWithPricing = budget.packages.map((section) => {
const sectionPackages = section.package.map((subPkg) => {
const pricingObj = getTotalPricingByType(
subPkg.clients,
tableHeaderArray,
);
const totalItem = Object.values(pricingObj).reduce(
(acc: number, val: any) =>
typeof val === 'number' ? acc + val : acc,
0,
);
total += totalItem;
return {
...subPkg,
pricing: pricingObj,
};
});
return {
...section,
package: sectionPackages,
};
});
const discountRate = 0.1;
const totalCard = +total.toFixed(2);
const totalPix = +(total * (1 - discountRate)).toFixed(2);
return {
...budget,
packages: packagesWithPricing,
table_head: tableHead,
totalPricing: {
totalCard,
totalPix,
discountPercent: discountRate * 100,
},
};
}