Spaces:
Runtime error
Runtime error
| import { fetchUnits, fetchUnitsJson, getIndexByLabel } from './propertyware.service'; | |
| import PwUnits from '../../models/pwUnits'; | |
| import { logger } from '../../utils/logger'; | |
| import PwPortfolio from '../../models/pwPortfolio'; | |
| import PwBuilding from '../../models/pwBuildings'; | |
| export const syncUnitsDataService = async (): Promise<string> => { | |
| try { | |
| const unitsData = await fetchUnits(); | |
| const unitsResponseData = unitsData.map((unit: Record<string, string>) => ({ | |
| pw_id: unit.id, | |
| name: unit.name, | |
| pw_portfolio_id: unit.portfolioID, | |
| pw_building_id: unit.buildingID | |
| })); | |
| await PwUnits.bulkCreate(unitsResponseData, { | |
| updateOnDuplicate: ['name', 'pw_portfolio_id', 'pw_building_id'], | |
| }); | |
| logger.info("Units data synced successfully"); | |
| return "Units data synced successfully"; | |
| } catch (error) { | |
| logger.error("Error syncing Units"); | |
| logger.error(error); | |
| throw new Error("Error syncing Units"); | |
| } | |
| }; | |
| export const syncUnitsDataFromUrlService = async (): Promise<string> => { | |
| try { | |
| const units = await fetchUnitsJson(); | |
| const portfolioBulkData: any[] = []; | |
| const buildingBulkData: any[] = []; | |
| const unitsBulkData: any[] = []; | |
| const indexes = { | |
| portfolio_id: getIndexByLabel(units.columns, "Portfolio Entity ID"), | |
| portfolio_name:getIndexByLabel(units.columns, "Portfolio Name"), | |
| building_id: getIndexByLabel(units.columns, "Building Entity ID"), | |
| building_name: getIndexByLabel(units.columns, "Building Address"), | |
| unit_id: getIndexByLabel(units.columns, "Unit Entity ID"), | |
| unit_name: getIndexByLabel(units.columns, "Unit Name"), | |
| }; | |
| for (const record of units.records) { | |
| portfolioBulkData.push({ | |
| pw_id: record[indexes.portfolio_id as number], | |
| name: record[indexes.portfolio_name as number], | |
| }); | |
| buildingBulkData.push({ | |
| pw_id: record[indexes.building_id as number], | |
| name: record[indexes.building_name as number], | |
| pw_portfolio_id: record[indexes.portfolio_id as number], | |
| }); | |
| unitsBulkData.push({ | |
| pw_id: record[indexes.unit_id as number], | |
| name: record[indexes.unit_name as number], | |
| pw_portfolio_id: record[indexes.portfolio_id as number], | |
| pw_building_id: record[indexes.building_id as number], | |
| }); | |
| } | |
| // Perform bulk insertions | |
| await PwPortfolio.bulkCreate(portfolioBulkData, { ignoreDuplicates: true }); | |
| await PwBuilding.bulkCreate(buildingBulkData, { ignoreDuplicates: true }); | |
| await PwUnits.bulkCreate(unitsBulkData, { ignoreDuplicates: true }); | |
| logger.info(`Synced ${units.records.length} units successfully`); | |
| return "Units data synced successfully"; | |
| } catch (error) { | |
| logger.error("Error syncing Units"); | |
| logger.error(error); | |
| throw new Error("Error syncing Units"); | |
| } | |
| }; | |