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 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 | |
| Viewing file: Select action/file-type: import express from 'express';
import { Maintenance, Building, Floor, Room, Bed, Activity } from '../models/index.js';
import { auth, authorize } from '../middleware/auth.js';
import { maintenanceValidators } from '../middleware/validators.js';
const router = express.Router();
// ✅ Create maintenance
router.post('/', auth, authorize(['manage']), maintenanceValidators.create, async (req, res) => {
try {
const maintenance = await Maintenance.create(req.body);
const data = {
model_id: maintenance.id,
model_name: 'Maintenance',
description: `Maintenance #${maintenance.id} for ${maintenance.type} created`,
};
const activity = await Activity.create(data);
res.status(201).json(maintenance);
} catch (err) {
console.error('Create maintenance error:', err);
res.status(500).json({ error: 'Failed to create maintenance record' });
}
});
// ✅ Get all maintenance records (with optional filters)
router.get('/', auth, async (req, res) => {
try {
const where = {};
['building_id', 'floor_id', 'room_id', 'bed_id', 'status'].forEach(key => {
if (req.query[key]) where[key] = req.query[key];
});
const maintenances = await Maintenance.findAll({
where,
include: [
{
model: Building,
as: 'building',
},
{
model: Room,
as: 'room',
include: [
{
model: Floor,
as: 'floor',
},
]
},
],
order: [['created_at', 'DESC']]
});
res.json(maintenances);
} catch (err) {
console.error('Fetch maintenance records error:', err);
res.status(500).json({ error: 'Failed to fetch maintenance records' });
}
});
// ✅ Get maintenance record by ID
router.get('/:id', auth, async (req, res) => {
try {
const maintenance = await Maintenance.findByPk(req.params.id, {
include: [
{ model: Building },
{ model: Floor },
{ model: Room },
{ model: Bed }
]
});
if (!maintenance) {
return res.status(404).json({ error: 'Maintenance record not found' });
}
res.json(maintenance);
} catch (err) {
console.error('Get maintenance record error:', err);
res.status(500).json({ error: 'Failed to fetch maintenance record' });
}
});
// ✅ Update maintenance record
router.put('/:id', auth, authorize(['manage']), maintenanceValidators.update, async (req, res) => {
try {
const maintenance = await Maintenance.findByPk(req.params.id);
if (!maintenance) {
return res.status(404).json({ error: 'Maintenance record not found' });
}
await maintenance.update(req.body);
const data = {
model_id: maintenance.id,
model_name: 'Maintenance',
description: `Maintenance #${maintenance.id} for ${maintenance.type} updated`,
};
const activity = await Activity.create(data);
res.json(maintenance);
} catch (err) {
console.error('Update maintenance error:', err);
res.status(500).json({ error: 'Failed to update maintenance record' });
}
});
// ✅ Delete maintenance record
router.delete('/:id', auth, authorize(['delete']), async (req, res) => {
try {
const maintenance = await Maintenance.findByPk(req.params.id);
if (!maintenance) {
return res.status(404).json({ error: 'Maintenance record not found' });
}
const data = {
model_id: maintenance.id,
model_name: 'Maintenance',
description: `Maintenance #${maintenance.id} deleted`,
};
const activity = await Activity.create(data);
await maintenance.destroy();
res.json({ message: 'Maintenance record deleted successfully' });
} catch (err) {
console.error('Delete maintenance error:', err);
res.status(500).json({ error: 'Failed to delete maintenance record' });
}
});
export default router;
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0048 ]-- |