Spaces:
Runtime error
Runtime error
Bansari Akhani
commited on
Commit
·
c2bc2d7
1
Parent(s):
d012606
location api for AI service - #8264, fix #8263, remove debug console
Browse files- src/controllers/invoice/invoice.controller.ts +56 -51
- src/controllers/propertyware/bills.controller.ts +0 -2
- src/controllers/propertyware/vendors.controller.ts +8 -5
- src/controllers/propertyware/workorders.controller.ts +0 -1
- src/middlewares/authMiddleware.ts +0 -1
- src/routes/propertyware.routes.ts +2 -2
src/controllers/invoice/invoice.controller.ts
CHANGED
|
@@ -44,17 +44,17 @@ export const createInvoice = async (req: AuthenticatedRequest, res: Response) =>
|
|
| 44 |
|
| 45 |
// validate if parsed workorderID exists in propertyware workorders
|
| 46 |
let portfolioId: number | null = null;
|
| 47 |
-
let buildingId: number | null
|
| 48 |
-
let unitId: number | null
|
| 49 |
-
|
| 50 |
if (aiServiceData.workOrderID) {
|
| 51 |
try {
|
| 52 |
const PWWorkorderDetails = await fetchWorkorderById(aiServiceData.workOrderID);
|
| 53 |
-
portfolioId= PWWorkorderDetails.portfolioID;
|
| 54 |
-
buildingId= PWWorkorderDetails.buildingID;
|
| 55 |
-
unitId= PWWorkorderDetails.unitID;
|
| 56 |
} catch (error) {
|
| 57 |
-
console.log("workorder errors = ",error);
|
| 58 |
aiServiceData.workOrderID = null;
|
| 59 |
}
|
| 60 |
}
|
|
@@ -67,7 +67,14 @@ export const createInvoice = async (req: AuthenticatedRequest, res: Response) =>
|
|
| 67 |
const dueDate = new Date(aiServiceData.dueDate);
|
| 68 |
|
| 69 |
// check if vendor has default bill split ID defined in Propertyware
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
let invoiceRecord = {
|
| 73 |
reference_number: aiServiceData.refNo,
|
|
@@ -102,7 +109,7 @@ export const createInvoice = async (req: AuthenticatedRequest, res: Response) =>
|
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
-
if (details.building && !buildingId
|
| 106 |
const buildingData = await PwBuilding.findOne({
|
| 107 |
where: {
|
| 108 |
name: details.building,
|
|
@@ -129,7 +136,7 @@ export const createInvoice = async (req: AuthenticatedRequest, res: Response) =>
|
|
| 129 |
pw_portfolio_id: portfolioId,
|
| 130 |
pw_building_id: buildingId,
|
| 131 |
pw_unit_id: unitId,
|
| 132 |
-
pw_gl_account_id: isDefaultBillsplitAccountSet? isDefaultBillsplitAccountSet : (details.glAccount ? details.glAccount : null),
|
| 133 |
amount: details.amount,
|
| 134 |
description: details.description,
|
| 135 |
};
|
|
@@ -193,32 +200,32 @@ const buildInvoiceWhereClause = (filter: Record<string, any>): any => {
|
|
| 193 |
if (filter) {
|
| 194 |
|
| 195 |
if (filter.date) {
|
| 196 |
-
const date = new Date(filter.date);
|
| 197 |
if (!isNaN(date.getTime())) {
|
| 198 |
-
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
}
|
| 209 |
-
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
|
| 223 |
if (filter.vendor_name) {
|
| 224 |
whereClause.vendor_name = { [Op.like]: `%${filter.vendor_name}%` };
|
|
@@ -272,7 +279,7 @@ const buildInvoiceWhereClause = (filter: Record<string, any>): any => {
|
|
| 272 |
}
|
| 273 |
}
|
| 274 |
|
| 275 |
-
if (filter.total){
|
| 276 |
whereClause.total = { [Op.eq]: filter.total };
|
| 277 |
}
|
| 278 |
}
|
|
@@ -319,7 +326,7 @@ export const getAllInvoices = async (req: AuthenticatedRequest, res: Response):
|
|
| 319 |
options.order = [['id', 'DESC']];
|
| 320 |
}
|
| 321 |
|
| 322 |
-
let invoices:any = await Invoice.findAll({
|
| 323 |
...options,
|
| 324 |
include: [
|
| 325 |
{
|
|
@@ -419,19 +426,19 @@ export const getInvoiceById = async (req: AuthenticatedRequest, res: Response):
|
|
| 419 |
const user = req.user;
|
| 420 |
const roleData = await Role.findByPk(user?.role_id);
|
| 421 |
if (roleData) {
|
| 422 |
-
if (roleData.name === 'Property Manager') {
|
| 423 |
if (invoice.status === 'PM Approved') {
|
| 424 |
showApprove = false;
|
| 425 |
-
}
|
| 426 |
}
|
| 427 |
-
if (roleData.name === "Accountant"){
|
| 428 |
showApprove = false;
|
| 429 |
}
|
| 430 |
}
|
| 431 |
}
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
return res.status(200).json({ invoice: invoice.dataValues, billSplit: billSplit, showApprove: showApprove});
|
| 435 |
} catch (error) {
|
| 436 |
logger.error("Error fetching invoice:", error);
|
| 437 |
return res.status(500).json({ error: "Error fetching invoice details" });
|
|
@@ -462,15 +469,15 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
|
|
| 462 |
|
| 463 |
if (!existingDetail.id) {
|
| 464 |
const newPortfolioName = newDetail.pw_portfolio_id
|
| 465 |
-
|
| 466 |
: 'null';
|
| 467 |
const newBuildingName = newDetail.pw_building_id
|
| 468 |
-
|
| 469 |
: 'null';
|
| 470 |
const newUnitName = newDetail.pw_unit_id
|
| 471 |
-
|
| 472 |
: 'null';
|
| 473 |
-
|
| 474 |
const newValue = `${newPortfolioName}, ${newBuildingName}, ${newUnitName}`;
|
| 475 |
await logInvoiceAction({
|
| 476 |
invoice_id: invoiceId,
|
|
@@ -481,8 +488,7 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
|
|
| 481 |
new_value: newValue,
|
| 482 |
});
|
| 483 |
} else {
|
| 484 |
-
if (existingDetail.pw_portfolio_id !== newDetail.pw_portfolio_id)
|
| 485 |
-
{
|
| 486 |
const oldPortfolioName = existingDetail.pw_portfolio_id
|
| 487 |
? (await fetchPortfolioById(existingDetail.pw_portfolio_id))?.name || 'Unknown Portfolio'
|
| 488 |
: 'null';
|
|
@@ -518,7 +524,7 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
|
|
| 518 |
});
|
| 519 |
}
|
| 520 |
}
|
| 521 |
-
if (existingDetail.pw_unit_id !== newDetail.pw_unit_id) {
|
| 522 |
|
| 523 |
const oldUnitName = existingDetail.pw_unit_id
|
| 524 |
? (await fetchUnitsById(existingDetail.pw_unit_id))?.name || 'Unknown Unit'
|
|
@@ -526,7 +532,7 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
|
|
| 526 |
const newUnitName = newDetail.pw_unit_id
|
| 527 |
? (await fetchUnitsById(newDetail.pw_unit_id))?.name || ''
|
| 528 |
: 'null';
|
| 529 |
-
|
| 530 |
if (oldUnitName !== newUnitName) {
|
| 531 |
await logInvoiceAction({
|
| 532 |
invoice_id: invoiceId,
|
|
@@ -538,8 +544,7 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
|
|
| 538 |
});
|
| 539 |
}
|
| 540 |
}
|
| 541 |
-
if (existingDetail.pw_gl_account_id !== newDetail.pw_gl_account_id)
|
| 542 |
-
{
|
| 543 |
const oldGhlAccountName = existingDetail.pw_gl_account_id
|
| 544 |
? (await fetchGLAccountById(existingDetail.pw_gl_account_id))?.name || 'Unknown GHL Account'
|
| 545 |
: 'null';
|
|
@@ -712,13 +717,13 @@ export const approveInvoice = async (req: AuthenticatedRequest, res: Response):
|
|
| 712 |
return;
|
| 713 |
}
|
| 714 |
if (roleData.name === "Admin") {
|
| 715 |
-
|
| 716 |
|
| 717 |
-
|
| 718 |
-
|
| 719 |
|
| 720 |
res.status(200).json({ message: 'Invoice approved' });
|
| 721 |
-
|
| 722 |
} else if (invoice.total < 1500) {
|
| 723 |
if (roleData.name === 'Property Manager' || roleData.name === 'Accounting Supervisor') {
|
| 724 |
await approveAndCreateRecord(invoice.id as number, userData.id, roleData.id, comment);
|
|
|
|
| 44 |
|
| 45 |
// validate if parsed workorderID exists in propertyware workorders
|
| 46 |
let portfolioId: number | null = null;
|
| 47 |
+
let buildingId: number | null = null;
|
| 48 |
+
let unitId: number | null = null;
|
| 49 |
+
|
| 50 |
if (aiServiceData.workOrderID) {
|
| 51 |
try {
|
| 52 |
const PWWorkorderDetails = await fetchWorkorderById(aiServiceData.workOrderID);
|
| 53 |
+
portfolioId = PWWorkorderDetails.portfolioID;
|
| 54 |
+
buildingId = PWWorkorderDetails.buildingID;
|
| 55 |
+
unitId = PWWorkorderDetails.unitID;
|
| 56 |
} catch (error) {
|
| 57 |
+
console.log("workorder errors = ", error);
|
| 58 |
aiServiceData.workOrderID = null;
|
| 59 |
}
|
| 60 |
}
|
|
|
|
| 67 |
const dueDate = new Date(aiServiceData.dueDate);
|
| 68 |
|
| 69 |
// check if vendor has default bill split ID defined in Propertyware
|
| 70 |
+
let isDefaultBillsplitAccountSet = false;
|
| 71 |
+
if (aiServiceData.vendor_id) {
|
| 72 |
+
try {
|
| 73 |
+
isDefaultBillsplitAccountSet = await isVendorHasDefaultBillsplitAccount(aiServiceData.vendor_id);
|
| 74 |
+
} catch (error) {
|
| 75 |
+
console.log(error);
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
|
| 79 |
let invoiceRecord = {
|
| 80 |
reference_number: aiServiceData.refNo,
|
|
|
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
+
if (details.building && !buildingId) {
|
| 113 |
const buildingData = await PwBuilding.findOne({
|
| 114 |
where: {
|
| 115 |
name: details.building,
|
|
|
|
| 136 |
pw_portfolio_id: portfolioId,
|
| 137 |
pw_building_id: buildingId,
|
| 138 |
pw_unit_id: unitId,
|
| 139 |
+
pw_gl_account_id: isDefaultBillsplitAccountSet ? isDefaultBillsplitAccountSet : (details.glAccount ? details.glAccount : null),
|
| 140 |
amount: details.amount,
|
| 141 |
description: details.description,
|
| 142 |
};
|
|
|
|
| 200 |
if (filter) {
|
| 201 |
|
| 202 |
if (filter.date) {
|
| 203 |
+
const date = new Date(filter.date);
|
| 204 |
if (!isNaN(date.getTime())) {
|
| 205 |
+
const startOfDay = new Date(date);
|
| 206 |
+
startOfDay.setHours(0, 0, 0, 0);
|
| 207 |
|
| 208 |
+
const endOfDay = new Date(date);
|
| 209 |
+
endOfDay.setHours(23, 59, 59, 999);
|
| 210 |
|
| 211 |
+
whereClause.created_at = {
|
| 212 |
+
[Op.gte]: startOfDay,
|
| 213 |
+
[Op.lte]: endOfDay
|
| 214 |
+
};
|
| 215 |
}
|
| 216 |
+
}
|
| 217 |
|
| 218 |
+
if (filter.id) {
|
| 219 |
+
whereClause.id = { [Op.eq]: filter.id };
|
| 220 |
+
}
|
| 221 |
|
| 222 |
+
if (filter.filename) {
|
| 223 |
+
whereClause.filename = { [Op.like]: `%${filter.filename}%` };
|
| 224 |
+
}
|
| 225 |
|
| 226 |
+
if (filter.id) {
|
| 227 |
+
whereClause.id = { [Op.eq]: filter.id };
|
| 228 |
+
}
|
| 229 |
|
| 230 |
if (filter.vendor_name) {
|
| 231 |
whereClause.vendor_name = { [Op.like]: `%${filter.vendor_name}%` };
|
|
|
|
| 279 |
}
|
| 280 |
}
|
| 281 |
|
| 282 |
+
if (filter.total) {
|
| 283 |
whereClause.total = { [Op.eq]: filter.total };
|
| 284 |
}
|
| 285 |
}
|
|
|
|
| 326 |
options.order = [['id', 'DESC']];
|
| 327 |
}
|
| 328 |
|
| 329 |
+
let invoices: any = await Invoice.findAll({
|
| 330 |
...options,
|
| 331 |
include: [
|
| 332 |
{
|
|
|
|
| 426 |
const user = req.user;
|
| 427 |
const roleData = await Role.findByPk(user?.role_id);
|
| 428 |
if (roleData) {
|
| 429 |
+
if (roleData.name === 'Property Manager') {
|
| 430 |
if (invoice.status === 'PM Approved') {
|
| 431 |
showApprove = false;
|
| 432 |
+
}
|
| 433 |
}
|
| 434 |
+
if (roleData.name === "Accountant") {
|
| 435 |
showApprove = false;
|
| 436 |
}
|
| 437 |
}
|
| 438 |
}
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
return res.status(200).json({ invoice: invoice.dataValues, billSplit: billSplit, showApprove: showApprove });
|
| 442 |
} catch (error) {
|
| 443 |
logger.error("Error fetching invoice:", error);
|
| 444 |
return res.status(500).json({ error: "Error fetching invoice details" });
|
|
|
|
| 469 |
|
| 470 |
if (!existingDetail.id) {
|
| 471 |
const newPortfolioName = newDetail.pw_portfolio_id
|
| 472 |
+
? (await fetchPortfolioById(newDetail.pw_portfolio_id))?.name || ''
|
| 473 |
: 'null';
|
| 474 |
const newBuildingName = newDetail.pw_building_id
|
| 475 |
+
? (await fetchBuildingsById(newDetail.pw_building_id))?.name || ''
|
| 476 |
: 'null';
|
| 477 |
const newUnitName = newDetail.pw_unit_id
|
| 478 |
+
? (await fetchUnitsById(newDetail.pw_unit_id))?.name || ''
|
| 479 |
: 'null';
|
| 480 |
+
|
| 481 |
const newValue = `${newPortfolioName}, ${newBuildingName}, ${newUnitName}`;
|
| 482 |
await logInvoiceAction({
|
| 483 |
invoice_id: invoiceId,
|
|
|
|
| 488 |
new_value: newValue,
|
| 489 |
});
|
| 490 |
} else {
|
| 491 |
+
if (existingDetail.pw_portfolio_id !== newDetail.pw_portfolio_id) {
|
|
|
|
| 492 |
const oldPortfolioName = existingDetail.pw_portfolio_id
|
| 493 |
? (await fetchPortfolioById(existingDetail.pw_portfolio_id))?.name || 'Unknown Portfolio'
|
| 494 |
: 'null';
|
|
|
|
| 524 |
});
|
| 525 |
}
|
| 526 |
}
|
| 527 |
+
if (existingDetail.pw_unit_id !== newDetail.pw_unit_id) {
|
| 528 |
|
| 529 |
const oldUnitName = existingDetail.pw_unit_id
|
| 530 |
? (await fetchUnitsById(existingDetail.pw_unit_id))?.name || 'Unknown Unit'
|
|
|
|
| 532 |
const newUnitName = newDetail.pw_unit_id
|
| 533 |
? (await fetchUnitsById(newDetail.pw_unit_id))?.name || ''
|
| 534 |
: 'null';
|
| 535 |
+
|
| 536 |
if (oldUnitName !== newUnitName) {
|
| 537 |
await logInvoiceAction({
|
| 538 |
invoice_id: invoiceId,
|
|
|
|
| 544 |
});
|
| 545 |
}
|
| 546 |
}
|
| 547 |
+
if (existingDetail.pw_gl_account_id !== newDetail.pw_gl_account_id) {
|
|
|
|
| 548 |
const oldGhlAccountName = existingDetail.pw_gl_account_id
|
| 549 |
? (await fetchGLAccountById(existingDetail.pw_gl_account_id))?.name || 'Unknown GHL Account'
|
| 550 |
: 'null';
|
|
|
|
| 717 |
return;
|
| 718 |
}
|
| 719 |
if (roleData.name === "Admin") {
|
| 720 |
+
await approveAndCreateRecord(invoice.id as number, userData.id, roleData.id, comment);
|
| 721 |
|
| 722 |
+
invoice.status = 'Approved';
|
| 723 |
+
await invoice.save();
|
| 724 |
|
| 725 |
res.status(200).json({ message: 'Invoice approved' });
|
| 726 |
+
|
| 727 |
} else if (invoice.total < 1500) {
|
| 728 |
if (roleData.name === 'Property Manager' || roleData.name === 'Accounting Supervisor') {
|
| 729 |
await approveAndCreateRecord(invoice.id as number, userData.id, roleData.id, comment);
|
src/controllers/propertyware/bills.controller.ts
CHANGED
|
@@ -70,7 +70,6 @@ export const syncInvoices = async (req: Request, res: Response): Promise<void> =
|
|
| 70 |
const formData = new FormData();
|
| 71 |
formData.append('file', invoice.pdf_url as string);
|
| 72 |
const PWBillUploadResult = await uploadBill(response.id, formData);
|
| 73 |
-
console.log(PWBillUploadResult);
|
| 74 |
}
|
| 75 |
|
| 76 |
} catch (error) {
|
|
@@ -82,7 +81,6 @@ export const syncInvoices = async (req: Request, res: Response): Promise<void> =
|
|
| 82 |
logger.info(`Bill for invoice ${bill.refNo} synced successfully`);
|
| 83 |
} catch (error) {
|
| 84 |
let errorMessage = `Error syncing bill for invoice ${bill.refNo}.`;
|
| 85 |
-
console.log(error);
|
| 86 |
const errorobj = (error as ErrorResponse);
|
| 87 |
|
| 88 |
const errordata = errorobj.response.data;
|
|
|
|
| 70 |
const formData = new FormData();
|
| 71 |
formData.append('file', invoice.pdf_url as string);
|
| 72 |
const PWBillUploadResult = await uploadBill(response.id, formData);
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
} catch (error) {
|
|
|
|
| 81 |
logger.info(`Bill for invoice ${bill.refNo} synced successfully`);
|
| 82 |
} catch (error) {
|
| 83 |
let errorMessage = `Error syncing bill for invoice ${bill.refNo}.`;
|
|
|
|
| 84 |
const errorobj = (error as ErrorResponse);
|
| 85 |
|
| 86 |
const errordata = errorobj.response.data;
|
src/controllers/propertyware/vendors.controller.ts
CHANGED
|
@@ -21,11 +21,14 @@ export const fetchVendorsData = async (req: Request, res: Response) => {
|
|
| 21 |
};
|
| 22 |
|
| 23 |
export const isVendorHasDefaultBillsplitAccount = async (vendorId: number) => {
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
-
return false;
|
| 31 |
};
|
|
|
|
| 21 |
};
|
| 22 |
|
| 23 |
export const isVendorHasDefaultBillsplitAccount = async (vendorId: number) => {
|
| 24 |
+
try {
|
| 25 |
+
const vendor = await fetchVendorById(vendorId);
|
| 26 |
+
if (vendor && vendor.defaultBillSplitAccountId){
|
| 27 |
+
return vendor.defaultBillSplitAccountId;
|
| 28 |
+
}
|
| 29 |
+
} catch (error){
|
| 30 |
+
logger.error(error);
|
| 31 |
+
return false;
|
| 32 |
}
|
| 33 |
|
|
|
|
| 34 |
};
|
src/controllers/propertyware/workorders.controller.ts
CHANGED
|
@@ -7,7 +7,6 @@ export const fetchWorkorders = async (req: Request, res: Response) => {
|
|
| 7 |
try {
|
| 8 |
|
| 9 |
const workorders = await fetchAllWorkorders();
|
| 10 |
-
console.log(workorders);
|
| 11 |
|
| 12 |
const workordersResponseData = workorders.map((workorder: {
|
| 13 |
unitID: any;
|
|
|
|
| 7 |
try {
|
| 8 |
|
| 9 |
const workorders = await fetchAllWorkorders();
|
|
|
|
| 10 |
|
| 11 |
const workordersResponseData = workorders.map((workorder: {
|
| 12 |
unitID: any;
|
src/middlewares/authMiddleware.ts
CHANGED
|
@@ -10,7 +10,6 @@ const jwtMiddleware = (req: AuthenticatedRequest, res: Response, next: NextFunct
|
|
| 10 |
|
| 11 |
try {
|
| 12 |
const user = verifyToken(token);
|
| 13 |
-
console.log("Loagged in User ", user);
|
| 14 |
req.user = user as UserInterface;
|
| 15 |
next();
|
| 16 |
} catch (error) {
|
|
|
|
| 10 |
|
| 11 |
try {
|
| 12 |
const user = verifyToken(token);
|
|
|
|
| 13 |
req.user = user as UserInterface;
|
| 14 |
next();
|
| 15 |
} catch (error) {
|
src/routes/propertyware.routes.ts
CHANGED
|
@@ -192,8 +192,6 @@ pwRouter.get("/glAccountsDetails", fetchGLAccountsWithDetails);
|
|
| 192 |
*/
|
| 193 |
pwRouter.get("/vendors", fetchVendorsData);
|
| 194 |
|
| 195 |
-
pwRouter.use(jwtMiddleware);
|
| 196 |
-
|
| 197 |
/**
|
| 198 |
* @swagger
|
| 199 |
* /api/pw/locationLookup:
|
|
@@ -208,6 +206,8 @@ pwRouter.use(jwtMiddleware);
|
|
| 208 |
*/
|
| 209 |
pwRouter.get('/locationLookup', LocationLookup);
|
| 210 |
|
|
|
|
|
|
|
| 211 |
/**
|
| 212 |
* @swagger
|
| 213 |
* /api/pw/workorders:
|
|
|
|
| 192 |
*/
|
| 193 |
pwRouter.get("/vendors", fetchVendorsData);
|
| 194 |
|
|
|
|
|
|
|
| 195 |
/**
|
| 196 |
* @swagger
|
| 197 |
* /api/pw/locationLookup:
|
|
|
|
| 206 |
*/
|
| 207 |
pwRouter.get('/locationLookup', LocationLookup);
|
| 208 |
|
| 209 |
+
pwRouter.use(jwtMiddleware);
|
| 210 |
+
|
| 211 |
/**
|
| 212 |
* @swagger
|
| 213 |
* /api/pw/workorders:
|