Fix: Set SSL initialize check with the current time. Closes: #100

- When adding a new SSL certificate, initialize with the current time. This ensures that the SSL check is triggered shortly after the certificate is added to the monitoring system.
This commit is contained in:
Tola Leng
2025-07-31 17:28:41 +07:00
parent 50b4d1dfb0
commit f7cf2fcc20
@@ -13,6 +13,8 @@ export const addSSLCertificate = async (
certificateData: AddSSLCertificateDto certificateData: AddSSLCertificateDto
): Promise<SSLCertificate> => { ): Promise<SSLCertificate> => {
try { try {
const currentTime = new Date().toISOString();
// Prepare the data for saving to database // Prepare the data for saving to database
// The Go service will handle the actual SSL checking // The Go service will handle the actual SSL checking
const data = { const data = {
@@ -23,14 +25,15 @@ export const addSSLCertificate = async (
cert_sans: "", cert_sans: "",
cert_alg: "", cert_alg: "",
serial_number: "", serial_number: "",
valid_from: new Date().toISOString(), // Will be updated by Go service valid_from: currentTime, // Will be updated by Go service
valid_till: new Date().toISOString(), // Will be updated by Go service valid_till: currentTime, // Will be updated by Go service
validity_days: 0, // Will be updated by Go service validity_days: 0, // Will be updated by Go service
days_left: 0, // Will be updated by Go service days_left: 0, // Will be updated by Go service
warning_threshold: Number(certificateData.warning_threshold) || 30, warning_threshold: Number(certificateData.warning_threshold) || 30,
expiry_threshold: Number(certificateData.expiry_threshold) || 7, expiry_threshold: Number(certificateData.expiry_threshold) || 7,
notification_channel: certificateData.notification_channel || "", notification_channel: certificateData.notification_channel || "",
check_interval: Number(certificateData.check_interval) || 1, // New field check_interval: Number(certificateData.check_interval) || 1, // New field
check_at: currentTime, // Set to current time to trigger immediate check
}; };
// Save to database // Save to database
@@ -38,7 +41,7 @@ export const addSSLCertificate = async (
return record as unknown as SSLCertificate; return record as unknown as SSLCertificate;
} catch (error) { } catch (error) {
// console.error("Error adding SSL certificate:", error); console.error("Error adding SSL certificate:", error);
throw error; throw error;
} }
}; };
@@ -67,7 +70,7 @@ export const checkAndUpdateCertificate = async (
// Return the current certificate data // Return the current certificate data
return typedCertificate; return typedCertificate;
} catch (error) { } catch (error) {
// console.error("Error updating SSL certificate:", error); console.error("Error updating SSL certificate:", error);
throw error; throw error;
} }
}; };
@@ -84,10 +87,10 @@ export const triggerImmediateCheck = async (certificateId: string): Promise<void
check_at: currentTime check_at: currentTime
}); });
// console.log(`Triggered immediate check for certificate ${certificateId} at ${currentTime}`); console.log(`Triggered immediate check for certificate ${certificateId} at ${currentTime}`);
toast.success("SSL check scheduled - certificate will be checked shortly"); toast.success("SSL check scheduled - certificate will be checked shortly");
} catch (error) { } catch (error) {
// console.error("Error triggering immediate SSL check:", error); console.error("Error triggering immediate SSL check:", error);
toast.error("Failed to schedule SSL check"); toast.error("Failed to schedule SSL check");
throw error; throw error;
} }
@@ -101,7 +104,7 @@ export const deleteSSLCertificate = async (id: string): Promise<boolean> => {
await pb.collection("ssl_certificates").delete(id); await pb.collection("ssl_certificates").delete(id);
return true; return true;
} catch (error) { } catch (error) {
// console.error("Error deleting SSL certificate:", error); console.error("Error deleting SSL certificate:", error);
throw error; throw error;
} }
}; };
@@ -112,10 +115,10 @@ export const deleteSSLCertificate = async (id: string): Promise<boolean> => {
*/ */
export const refreshAllCertificates = async (): Promise<{ success: number; failed: number }> => { export const refreshAllCertificates = async (): Promise<{ success: number; failed: number }> => {
try { try {
const response = await pb.collection("ssl_certificates").getList(1, 200); const response = await pb.collection("ssl_certificates").getList(1, 100);
const certificates = response.items as unknown as SSLCertificate[]; const certificates = response.items as unknown as SSLCertificate[];
// console.log(`Refreshing ${certificates.length} certificates...`); console.log(`Refreshing ${certificates.length} certificates...`);
let success = 0; let success = 0;
let failed = 0; let failed = 0;
@@ -125,14 +128,14 @@ export const refreshAllCertificates = async (): Promise<{ success: number; faile
await checkCertificateAndNotify(cert); await checkCertificateAndNotify(cert);
success++; success++;
} catch (error) { } catch (error) {
// console.error(`Failed to refresh certificate ${cert.domain}:`, error); console.error(`Failed to refresh certificate ${cert.domain}:`, error);
failed++; failed++;
} }
} }
return { success, failed }; return { success, failed };
} catch (error) { } catch (error) {
// console.error("Error refreshing certificates:", error); console.error("Error refreshing certificates:", error);
throw error; throw error;
} }
}; };