File size: 2,817 Bytes
1f6a8f9
 
2b10760
2c97468
ea09d29
1f4097b
 
 
1f6a8f9
 
 
185ec1f
 
 
d747060
 
185ec1f
 
 
1f6a8f9
ea09d29
 
 
 
 
d747060
 
ea09d29
 
 
 
1f4097b
2b10760
d4bef81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f4097b
d4bef81
 
1f4097b
d747060
 
1f4097b
 
d4bef81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b10760
d4bef81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Request, Response } from 'express';

import { fetchBuildings, fetchPortfolio, fetchUnits } from '../../shared/services/propertyware.service';
import { logger } from '../../utils/logger';
import { syncUnitsDataFromUrlService, syncUnitsDataService } from '../../shared/services/units.service';
import PwPortfolio from '../../models/pwPortfolio';
import PwBuilding from '../../models/pwBuildings';
import PwUnit from '../../models/pwUnits';

export const syncUnitsData = async (req: Request, res: Response) => {
    try {
        const result = await syncUnitsDataService();
        res.status(200).send(result);
    } catch (error) {
        logger.error('Error syncing Units');
        logger.error(error);
        res.status(500).send((error as Error).message);
    }
};

export const syncUnitsDataFromUrl = async (req: Request, res: Response) => {
    try {
        const result = await syncUnitsDataFromUrlService();
        res.status(200).send(result);
    } catch (error) {
        logger.error('Error syncing Units from URL');
        logger.error(error);
        res.status(500).send((error as Error).message);
    }
};

export const LocationLookup = async (req: Request, res: Response) => {
    try {
        const unitsWithBuildingPortfolio = await PwUnit.findAll({
            attributes: [['pw_id', 'id'], 'name'],
            include: [
                {
                    model: PwPortfolio,
                    as: 'portfolio',
                    attributes: [['pw_id', 'id'], 'name'],
                },
                {
                    model: PwBuilding,
                    as: 'building',
                    attributes: [['pw_id', 'id'], 'name'],
                },
            ],
        });

        const portfolios = transformUnitsToPortfolios(unitsWithBuildingPortfolio)
        res.json(portfolios);
    } catch (error) {
        logger.error('Error fetching location data:', error);
        logger.error(error);
        res.status(500).json({ error: 'An error occurred while fetching data' });
    }
};

function transformUnitsToPortfolios(units: any[]) {
    const portfolios: any[] = [];
  
    units.forEach(unit => {
      const { portfolio, building, id, name } = unit;
  
      let portfolioEntry = portfolios.find(p => p.id === portfolio.id);
      if (!portfolioEntry) {
        portfolioEntry = { id: portfolio.id, name: portfolio.name, buildings: [] };
        portfolios.push(portfolioEntry);
      }
  
      let buildingEntry = portfolioEntry.buildings.find((b: { id: any; }) => b.id === building.id);
      if (!buildingEntry) {
        buildingEntry = { id: building.id, name: building.name, units: [] };
        portfolioEntry.buildings.push(buildingEntry);
      }
  
      buildingEntry.units.push({ id, name });
    });
  
    return portfolios;
  }