mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-07-26 11:38:13 +00:00
Enhance DNS record management by adding partial updates and handling proxied fields. This includes a new PATCH endpoint and improved data handling in the database.
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/e10d65b2-ab2b-425c-be36-4014ae3f21f8.jpg
This commit is contained in:
@@ -607,9 +607,13 @@ export class DatabaseStorage implements IStorage {
|
||||
// Use direct PostgreSQL for consistency with other methods
|
||||
const { pool } = await import('./db');
|
||||
|
||||
// Filter out fields that don't exist in the database
|
||||
// Handle special fields like proxied, isAutoIP, etc.
|
||||
const { proxied, isAutoIP, providerId, ...validFields } = recordData as any;
|
||||
|
||||
// Store proxied value to return to the client later
|
||||
const proxiedValue = proxied !== undefined ? proxied : null;
|
||||
console.log(`[DEBUG] Proxied value: ${proxiedValue}`);
|
||||
|
||||
// Make all fields database-safe before proceeding
|
||||
const dbSafeFields: Record<string, any> = {};
|
||||
Object.entries(validFields).forEach(([key, value]) => {
|
||||
@@ -620,14 +624,24 @@ export class DatabaseStorage implements IStorage {
|
||||
|
||||
// Process isActive separately since it's a common toggle
|
||||
if (recordData.isActive !== undefined) {
|
||||
console.log(`[DEBUG] Setting is_active to ${recordData.isActive}`);
|
||||
dbSafeFields['is_active'] = recordData.isActive;
|
||||
}
|
||||
|
||||
// Add isAutoIP if present (maps to auto_update in database)
|
||||
if (isAutoIP !== undefined) {
|
||||
console.log(`[DEBUG] Setting auto_update to ${isAutoIP}`);
|
||||
dbSafeFields['auto_update'] = isAutoIP;
|
||||
}
|
||||
|
||||
// Store proxied value in a metadata field if it's present
|
||||
// Note: We don't have a dedicated proxied column, but we'll return it in the response
|
||||
if (proxied !== undefined) {
|
||||
console.log(`[DEBUG] Setting metadata field for proxied to ${proxied}`);
|
||||
// You could store this in a 'metadata' JSON field if you have one
|
||||
// For now, we'll just remember to add it back to the response
|
||||
}
|
||||
|
||||
// Always update last_updated timestamp
|
||||
dbSafeFields['last_updated'] = new Date().toISOString();
|
||||
|
||||
@@ -667,7 +681,7 @@ export class DatabaseStorage implements IStorage {
|
||||
notes: simpleRecord.notes,
|
||||
lastUpdated: simpleRecord.last_updated,
|
||||
createdAt: simpleRecord.created_at,
|
||||
proxied: null // Maintained for frontend compatibility
|
||||
proxied: proxiedValue // Use the proxied value from the input data or null
|
||||
};
|
||||
}
|
||||
|
||||
@@ -723,7 +737,7 @@ export class DatabaseStorage implements IStorage {
|
||||
notes: updatedRecord.notes,
|
||||
lastUpdated: updatedRecord.last_updated,
|
||||
createdAt: updatedRecord.created_at,
|
||||
proxied: null // Maintained for frontend compatibility
|
||||
proxied: proxiedValue // Use the proxied value from the input data
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error in updateDnsRecord:", error);
|
||||
|
||||
@@ -279,6 +279,74 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
}
|
||||
});
|
||||
|
||||
// PATCH endpoint for simple updates (like toggling active status)
|
||||
app.patch("/api/dns-records/:id", requireRole(["admin", "manager"]), async (req, res) => {
|
||||
try {
|
||||
console.log("Processing DNS record PATCH for id:", req.params.id);
|
||||
console.log("PATCH payload:", JSON.stringify(req.body));
|
||||
|
||||
const recordId = req.params.id;
|
||||
const validatedData = insertDnsRecordSchema.partial().parse(req.body);
|
||||
|
||||
// Get current record for history
|
||||
const currentRecord = await storage.getDnsRecord(recordId);
|
||||
if (!currentRecord) {
|
||||
console.log(`DNS record not found with ID: ${recordId}`);
|
||||
return res.status(404).json({ message: "DNS record not found" });
|
||||
}
|
||||
|
||||
console.log(`Found existing record:`, JSON.stringify(currentRecord));
|
||||
|
||||
// For PATCH operations, we only update the specific fields provided
|
||||
console.log(`Applying PATCH with data:`, JSON.stringify(validatedData));
|
||||
|
||||
// Update the record
|
||||
const record = await storage.updateDnsRecord(recordId, validatedData);
|
||||
|
||||
if (!record) {
|
||||
console.log(`PATCH failed - record not found or update failed`);
|
||||
return res.status(404).json({ message: "DNS record not found or update failed" });
|
||||
}
|
||||
|
||||
console.log(`Record patched successfully:`, JSON.stringify(record));
|
||||
|
||||
// Track history
|
||||
await storage.addDnsHistory(
|
||||
recordId,
|
||||
"patch",
|
||||
JSON.stringify(currentRecord),
|
||||
JSON.stringify(record),
|
||||
req.user?.id
|
||||
);
|
||||
|
||||
// Sync with provider if applicable, but only if we're changing isActive status
|
||||
if (validatedData.isActive !== undefined) {
|
||||
try {
|
||||
const providerRecordId = await syncDnsRecordWithProvider(
|
||||
'update',
|
||||
record.domainId,
|
||||
record,
|
||||
record.providerRecordId || undefined
|
||||
);
|
||||
|
||||
if (providerRecordId && providerRecordId !== record.providerRecordId) {
|
||||
console.log(`Record synced with provider, updating provider record ID: ${providerRecordId}`);
|
||||
// Update the record with the provider record ID
|
||||
await storage.updateDnsRecord(record.id, { providerRecordId });
|
||||
}
|
||||
} catch (syncError) {
|
||||
console.error("Failed to sync with provider:", syncError);
|
||||
// Don't fail the request if sync fails, just log the error
|
||||
}
|
||||
}
|
||||
|
||||
return res.json(record);
|
||||
} catch (error) {
|
||||
console.error("Error in PATCH DNS record:", error);
|
||||
return res.status(500).json({ message: "Failed to update DNS record", error: String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.put("/api/dns-records/:id", requireRole(["admin", "manager"]), async (req, res) => {
|
||||
try {
|
||||
console.log("Processing DNS record update for id:", req.params.id);
|
||||
@@ -310,6 +378,11 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the proxied field separately for Cloudflare records
|
||||
if (validatedData.proxied !== undefined) {
|
||||
console.log(`Handling proxied field with value:`, validatedData.proxied);
|
||||
}
|
||||
|
||||
// Log what we're about to update
|
||||
console.log(`Updating record with data:`, JSON.stringify(validatedData));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user