From c88a6123e81b3da9888a95157b375a82921d6f5b Mon Sep 17 00:00:00 2001 From: Chandini Date: Thu, 28 Aug 2025 12:23:16 +0530 Subject: [PATCH] 2nd commit --- .../template-manager/src/routes/templates.js | 103 +++++++++++++++--- 1 file changed, 90 insertions(+), 13 deletions(-) diff --git a/services/template-manager/src/routes/templates.js b/services/template-manager/src/routes/templates.js index 72d2991..33077a5 100644 --- a/services/template-manager/src/routes/templates.js +++ b/services/template-manager/src/routes/templates.js @@ -740,14 +740,56 @@ router.post('/', async (req, res) => { } }); -// PUT /api/templates/:id - Update template +// PUT /api/templates/:id - Update template or custom template based on isCustom flag router.put('/:id', async (req, res) => { try { const { id } = req.params; const updateData = req.body; - console.log(`📝 Updating template: ${id}`); - + const isCustomParam = (req.query.isCustom || req.query.is_custom || '').toString().toLowerCase(); + const isCustom = isCustomParam === 'true' || isCustomParam === '1' || isCustomParam === 'yes'; + console.log('📝 [PUT /api/templates/:id] start', { id, isCustom, bodyKeys: Object.keys(updateData || {}) }); + + if (isCustom) { + console.log('🔎 Looking up custom template by id'); + const custom = await CustomTemplate.getById(id); + console.log('🔎 Lookup result (custom):', { found: !!custom }); + if (!custom) { + return res.status(404).json({ + success: false, + error: 'Template not found', + message: `Custom template with ID ${id} does not exist` + }); + } + // Validate allowed fields for custom templates to avoid no-op updates + const allowed = [ + 'title','description','icon','category','gradient','border','text','subtext', + 'complexity','business_rules','technical_requirements','approved','usage_count', + 'status','admin_notes','admin_reviewed_at','admin_reviewed_by', + 'canonical_template_id','similarity_score','user_id' + ]; + const providedKeys = Object.keys(updateData || {}); + const updatableKeys = providedKeys.filter(k => allowed.includes(k)); + console.log('🧮 Update keys (custom):', { providedKeys, updatableKeys }); + if (updatableKeys.length === 0) { + return res.status(400).json({ + success: false, + error: 'No updatable fields', + message: 'Provide at least one updatable field' + }); + } + console.log('📝 Updating custom template...'); + const updated = await CustomTemplate.update(id, updateData); + console.log('📝 Update result (custom):', { updated: !!updated }); + return res.json({ + success: true, + data: updated, + message: `Custom template '${updated?.title || updated?.id}' updated successfully` + }); + } + + console.log('🔎 Looking up default template by id'); const template = await Template.getByIdWithFeatures(id); + console.log('🔎 Lookup result (default):', { found: !!template }); if (!template) { return res.status(404).json({ success: false, @@ -755,8 +797,10 @@ router.put('/:id', async (req, res) => { message: `Template with ID ${id} does not exist` }); } - + + console.log('📝 Updating default template...'); const updatedTemplate = await template.update(updateData); + console.log('📝 Update result (default):', { updated: !!updatedTemplate }); res.json({ success: true, @@ -764,7 +808,7 @@ router.put('/:id', async (req, res) => { message: `Template '${updatedTemplate.title}' updated successfully` }); } catch (error) { - console.error('❌ Error updating template:', error.message); + console.error('❌ Error updating template:', { message: error.message, stack: error.stack }); res.status(500).json({ success: false, error: 'Failed to update template', @@ -773,31 +817,64 @@ router.put('/:id', async (req, res) => { } }); -// DELETE /api/templates/:id - Soft delete template +// DELETE /api/templates/:id - Delete template or custom template based on isCustom flag router.delete('/:id', async (req, res) => { try { const { id } = req.params; - console.log(`🗑️ Deleting template: ${id}`); - + const isCustomParam = (req.query.isCustom || req.query.is_custom || '').toString().toLowerCase(); + const isCustom = isCustomParam === 'true' || isCustomParam === '1' || isCustomParam === 'yes'; + console.log('🗑️ [DELETE /api/templates/:id] start', { id, query: req.query, isCustomParam, isCustom }); + + if (isCustom) { + console.log('🔎 Looking up custom template by id'); + const custom = await CustomTemplate.getById(id); + console.log('🔎 Lookup result (custom):', { found: !!custom }); + if (!custom) { + console.warn('⚠️ Custom template not found', { id }); + return res.status(404).json({ + success: false, + error: 'Template not found', + message: `Custom template with ID ${id} does not exist` + }); + } + console.log('🗑️ Deleting custom template...'); + const deleted = await CustomTemplate.delete(id); + console.log('🗑️ Delete result (custom):', { deleted }); + if (!deleted) { + return res.status(500).json({ + success: false, + error: 'Failed to delete template', + message: `Failed to delete custom template with ID ${id}` + }); + } + return res.json({ + success: true, + message: `Custom template '${custom.title || custom.id}' deleted successfully` + }); + } + + console.log('🔎 Looking up default template by id'); const template = await Template.getByIdWithFeatures(id); + console.log('🔎 Lookup result (default):', { found: !!template }); if (!template) { + console.warn('⚠️ Default template not found', { id }); return res.status(404).json({ success: false, error: 'Template not found', message: `Template with ID ${id} does not exist` }); } - - // Soft delete by updating the instance - // await template.update({ is_active = false }); + + console.log('🗑️ Deleting default template...'); await Template.delete(id); - + console.log('🗑️ Delete done (default)'); + res.json({ success: true, message: `Template '${template.title}' deleted successfully` }); } catch (error) { - console.error('❌ Error deleting template:', error.message); + console.error('❌ Error deleting template:', { message: error.message, stack: error.stack }); res.status(500).json({ success: false, error: 'Failed to delete template',