Test notifications

This commit is contained in:
2026-02-01 23:40:32 -06:00
parent 1330421ddd
commit 203e6069c8
2 changed files with 106 additions and 10 deletions

View File

@@ -6,10 +6,25 @@ import { query } from '../config/database';
/** /**
* POST /api/test/create-negative-flow-meter * POST /api/test/create-negative-flow-meter
* Create a test meter with negative flow for testing notifications * Create a test meter with negative flow for testing notifications
* Body (optional): { flowValue: number } - Defaults to -25.75
*/ */
export async function createTestMeterWithNegativeFlow(req: AuthenticatedRequest, res: Response): Promise<void> { export async function createTestMeterWithNegativeFlow(req: AuthenticatedRequest, res: Response): Promise<void> {
try { try {
console.log('🧪 [Test] Creating test meter with negative flow...'); // Get custom flow value from request body, default to -25.75
const { flowValue } = req.body as { flowValue?: number };
const negativeFlowValue = flowValue !== undefined ? flowValue : -25.75;
// Ensure the value is negative
if (negativeFlowValue >= 0) {
res.status(400).json({
success: false,
error: 'Flow value must be negative',
message: 'Please provide a negative flow value (e.g., -25.75)',
});
return;
}
console.log(`🧪 [Test] Creating test meter with negative flow: ${negativeFlowValue}`);
// Get first active concentrator // Get first active concentrator
const concentratorResult = await query(` const concentratorResult = await query(`
@@ -46,14 +61,14 @@ export async function createTestMeterWithNegativeFlow(req: AuthenticatedRequest,
await query(` await query(`
UPDATE meters UPDATE meters
SET SET
last_reading_value = -25.75, last_reading_value = $1,
last_reading_at = CURRENT_TIMESTAMP, last_reading_at = CURRENT_TIMESTAMP,
status = 'ACTIVE', status = 'ACTIVE',
updated_at = CURRENT_TIMESTAMP updated_at = CURRENT_TIMESTAMP
WHERE id = $1 WHERE id = $2
`, [meterId]); `, [negativeFlowValue, meterId]);
console.log(`✅ [Test] Test meter updated: ${meterId}`); console.log(`✅ [Test] Test meter updated: ${meterId} with flow: ${negativeFlowValue}`);
} else { } else {
// Create new test meter // Create new test meter
action = 'created'; action = 'created';
@@ -80,16 +95,16 @@ export async function createTestMeterWithNegativeFlow(req: AuthenticatedRequest,
'Test Location - Building A', 'Test Location - Building A',
'LORA', 'LORA',
'ACTIVE', 'ACTIVE',
-25.75, $2,
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,
'2024-01-01', '2024-01-01',
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP CURRENT_TIMESTAMP
) RETURNING id ) RETURNING id
`, [concentrator.id]); `, [concentrator.id, negativeFlowValue]);
meterId = createResult.rows[0].id; meterId = createResult.rows[0].id;
console.log(`✅ [Test] Test meter created: ${meterId}`); console.log(`✅ [Test] Test meter created: ${meterId} with flow: ${negativeFlowValue}`);
} }
// Get the created/updated meter details // Get the created/updated meter details
@@ -109,7 +124,7 @@ export async function createTestMeterWithNegativeFlow(req: AuthenticatedRequest,
res.status(200).json({ res.status(200).json({
success: true, success: true,
message: `Test meter ${action} successfully`, message: `Test meter ${action} successfully with flow value: ${negativeFlowValue}`,
data: { data: {
action, action,
meter: meterDetails.rows[0], meter: meterDetails.rows[0],
@@ -129,6 +144,74 @@ export async function createTestMeterWithNegativeFlow(req: AuthenticatedRequest,
} }
} }
/**
* PUT /api/test/update-meter-flow/:meterId
* Update a meter's last_reading_value for testing
* Body: { flowValue: number }
*/
export async function updateMeterFlow(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
const { meterId } = req.params;
const { flowValue } = req.body as { flowValue: number };
if (flowValue === undefined || flowValue === null) {
res.status(400).json({
success: false,
error: 'Flow value is required',
message: 'Please provide a flowValue in the request body',
});
return;
}
console.log(`🧪 [Test] Updating meter ${meterId} with flow value: ${flowValue}`);
// Update meter flow value
const updateResult = await query(`
UPDATE meters
SET
last_reading_value = $1,
last_reading_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE id = $2
RETURNING id, serial_number, name, last_reading_value, status
`, [flowValue, meterId]);
if (updateResult.rows.length === 0) {
res.status(404).json({
success: false,
error: 'Meter not found',
message: `No meter found with ID: ${meterId}`,
});
return;
}
const meter = updateResult.rows[0];
console.log(`✅ [Test] Meter ${meterId} updated with flow: ${flowValue}`);
res.status(200).json({
success: true,
message: `Meter flow updated successfully to ${flowValue}`,
data: {
meter,
isNegative: flowValue < 0,
instructions: flowValue < 0 ? {
next_step: 'This meter now has negative flow. Trigger the notification job.',
endpoint: 'POST /api/test/trigger-negative-flow',
} : {
next_step: 'This meter has positive flow and will not trigger notifications.',
},
},
});
} catch (error) {
console.error('❌ [Test] Error updating meter flow:', error);
res.status(500).json({
success: false,
error: 'Failed to update meter flow',
message: error instanceof Error ? error.message : 'Unknown error',
});
}
}
/** /**
* POST /api/test/trigger-negative-flow * POST /api/test/trigger-negative-flow
* Manually trigger the negative flow detection job for testing * Manually trigger the negative flow detection job for testing

View File

@@ -12,6 +12,7 @@ const router = Router();
/** /**
* POST /api/test/create-negative-flow-meter * POST /api/test/create-negative-flow-meter
* Create a test meter with negative flow * Create a test meter with negative flow
* Body (optional): { flowValue: number } - Default: -25.75
* This creates or updates the test meter TEST-NEGATIVE-001 * This creates or updates the test meter TEST-NEGATIVE-001
*/ */
router.post( router.post(
@@ -21,10 +22,22 @@ router.post(
testController.createTestMeterWithNegativeFlow testController.createTestMeterWithNegativeFlow
); );
/**
* PUT /api/test/update-meter-flow/:meterId
* Update any meter's last_reading_value for testing
* Body: { flowValue: number }
*/
router.put(
'/update-meter-flow/:meterId',
authenticateToken,
requireRole('ADMIN'),
testController.updateMeterFlow
);
/** /**
* POST /api/test/trigger-negative-flow * POST /api/test/trigger-negative-flow
* Manually trigger the negative flow detection job * Manually trigger the negative flow detection job
* This simulates the cron job that runs at 1:00 AM * This simulates the cron job that runs at 1:00 AM, 1:15 AM, and 1:30 AM PST
*/ */
router.post( router.post(
'/trigger-negative-flow', '/trigger-negative-flow',