2026-06-01 15:35:40 -03:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { DatabaseService } from '../../../infra/database/database.service';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class WhatsappTemplateRepository {
|
|
|
|
|
constructor(private readonly database: DatabaseService) {}
|
|
|
|
|
|
|
|
|
|
listTemplates() {
|
|
|
|
|
return this.database.query(`
|
|
|
|
|
SELECT
|
|
|
|
|
wt.*,
|
|
|
|
|
a.nome AS area_nome
|
|
|
|
|
FROM whatsapp_templates wt
|
|
|
|
|
LEFT JOIN areas a ON a.id = wt.area_id
|
|
|
|
|
ORDER BY wt.id ASC
|
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTemplateById(id: number) {
|
|
|
|
|
return this.database.query('SELECT * FROM whatsapp_templates WHERE id = $1 LIMIT 1', [id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveTemplate(input: {
|
|
|
|
|
name: string;
|
|
|
|
|
content: string;
|
|
|
|
|
category: string;
|
|
|
|
|
areaId?: number | null;
|
2026-06-23 09:56:56 -03:00
|
|
|
language: string;
|
|
|
|
|
metaTemplateName: string;
|
|
|
|
|
headerType?: string | null;
|
|
|
|
|
headerText?: string | null;
|
|
|
|
|
footerText?: string | null;
|
|
|
|
|
buttonsJson: string | null;
|
|
|
|
|
bodyVariablesJson: string | null;
|
2026-06-01 15:35:40 -03:00
|
|
|
}) {
|
|
|
|
|
return this.database.query(
|
|
|
|
|
`
|
|
|
|
|
INSERT INTO whatsapp_templates (
|
2026-06-23 09:56:56 -03:00
|
|
|
name, content, category, area_id, language,
|
|
|
|
|
meta_template_name, header_type, header_text, footer_text,
|
|
|
|
|
buttons, body_variables, status, updated_at
|
2026-06-01 15:35:40 -03:00
|
|
|
)
|
2026-06-23 09:56:56 -03:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::jsonb, $11::jsonb, 'draft', CURRENT_TIMESTAMP)
|
2026-06-01 15:35:40 -03:00
|
|
|
ON CONFLICT (name) DO UPDATE SET
|
2026-06-23 09:56:56 -03:00
|
|
|
content = EXCLUDED.content,
|
|
|
|
|
category = EXCLUDED.category,
|
|
|
|
|
area_id = EXCLUDED.area_id,
|
|
|
|
|
language = EXCLUDED.language,
|
|
|
|
|
meta_template_name = EXCLUDED.meta_template_name,
|
|
|
|
|
header_type = EXCLUDED.header_type,
|
|
|
|
|
header_text = EXCLUDED.header_text,
|
|
|
|
|
footer_text = EXCLUDED.footer_text,
|
|
|
|
|
buttons = EXCLUDED.buttons,
|
|
|
|
|
body_variables = EXCLUDED.body_variables,
|
|
|
|
|
status = 'draft',
|
|
|
|
|
meta_status = NULL,
|
|
|
|
|
meta_template_id = NULL,
|
|
|
|
|
meta_submitted_at = NULL,
|
|
|
|
|
meta_approved_at = NULL,
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP
|
2026-06-01 15:35:40 -03:00
|
|
|
RETURNING *
|
|
|
|
|
`,
|
2026-06-23 09:56:56 -03:00
|
|
|
[
|
|
|
|
|
input.name,
|
|
|
|
|
input.content,
|
|
|
|
|
input.category,
|
|
|
|
|
input.areaId || null,
|
|
|
|
|
input.language,
|
|
|
|
|
input.metaTemplateName,
|
|
|
|
|
input.headerType || null,
|
|
|
|
|
input.headerText || null,
|
|
|
|
|
input.footerText || null,
|
|
|
|
|
input.buttonsJson,
|
|
|
|
|
input.bodyVariablesJson,
|
|
|
|
|
],
|
2026-06-01 15:35:40 -03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 09:56:56 -03:00
|
|
|
updateTemplate(
|
|
|
|
|
id: number,
|
|
|
|
|
input: {
|
|
|
|
|
name: string;
|
|
|
|
|
content: string;
|
|
|
|
|
category: string;
|
|
|
|
|
areaId?: number | null;
|
|
|
|
|
language: string;
|
|
|
|
|
metaTemplateName: string;
|
|
|
|
|
headerType?: string | null;
|
|
|
|
|
headerText?: string | null;
|
|
|
|
|
footerText?: string | null;
|
|
|
|
|
buttonsJson: string | null;
|
|
|
|
|
bodyVariablesJson: string | null;
|
|
|
|
|
},
|
|
|
|
|
) {
|
2026-06-01 15:35:40 -03:00
|
|
|
return this.database.query(
|
|
|
|
|
`
|
2026-06-23 09:56:56 -03:00
|
|
|
UPDATE whatsapp_templates SET
|
|
|
|
|
name = $1,
|
|
|
|
|
content = $2,
|
|
|
|
|
category = $3,
|
|
|
|
|
area_id = $4,
|
|
|
|
|
language = $5,
|
|
|
|
|
meta_template_name = $6,
|
|
|
|
|
header_type = $7,
|
|
|
|
|
header_text = $8,
|
|
|
|
|
footer_text = $9,
|
|
|
|
|
buttons = $10::jsonb,
|
|
|
|
|
body_variables = $11::jsonb,
|
|
|
|
|
status = 'draft',
|
|
|
|
|
meta_status = NULL,
|
|
|
|
|
meta_template_id = NULL,
|
|
|
|
|
meta_submitted_at = NULL,
|
|
|
|
|
meta_approved_at = NULL,
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP
|
|
|
|
|
WHERE id = $12
|
2026-06-01 15:35:40 -03:00
|
|
|
RETURNING *
|
|
|
|
|
`,
|
2026-06-23 09:56:56 -03:00
|
|
|
[
|
|
|
|
|
input.name,
|
|
|
|
|
input.content,
|
|
|
|
|
input.category,
|
|
|
|
|
input.areaId || null,
|
|
|
|
|
input.language,
|
|
|
|
|
input.metaTemplateName,
|
|
|
|
|
input.headerType || null,
|
|
|
|
|
input.headerText || null,
|
|
|
|
|
input.footerText || null,
|
|
|
|
|
input.buttonsJson,
|
|
|
|
|
input.bodyVariablesJson,
|
|
|
|
|
id,
|
|
|
|
|
],
|
2026-06-01 15:35:40 -03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 09:56:56 -03:00
|
|
|
updateMetaInfo(
|
|
|
|
|
id: number,
|
|
|
|
|
info: { metaTemplateId?: string; metaTemplateName?: string; metaStatus: string },
|
|
|
|
|
) {
|
2026-06-01 15:35:40 -03:00
|
|
|
return this.database.query(
|
2026-06-23 09:56:56 -03:00
|
|
|
`UPDATE whatsapp_templates SET
|
|
|
|
|
meta_template_id = COALESCE($2, meta_template_id),
|
|
|
|
|
meta_template_name = COALESCE($3, meta_template_name),
|
|
|
|
|
meta_status = $4,
|
|
|
|
|
meta_submitted_at = CURRENT_TIMESTAMP,
|
|
|
|
|
status = 'pending',
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP
|
|
|
|
|
WHERE id = $1
|
|
|
|
|
RETURNING *`,
|
|
|
|
|
[id, info.metaTemplateId || null, info.metaTemplateName || null, info.metaStatus],
|
2026-06-01 15:35:40 -03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 09:56:56 -03:00
|
|
|
async updateMetaStatusByName(metaName: string, metaStatus: string, metaId: string): Promise<number> {
|
|
|
|
|
const result = await this.database.query(
|
|
|
|
|
`UPDATE whatsapp_templates SET
|
|
|
|
|
meta_status = $2,
|
|
|
|
|
meta_template_id = COALESCE($3, meta_template_id),
|
|
|
|
|
meta_approved_at = CASE WHEN $2 = 'APPROVED' THEN CURRENT_TIMESTAMP ELSE meta_approved_at END,
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP
|
|
|
|
|
WHERE meta_template_name = $1`,
|
|
|
|
|
[metaName, metaStatus, metaId || null],
|
2026-06-01 15:35:40 -03:00
|
|
|
);
|
2026-06-23 09:56:56 -03:00
|
|
|
return result.rowCount || 0;
|
2026-06-01 15:35:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteTemplate(id: number) {
|
2026-06-23 09:56:56 -03:00
|
|
|
return this.database.query('DELETE FROM whatsapp_templates WHERE id = $1 RETURNING meta_template_name', [id]);
|
2026-06-01 15:35:40 -03:00
|
|
|
}
|
|
|
|
|
}
|