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,
|
||||
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.content,
|
||||
@ -204,8 +212,8 @@ export class WhatsappTemplateRepository {
|
||||
],
|
||||
);
|
||||
return (result.rowCount || 0) > 0;
|
||||
} catch {
|
||||
return false;
|
||||
} catch (err: any) {
|
||||
throw new Error(`importFromMeta "${input.metaTemplateName}": ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -142,22 +142,26 @@ export class WhatsappTemplateService {
|
||||
}
|
||||
|
||||
const parsed = this.parseComponents(t.components || []);
|
||||
const wasInserted = await this.repo.importFromMeta({
|
||||
metaTemplateId: metaId,
|
||||
metaTemplateName: t.name,
|
||||
metaStatus: t.status,
|
||||
language: t.language || 'pt_BR',
|
||||
category: this.normalizeCategory(t.category),
|
||||
content: parsed.content,
|
||||
headerType: parsed.headerType,
|
||||
headerText: parsed.headerText,
|
||||
footerText: parsed.footerText,
|
||||
buttonsJson: parsed.buttons?.length ? JSON.stringify(parsed.buttons) : null,
|
||||
});
|
||||
try {
|
||||
const wasInserted = await this.repo.importFromMeta({
|
||||
metaTemplateId: metaId,
|
||||
metaTemplateName: t.name,
|
||||
metaStatus: t.status,
|
||||
language: t.language || 'pt_BR',
|
||||
category: this.normalizeCategory(t.category),
|
||||
content: parsed.content,
|
||||
headerType: parsed.headerType,
|
||||
headerText: parsed.headerText,
|
||||
footerText: parsed.footerText,
|
||||
buttonsJson: parsed.buttons?.length ? JSON.stringify(parsed.buttons) : null,
|
||||
});
|
||||
|
||||
if (wasInserted) {
|
||||
imported++;
|
||||
this.logger.log(`[Meta Templates] Importado: "${t.name}" (${t.status})`);
|
||||
if (wasInserted) {
|
||||
imported++;
|
||||
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