195 lines
7.4 KiB
JavaScript
195 lines
7.4 KiB
JavaScript
// Damage Reports & Maintenance Routes
|
|
import express from 'express';
|
|
import pool, { generateId } from '../db.js';
|
|
|
|
const router = express.Router();
|
|
|
|
// ==========================================
|
|
// DAMAGE REPORTS
|
|
// ==========================================
|
|
|
|
// POST /api/saveDamageReport
|
|
router.post('/saveDamageReport', async (req, res) => {
|
|
try {
|
|
const data = req.body;
|
|
const id = data.id || generateId();
|
|
|
|
if (data.id) {
|
|
// Update existing
|
|
await pool.query(
|
|
`UPDATE damage_reports SET
|
|
report_date = ?, reporter_name = ?, unit = ?, location = ?, item_name = ?,
|
|
inventory_code = ?, quantity = ?, description = ?, priority = ?,
|
|
has_photo = ?, photo_base64 = ?, photo_mime_type = ?, status = ?
|
|
WHERE id = ?`,
|
|
[
|
|
data.reportDate, data.reporterName, data.unit, data.location, data.itemName,
|
|
data.inventoryCode || null, data.quantity || 1, data.description, data.priority,
|
|
data.hasPhoto ? 1 : 0, data.photoBase64 || null, data.photoMimeType || null, data.status,
|
|
data.id
|
|
]
|
|
);
|
|
res.json({ status: 'success', message: 'Laporan berhasil diperbarui', id: data.id });
|
|
} else {
|
|
// Create new
|
|
await pool.query(
|
|
`INSERT INTO damage_reports
|
|
(id, report_date, reporter_name, unit, location, item_name, inventory_code,
|
|
quantity, description, priority, has_photo, photo_base64, photo_mime_type, status)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
id, data.reportDate, data.reporterName, data.unit, data.location, data.itemName,
|
|
data.inventoryCode || null, data.quantity || 1, data.description, data.priority,
|
|
data.hasPhoto ? 1 : 0, data.photoBase64 || null, data.photoMimeType || null,
|
|
data.status || 'Menunggu Verifikasi'
|
|
]
|
|
);
|
|
res.json({ status: 'success', message: 'Laporan berhasil disimpan', id });
|
|
}
|
|
} catch (error) {
|
|
console.error('Save damage report error:', error);
|
|
res.json({ status: 'error', message: error.message });
|
|
}
|
|
});
|
|
|
|
// POST /api/updateReportStatus
|
|
router.post('/updateReportStatus', async (req, res) => {
|
|
try {
|
|
const { id, status } = req.body;
|
|
await pool.query('UPDATE damage_reports SET status = ? WHERE id = ?', [status, id]);
|
|
res.json({ status: 'success', message: 'Status berhasil diperbarui' });
|
|
} catch (error) {
|
|
console.error('Update report status error:', error);
|
|
res.json({ status: 'error', message: error.message });
|
|
}
|
|
});
|
|
|
|
// ==========================================
|
|
// MAINTENANCE RECORDS
|
|
// ==========================================
|
|
|
|
// POST /api/saveMaintenanceRecord
|
|
router.post('/saveMaintenanceRecord', async (req, res) => {
|
|
try {
|
|
const data = req.body;
|
|
|
|
await pool.query(
|
|
`INSERT INTO maintenance_records
|
|
(date, location, item_name, specification, work_type, materials_used, cost, executor, notes)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
data.date, data.location, data.itemName, data.specification || '',
|
|
data.workType, data.materialsUsed || '', data.cost || 0,
|
|
data.executor || '', data.notes || ''
|
|
]
|
|
);
|
|
|
|
res.json({ status: 'success', message: 'Catatan pemeliharaan berhasil disimpan' });
|
|
} catch (error) {
|
|
console.error('Save maintenance record error:', error);
|
|
res.json({ status: 'error', message: error.message });
|
|
}
|
|
});
|
|
|
|
// ==========================================
|
|
// GET REPORTS (Unified)
|
|
// ==========================================
|
|
|
|
// POST /api/getReports
|
|
router.post('/getReports', async (req, res) => {
|
|
try {
|
|
const { type, startDate, endDate } = req.body;
|
|
let data = [];
|
|
|
|
if (type === 'damage') {
|
|
let query = 'SELECT * FROM damage_reports';
|
|
const params = [];
|
|
|
|
if (startDate && endDate) {
|
|
query += ' WHERE report_date BETWEEN ? AND ?';
|
|
params.push(startDate, endDate);
|
|
}
|
|
query += ' ORDER BY report_date DESC';
|
|
|
|
const [rows] = await pool.query(query, params);
|
|
data = rows.map(row => ({
|
|
id: row.id,
|
|
reportDate: row.report_date,
|
|
reporterName: row.reporter_name,
|
|
unit: row.unit,
|
|
location: row.location,
|
|
itemName: row.item_name,
|
|
inventoryCode: row.inventory_code,
|
|
quantity: row.quantity,
|
|
description: row.description,
|
|
priority: row.priority,
|
|
hasPhoto: row.has_photo === 1,
|
|
photoBase64: row.photo_base64,
|
|
photoMimeType: row.photo_mime_type,
|
|
status: row.status
|
|
}));
|
|
} else if (type === 'maintenance') {
|
|
let query = 'SELECT * FROM maintenance_records';
|
|
const params = [];
|
|
|
|
if (startDate && endDate) {
|
|
query += ' WHERE date BETWEEN ? AND ?';
|
|
params.push(startDate, endDate);
|
|
}
|
|
query += ' ORDER BY date DESC';
|
|
|
|
const [rows] = await pool.query(query, params);
|
|
data = rows.map(row => ({
|
|
date: row.date,
|
|
location: row.location,
|
|
itemName: row.item_name,
|
|
specification: row.specification,
|
|
workType: row.work_type,
|
|
materialsUsed: row.materials_used,
|
|
cost: parseFloat(row.cost),
|
|
executor: row.executor,
|
|
notes: row.notes
|
|
}));
|
|
} else if (type === 'inspection') {
|
|
let query = `
|
|
SELECT i.*, GROUP_CONCAT(
|
|
CONCAT(ii.item_name, ':', ii.condition, ':', IFNULL(ii.notes, ''))
|
|
SEPARATOR '||'
|
|
) as items
|
|
FROM inspections i
|
|
LEFT JOIN inspection_items ii ON i.id = ii.inspection_id
|
|
`;
|
|
const params = [];
|
|
|
|
if (startDate && endDate) {
|
|
query += ' WHERE i.inspection_date BETWEEN ? AND ?';
|
|
params.push(startDate, endDate);
|
|
}
|
|
query += ' GROUP BY i.id ORDER BY i.inspection_date DESC';
|
|
|
|
const [rows] = await pool.query(query, params);
|
|
data = rows.map(row => ({
|
|
id: row.id,
|
|
areaId: row.area_id,
|
|
areaName: row.area_name,
|
|
locationName: row.location_name,
|
|
inspectorName: row.inspector_name,
|
|
inspectionDate: row.inspection_date,
|
|
summary: row.summary,
|
|
status: row.status,
|
|
checks: row.items ? row.items.split('||').map(item => {
|
|
const [name, condition, notes] = item.split(':');
|
|
return { name, condition, notes };
|
|
}) : []
|
|
}));
|
|
}
|
|
|
|
res.json({ status: 'success', data });
|
|
} catch (error) {
|
|
console.error('Get reports error:', error);
|
|
res.json({ status: 'error', message: error.message });
|
|
}
|
|
});
|
|
|
|
export default router;
|