2nd commit
This commit is contained in:
parent
229e0fee4e
commit
c88a6123e8
@ -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) => {
|
router.put('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const updateData = req.body;
|
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);
|
const template = await Template.getByIdWithFeatures(id);
|
||||||
|
console.log('🔎 Lookup result (default):', { found: !!template });
|
||||||
if (!template) {
|
if (!template) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
@ -756,7 +798,9 @@ router.put('/:id', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('📝 Updating default template...');
|
||||||
const updatedTemplate = await template.update(updateData);
|
const updatedTemplate = await template.update(updateData);
|
||||||
|
console.log('📝 Update result (default):', { updated: !!updatedTemplate });
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -764,7 +808,7 @@ router.put('/:id', async (req, res) => {
|
|||||||
message: `Template '${updatedTemplate.title}' updated successfully`
|
message: `Template '${updatedTemplate.title}' updated successfully`
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error updating template:', error.message);
|
console.error('❌ Error updating template:', { message: error.message, stack: error.stack });
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Failed to update template',
|
error: 'Failed to update template',
|
||||||
@ -773,14 +817,47 @@ 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) => {
|
router.delete('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
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);
|
const template = await Template.getByIdWithFeatures(id);
|
||||||
|
console.log('🔎 Lookup result (default):', { found: !!template });
|
||||||
if (!template) {
|
if (!template) {
|
||||||
|
console.warn('⚠️ Default template not found', { id });
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Template not found',
|
error: 'Template not found',
|
||||||
@ -788,16 +865,16 @@ router.delete('/:id', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Soft delete by updating the instance
|
console.log('🗑️ Deleting default template...');
|
||||||
// await template.update({ is_active = false });
|
|
||||||
await Template.delete(id);
|
await Template.delete(id);
|
||||||
|
console.log('🗑️ Delete done (default)');
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: `Template '${template.title}' deleted successfully`
|
message: `Template '${template.title}' deleted successfully`
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error deleting template:', error.message);
|
console.error('❌ Error deleting template:', { message: error.message, stack: error.stack });
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Failed to delete template',
|
error: 'Failed to delete template',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user