Spaces:
Runtime error
Runtime error
abhishek-akbari01 commited on
Commit ·
b63b5c4
1
Parent(s): 28d50d6
show invoice based on user role
Browse files
src/controllers/invoice/invoice.controller.ts
CHANGED
|
@@ -12,7 +12,7 @@ import PwBuilding from "../../models/pwBuildings";
|
|
| 12 |
import PwUnit from "../../models/pwUnits";
|
| 13 |
import { logger } from '../../utils/logger';
|
| 14 |
import ErrorLog from "../../models/errorLog";
|
| 15 |
-
import { fetchWorkorderById } from "../../shared/services/propertyware.service";
|
| 16 |
import { logInvoiceAction } from "../invoiceActivityLogs.controller";
|
| 17 |
import InvoiceApproval from "../../models/invoiceApproval";
|
| 18 |
import Role from "../../models/roles";
|
|
@@ -273,11 +273,13 @@ const buildInvoiceWhereClause = (filter: Record<string, any>): any => {
|
|
| 273 |
};
|
| 274 |
|
| 275 |
// Get All Invoices
|
| 276 |
-
export const getAllInvoices = async (req:
|
| 277 |
try {
|
| 278 |
const { sort_by, sort_order, page, limit } = req.query;
|
| 279 |
const filter = req.query.filter as Record<string, any>;
|
| 280 |
|
|
|
|
|
|
|
| 281 |
const allowedSortColumns = [
|
| 282 |
'id',
|
| 283 |
'invoice_date',
|
|
@@ -311,29 +313,60 @@ export const getAllInvoices = async (req: Request, res: Response): Promise<Respo
|
|
| 311 |
options.order = [['id', 'ASC']];
|
| 312 |
}
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
const responseData = {
|
| 329 |
page: currentPage,
|
| 330 |
limit: pageSize,
|
| 331 |
total: totalInvoices,
|
| 332 |
-
data:
|
| 333 |
};
|
| 334 |
|
| 335 |
return res.status(200).json(responseData);
|
| 336 |
} catch (error) {
|
|
|
|
| 337 |
logger.error("Error fetching invoices:", error);
|
| 338 |
return res.status(500).json({ error: "Error fetching invoices" });
|
| 339 |
}
|
|
|
|
| 12 |
import PwUnit from "../../models/pwUnits";
|
| 13 |
import { logger } from '../../utils/logger';
|
| 14 |
import ErrorLog from "../../models/errorLog";
|
| 15 |
+
import { fetchBuildingPropertyManager, fetchWorkorderById } from "../../shared/services/propertyware.service";
|
| 16 |
import { logInvoiceAction } from "../invoiceActivityLogs.controller";
|
| 17 |
import InvoiceApproval from "../../models/invoiceApproval";
|
| 18 |
import Role from "../../models/roles";
|
|
|
|
| 273 |
};
|
| 274 |
|
| 275 |
// Get All Invoices
|
| 276 |
+
export const getAllInvoices = async (req: AuthenticatedRequest, res: Response): Promise<Response> => {
|
| 277 |
try {
|
| 278 |
const { sort_by, sort_order, page, limit } = req.query;
|
| 279 |
const filter = req.query.filter as Record<string, any>;
|
| 280 |
|
| 281 |
+
const roleData = await Role.findByPk(req.user?.role_id);
|
| 282 |
+
|
| 283 |
const allowedSortColumns = [
|
| 284 |
'id',
|
| 285 |
'invoice_date',
|
|
|
|
| 313 |
options.order = [['id', 'ASC']];
|
| 314 |
}
|
| 315 |
|
| 316 |
+
let invoices:any = await Invoice.findAll({
|
| 317 |
+
...options,
|
| 318 |
+
include: [
|
| 319 |
+
{
|
| 320 |
+
model: User,
|
| 321 |
+
as: 'uploadedBy',
|
| 322 |
+
attributes: { exclude: ['password'] }
|
| 323 |
+
},
|
| 324 |
+
{
|
| 325 |
+
model: InvoiceDetail,
|
| 326 |
+
as: 'InvoiceDetails',
|
| 327 |
+
}
|
| 328 |
+
]
|
| 329 |
+
});
|
| 330 |
+
|
| 331 |
+
if (roleData?.name === "Property Manager") {
|
| 332 |
+
const propertyManagerEmail = req?.user?.email;
|
| 333 |
+
invoices = await Promise.all(
|
| 334 |
+
invoices.map(async (invoice: any) => {
|
| 335 |
+
const invoiceDetails = invoice.InvoiceDetails || [];
|
| 336 |
+
for (const detail of invoiceDetails) {
|
| 337 |
+
if (detail.pw_building_id) {
|
| 338 |
+
const buildingManagers = await fetchBuildingPropertyManager(detail.pw_building_id);
|
| 339 |
+
if (buildingManagers.some((manager: any) => manager.email === propertyManagerEmail)) {
|
| 340 |
+
return invoice;
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
return null;
|
| 345 |
+
})
|
| 346 |
+
);
|
| 347 |
+
invoices = invoices.filter((invoice: any) => invoice !== null);
|
| 348 |
+
} else if (roleData?.name === "Accountant") {
|
| 349 |
+
invoices = invoices.filter((invoice: any) => {
|
| 350 |
+
const hasInvoiceDetails = invoice.InvoiceDetails && invoice.InvoiceDetails.length > 0;
|
| 351 |
+
return !hasInvoiceDetails;
|
| 352 |
+
});
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
const totalInvoices = invoices.length;
|
| 357 |
+
|
| 358 |
+
const paginatedInvoices = invoices.slice((currentPage - 1) * pageSize, currentPage * pageSize);
|
| 359 |
|
| 360 |
const responseData = {
|
| 361 |
page: currentPage,
|
| 362 |
limit: pageSize,
|
| 363 |
total: totalInvoices,
|
| 364 |
+
data: paginatedInvoices
|
| 365 |
};
|
| 366 |
|
| 367 |
return res.status(200).json(responseData);
|
| 368 |
} catch (error) {
|
| 369 |
+
console.log("error-", error)
|
| 370 |
logger.error("Error fetching invoices:", error);
|
| 371 |
return res.status(500).json({ error: "Error fetching invoices" });
|
| 372 |
}
|