67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { WhatsappTemplateRepository } from './repositories/whatsapp-template.repository';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class WhatsappTemplateService {
|
||
|
|
constructor(private readonly whatsappTemplateRepository: WhatsappTemplateRepository) {}
|
||
|
|
|
||
|
|
async getTemplates() {
|
||
|
|
await this.whatsappTemplateRepository.refreshMetaApprovals();
|
||
|
|
const result = await this.whatsappTemplateRepository.listTemplates();
|
||
|
|
return result.rows;
|
||
|
|
}
|
||
|
|
|
||
|
|
async getTemplateById(id: number) {
|
||
|
|
await this.whatsappTemplateRepository.refreshMetaApprovals();
|
||
|
|
const result = await this.whatsappTemplateRepository.getTemplateById(id);
|
||
|
|
return result.rows[0] || null;
|
||
|
|
}
|
||
|
|
|
||
|
|
async saveTemplate(name: string, content: string, areaId?: number | null, requestedByRole = 'admin', category = 'UTILITY') {
|
||
|
|
const isSupervisor = requestedByRole === 'supervisor';
|
||
|
|
const result = await this.whatsappTemplateRepository.saveTemplate({
|
||
|
|
name,
|
||
|
|
content,
|
||
|
|
category: this.normalizeTemplateCategory(category),
|
||
|
|
areaId,
|
||
|
|
status: isSupervisor ? 'admin_review' : 'meta_review',
|
||
|
|
requestedByRole,
|
||
|
|
adminApprovedAtSql: isSupervisor ? 'NULL' : 'CURRENT_TIMESTAMP',
|
||
|
|
metaSubmittedAtSql: isSupervisor ? 'NULL' : 'CURRENT_TIMESTAMP',
|
||
|
|
});
|
||
|
|
|
||
|
|
return result.rows[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
async updateTemplate(id: number, name: string, content: string, areaId?: number | null, category = 'UTILITY') {
|
||
|
|
const result = await this.whatsappTemplateRepository.updateTemplate(id, {
|
||
|
|
name,
|
||
|
|
content,
|
||
|
|
areaId,
|
||
|
|
category: this.normalizeTemplateCategory(category),
|
||
|
|
});
|
||
|
|
|
||
|
|
return result.rows[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
async approveTemplateByAdmin(id: number) {
|
||
|
|
const result = await this.whatsappTemplateRepository.approveTemplateByAdmin(id);
|
||
|
|
return result.rows[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
async rejectTemplateByAdmin(id: number) {
|
||
|
|
const result = await this.whatsappTemplateRepository.rejectTemplateByAdmin(id);
|
||
|
|
return result.rows[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
async deleteTemplate(id: number) {
|
||
|
|
await this.whatsappTemplateRepository.deleteTemplate(id);
|
||
|
|
return { success: true };
|
||
|
|
}
|
||
|
|
|
||
|
|
private normalizeTemplateCategory(category?: string) {
|
||
|
|
const normalized = String(category || 'UTILITY').trim().toUpperCase();
|
||
|
|
return ['UTILITY', 'MARKETING', 'AUTHENTICATION'].includes(normalized) ? normalized : 'UTILITY';
|
||
|
|
}
|
||
|
|
}
|