FIX: importFromMeta resolve conflito de nome com templates locais existentes
ON CONFLICT (name) DO UPDATE atualiza meta info em templates locais com mesmo nome mas sem meta_template_name. Remove catch silencioso e loga falhas de importacao.
This commit is contained in:
parent
85d89ae934
commit
7617480242
@ -188,7 +188,15 @@ export class WhatsappTemplateRepository {
|
|||||||
CASE WHEN $7 = 'APPROVED' THEN CURRENT_TIMESTAMP ELSE NULL END,
|
CASE WHEN $7 = 'APPROVED' THEN CURRENT_TIMESTAMP ELSE NULL END,
|
||||||
CURRENT_TIMESTAMP
|
CURRENT_TIMESTAMP
|
||||||
)
|
)
|
||||||
ON CONFLICT (meta_template_name) WHERE meta_template_name IS NOT NULL DO NOTHING`,
|
ON CONFLICT (name) DO UPDATE SET
|
||||||
|
meta_template_name = EXCLUDED.meta_template_name,
|
||||||
|
meta_template_id = EXCLUDED.meta_template_id,
|
||||||
|
meta_status = EXCLUDED.meta_status,
|
||||||
|
content = CASE WHEN whatsapp_templates.meta_template_name IS NULL THEN EXCLUDED.content ELSE whatsapp_templates.content END,
|
||||||
|
status = CASE WHEN EXCLUDED.meta_status = 'APPROVED' THEN 'approved' ELSE whatsapp_templates.status END,
|
||||||
|
meta_approved_at = CASE WHEN EXCLUDED.meta_status = 'APPROVED' THEN CURRENT_TIMESTAMP ELSE whatsapp_templates.meta_approved_at END,
|
||||||
|
updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE whatsapp_templates.meta_template_name IS NULL`,
|
||||||
[
|
[
|
||||||
input.metaTemplateName,
|
input.metaTemplateName,
|
||||||
input.content,
|
input.content,
|
||||||
@ -204,8 +212,8 @@ export class WhatsappTemplateRepository {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
return (result.rowCount || 0) > 0;
|
return (result.rowCount || 0) > 0;
|
||||||
} catch {
|
} catch (err: any) {
|
||||||
return false;
|
throw new Error(`importFromMeta "${input.metaTemplateName}": ${err.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -142,22 +142,26 @@ export class WhatsappTemplateService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parsed = this.parseComponents(t.components || []);
|
const parsed = this.parseComponents(t.components || []);
|
||||||
const wasInserted = await this.repo.importFromMeta({
|
try {
|
||||||
metaTemplateId: metaId,
|
const wasInserted = await this.repo.importFromMeta({
|
||||||
metaTemplateName: t.name,
|
metaTemplateId: metaId,
|
||||||
metaStatus: t.status,
|
metaTemplateName: t.name,
|
||||||
language: t.language || 'pt_BR',
|
metaStatus: t.status,
|
||||||
category: this.normalizeCategory(t.category),
|
language: t.language || 'pt_BR',
|
||||||
content: parsed.content,
|
category: this.normalizeCategory(t.category),
|
||||||
headerType: parsed.headerType,
|
content: parsed.content,
|
||||||
headerText: parsed.headerText,
|
headerType: parsed.headerType,
|
||||||
footerText: parsed.footerText,
|
headerText: parsed.headerText,
|
||||||
buttonsJson: parsed.buttons?.length ? JSON.stringify(parsed.buttons) : null,
|
footerText: parsed.footerText,
|
||||||
});
|
buttonsJson: parsed.buttons?.length ? JSON.stringify(parsed.buttons) : null,
|
||||||
|
});
|
||||||
|
|
||||||
if (wasInserted) {
|
if (wasInserted) {
|
||||||
imported++;
|
imported++;
|
||||||
this.logger.log(`[Meta Templates] Importado: "${t.name}" (${t.status})`);
|
this.logger.log(`[Meta Templates] Importado: "${t.name}" (${t.status})`);
|
||||||
|
}
|
||||||
|
} catch (importErr: any) {
|
||||||
|
this.logger.warn(`[Meta Templates] Falha ao importar "${t.name}": ${importErr.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user