Spaces:
Runtime error
Runtime error
Bansari Akhani commited on
Commit ·
d4bef81
1
Parent(s): 4c6cb1e
optimize location lookup api
Browse files- src/controllers/propertyware/units.controller.ts +44 -45
- src/models/index.ts +3 -0
- src/models/pwUnits.ts +2 -2
src/controllers/propertyware/units.controller.ts
CHANGED
|
@@ -27,53 +27,52 @@ export const syncUnitsDataFromUrl = async (req: Request, res: Response) => {
|
|
| 27 |
|
| 28 |
export const LocationLookup = async (req: Request, res: Response) => {
|
| 29 |
try {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
const
|
| 36 |
-
|
| 37 |
-
name: portfolio.getDataValue('name'),
|
| 38 |
-
}));
|
| 39 |
-
|
| 40 |
-
// Fetch buildings for each portfolio
|
| 41 |
-
const portfoliosWithBuildings = await Promise.all(extractedPortfolio.map(async (portfolio: { id: any; }) => {
|
| 42 |
-
|
| 43 |
-
const buildings = await PwBuilding.findAll({
|
| 44 |
-
attributes: ['pw_id', 'name'],
|
| 45 |
-
where: { pw_portfolio_id: portfolio.id }
|
| 46 |
-
});
|
| 47 |
-
|
| 48 |
-
const extractedBuildings = buildings.map(building => ({
|
| 49 |
-
id: building.getDataValue('pw_id'),
|
| 50 |
-
name: building.getDataValue('name'),
|
| 51 |
-
}));
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
// Fetch units for each building
|
| 55 |
-
const buildingsWithUnits = await Promise.all(extractedBuildings.map(async (building: { id: any; }) => {
|
| 56 |
-
|
| 57 |
-
const units = await PwUnit.findAll({
|
| 58 |
-
attributes: ['pw_id', 'name'],
|
| 59 |
-
where: { pw_building_id: building.id }
|
| 60 |
-
});
|
| 61 |
-
|
| 62 |
-
const extractedUnits = units.map(unit => ({
|
| 63 |
-
id: unit.getDataValue('pw_id'),
|
| 64 |
-
name: unit.getDataValue('name'),
|
| 65 |
-
}));
|
| 66 |
-
return { ...building, units: extractedUnits };
|
| 67 |
-
}));
|
| 68 |
-
|
| 69 |
-
return { ...portfolio, buildings: buildingsWithUnits };
|
| 70 |
-
}));
|
| 71 |
-
|
| 72 |
-
res.json(portfoliosWithBuildings);
|
| 73 |
} catch (error) {
|
| 74 |
-
console.
|
| 75 |
-
logger.error('Error fetching nested data:', error);
|
| 76 |
res.status(500).json({ error: 'An error occurred while fetching data' });
|
| 77 |
}
|
| 78 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
| 27 |
|
| 28 |
export const LocationLookup = async (req: Request, res: Response) => {
|
| 29 |
try {
|
| 30 |
+
const unitsWithBuildingPortfolio = await PwUnit.findAll({
|
| 31 |
+
attributes: [['pw_id', 'id'], 'name'],
|
| 32 |
+
include: [
|
| 33 |
+
{
|
| 34 |
+
model: PwPortfolio,
|
| 35 |
+
as: 'portfolio',
|
| 36 |
+
attributes: [['pw_id', 'id'], 'name'],
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
model: PwBuilding,
|
| 40 |
+
as: 'building',
|
| 41 |
+
attributes: [['pw_id', 'id'], 'name'],
|
| 42 |
+
},
|
| 43 |
+
],
|
| 44 |
+
});
|
| 45 |
|
| 46 |
+
const portfolios = transformUnitsToPortfolios(unitsWithBuildingPortfolio)
|
| 47 |
+
res.json(portfolios);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
} catch (error) {
|
| 49 |
+
console.error('Error fetching location data:', error);
|
|
|
|
| 50 |
res.status(500).json({ error: 'An error occurred while fetching data' });
|
| 51 |
}
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
function transformUnitsToPortfolios(units: any[]) {
|
| 55 |
+
const portfolios: any[] = [];
|
| 56 |
+
|
| 57 |
+
units.forEach(unit => {
|
| 58 |
+
const { portfolio, building, id, name } = unit;
|
| 59 |
+
|
| 60 |
+
let portfolioEntry = portfolios.find(p => p.id === portfolio.id);
|
| 61 |
+
if (!portfolioEntry) {
|
| 62 |
+
portfolioEntry = { id: portfolio.id, name: portfolio.name, buildings: [] };
|
| 63 |
+
portfolios.push(portfolioEntry);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
let buildingEntry = portfolioEntry.buildings.find((b: { id: any; }) => b.id === building.id);
|
| 67 |
+
if (!buildingEntry) {
|
| 68 |
+
buildingEntry = { id: building.id, name: building.name, units: [] };
|
| 69 |
+
portfolioEntry.buildings.push(buildingEntry);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
buildingEntry.units.push({ id, name });
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
return portfolios;
|
| 76 |
+
}
|
| 77 |
|
| 78 |
+
|
src/models/index.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import { Options, Sequelize } from "sequelize";
|
| 2 |
|
| 3 |
import { config } from "../db/config/db.config";
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
export const sequelize = new Sequelize(
|
| 6 |
config.database as string,
|
|
|
|
| 1 |
import { Options, Sequelize } from "sequelize";
|
| 2 |
|
| 3 |
import { config } from "../db/config/db.config";
|
| 4 |
+
import PwPortfolio from "./pwPortfolio";
|
| 5 |
+
import PwBuilding from "./pwBuildings";
|
| 6 |
+
import PwUnit from "./pwUnits";
|
| 7 |
|
| 8 |
export const sequelize = new Sequelize(
|
| 9 |
config.database as string,
|
src/models/pwUnits.ts
CHANGED
|
@@ -70,7 +70,7 @@ PwUnit.init(
|
|
| 70 |
updatedAt: 'updated_at',
|
| 71 |
}
|
| 72 |
);
|
| 73 |
-
PwUnit.belongsTo(PwPortfolio, { foreignKey: 'pw_portfolio_id', targetKey: 'pw_id' });
|
| 74 |
-
PwUnit.belongsTo(PwBuilding, { foreignKey: 'pw_building_id', targetKey: 'pw_id' });
|
| 75 |
|
| 76 |
export default PwUnit;
|
|
|
|
| 70 |
updatedAt: 'updated_at',
|
| 71 |
}
|
| 72 |
);
|
| 73 |
+
PwUnit.belongsTo(PwPortfolio, { foreignKey: 'pw_portfolio_id', targetKey: 'pw_id', as: 'portfolio' });
|
| 74 |
+
PwUnit.belongsTo(PwBuilding, { foreignKey: 'pw_building_id', targetKey: 'pw_id', as: 'building' });
|
| 75 |
|
| 76 |
export default PwUnit;
|