diff --git a/src/modules/email/email.module.ts b/src/modules/email/email.module.ts
index cf7728e..e971ed3 100644
--- a/src/modules/email/email.module.ts
+++ b/src/modules/email/email.module.ts
@@ -10,7 +10,6 @@ import { MailboxResolverService } from "./services/mailbox-resolver.service";
import { WebSocketAuthService } from "./services/websocket-auth.service";
import { jwtConfig } from "../../configs/jwt.config";
import { MailServerModule } from "../mail-server/mail-server.module";
-import { TemplateProcessorService } from "../templates/services/template-processor.service";
import { TemplatesModule } from "../templates/templates.module";
import { User } from "../users/entities/user.entity";
import { UsersModule } from "../users/users.module";
@@ -18,7 +17,7 @@ import { UsersModule } from "../users/users.module";
@Module({
imports: [MailServerModule, UsersModule, TemplatesModule, JwtModule.registerAsync(jwtConfig()), MikroOrmModule.forFeature([User])],
controllers: [EmailController],
- providers: [EmailService, MailboxResolverService, EmailGateway, EmailNotificationService, WebSocketAuthService, TemplateProcessorService],
- exports: [EmailService, MailboxResolverService, EmailGateway, EmailNotificationService, WebSocketAuthService, TemplateProcessorService],
+ providers: [EmailService, MailboxResolverService, EmailGateway, EmailNotificationService, WebSocketAuthService],
+ exports: [EmailService, MailboxResolverService, EmailGateway, EmailNotificationService, WebSocketAuthService],
})
export class EmailModule {}
diff --git a/src/modules/email/services/email.service.ts b/src/modules/email/services/email.service.ts
index 70fb0da..d63b3ac 100644
--- a/src/modules/email/services/email.service.ts
+++ b/src/modules/email/services/email.service.ts
@@ -37,15 +37,20 @@ export class EmailService {
this.logger.log(`Processing email through business template for business: ${user.business.id}`);
// Process email content through business template
- const processedTemplate = await this.templateProcessorService.processEmailWithTemplate(user.business.id, {
+ const processedTemplate = await this.templateProcessorService.processEmailContent(user.business.id, {
text: sendEmailDto.text,
html: sendEmailDto.html,
subject: sendEmailDto.subject,
});
// Update the DTO with processed content
- sendEmailDto.html = processedTemplate.html;
- sendEmailDto.text = processedTemplate.text;
+ if (processedTemplate.isHtml) {
+ sendEmailDto.html = processedTemplate.content;
+ sendEmailDto.text = undefined; // Clear text when using HTML template
+ } else {
+ sendEmailDto.text = processedTemplate.content;
+ sendEmailDto.html = undefined; // Clear HTML when no template
+ }
if (processedTemplate.hasTemplate) {
this.logger.log(`Email content processed through template for business: ${user.business.id}`);
@@ -410,14 +415,19 @@ export class EmailService {
const user = await this.userRepository.findOne({ wildduckUserId: userEmailId, deletedAt: null }, { populate: ["business"] });
if (user && user.business && (updateDraftDto.text || updateDraftDto.html)) {
- const processedTemplate = await this.templateProcessorService.processEmailWithTemplate(user.business.id, {
+ const processedTemplate = await this.templateProcessorService.processEmailContent(user.business.id, {
text: updateDraftDto.text,
html: updateDraftDto.html,
subject: updateDraftDto.subject,
});
- draftData.html = processedTemplate.html;
- draftData.text = processedTemplate.text;
+ if (processedTemplate.isHtml) {
+ draftData.html = processedTemplate.content;
+ draftData.text = undefined;
+ } else {
+ draftData.text = processedTemplate.content;
+ draftData.html = undefined;
+ }
if (processedTemplate.hasTemplate) {
this.logger.log(`Draft content processed through template for business: ${user.business.id}`);
diff --git a/src/modules/templates/interfaces/structure.interface.ts b/src/modules/templates/interfaces/structure.interface.ts
index 36c1486..3cae048 100644
--- a/src/modules/templates/interfaces/structure.interface.ts
+++ b/src/modules/templates/interfaces/structure.interface.ts
@@ -102,3 +102,15 @@ export type ImageType = {
alignment?: HorizontalAlignment;
verticalAlignment?: VerticalAlignment;
};
+
+export interface EmailContent {
+ text?: string;
+ html?: string;
+ subject?: string;
+}
+
+export interface ProcessedEmailResult {
+ content: string;
+ isHtml: boolean;
+ hasTemplate: boolean;
+}
diff --git a/src/modules/templates/services/template-processor.service.ts b/src/modules/templates/services/template-processor.service.ts
index 20e4a8a..8a9c93e 100644
--- a/src/modules/templates/services/template-processor.service.ts
+++ b/src/modules/templates/services/template-processor.service.ts
@@ -1,13 +1,7 @@
import { Injectable, Logger } from "@nestjs/common";
import { TemplatesService } from "./templates.service";
-// import { PersonalityDataType, SectionItemType, TextType } from "../interfaces/structure.interface";
-
-interface ProcessedTemplateResult {
- html: string;
- text: string;
- hasTemplate: boolean;
-}
+import { EmailContent, ProcessedEmailResult } from "../interfaces/structure.interface";
@Injectable()
export class TemplateProcessorService {
@@ -15,101 +9,66 @@ export class TemplateProcessorService {
constructor(private readonly templatesService: TemplatesService) {}
- /**
- * Process email content through business template
- */
- async processEmailWithTemplate(
- businessId: string,
- emailContent: { text?: string; html?: string; subject?: string },
- // userId?: string,
- ): Promise
")
- .replace(/\n/g, "
")
- .replace(/^(.*)$/, "
$1
"); - } - - /** - * Strip HTML tags from content - */ + //**************************************************** */ private stripHtmlTags(html: string): string { return html .replace(/<[^>]*>/g, "") @@ -172,6 +89,7 @@ export class TemplateProcessorService { .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, '"') + .replace(/'/g, "'") .trim(); } } diff --git a/src/modules/templates/templates.controller.ts b/src/modules/templates/templates.controller.ts index 4baaac5..9e3b0cf 100644 --- a/src/modules/templates/templates.controller.ts +++ b/src/modules/templates/templates.controller.ts @@ -57,7 +57,7 @@ export class TemplatesController { return this.templatesService.deleteTemplate(params.id, businessId); } - @Post(":id/set-selected") + @Patch(":id/set-selected") @ApiOperation({ summary: "Set template as selected (unselects all other templates for the business)" }) @ApiResponse({ status: 200, description: "Template set as selected successfully" }) @ApiResponse({ status: 404, description: "Template not found" })