fix : test email : more detail

This commit is contained in:
morteza-mortezai
2025-12-07 23:08:08 +03:30
parent 9aadf42ceb
commit 05b8daede6
3 changed files with 104 additions and 6 deletions
+23 -1
View File
@@ -1,4 +1,4 @@
import { Controller, Get, Query } from "@nestjs/common";
import { Body, Controller, Get, Post, Query } from "@nestjs/common";
import { ApiOperation, ApiResponse } from "@nestjs/swagger";
import { EmailSpamService } from "./services/email-spam.service";
@@ -28,4 +28,26 @@ export class EmailBlockingController {
const shouldUnblock = unblock === "true" || unblock === "1";
return this.emailSpamService.diagnoseEmailBlocking(recipientEmail, senderDomain, shouldUnblock);
}
@Post("allowlist")
@AdminRoute()
@ApiOperation({
summary: "Add domain to allowlist (Admin only)",
description: "Add a sender domain to the allowlist for a recipient's business",
})
@ApiResponse({ status: 200, description: "Domain added to allowlist" })
@ApiResponse({ status: 400, description: "Invalid parameters" })
@ApiResponse({ status: 403, description: "Not authorized (Admin only)" })
async addToAllowlist(
@Body() body: { recipientEmail: string; senderDomain: string; description?: string },
) {
if (!body.recipientEmail || !body.senderDomain) {
throw new Error("Both recipientEmail and senderDomain are required");
}
return this.emailSpamService.addDomainToAllowlistForUser(
body.recipientEmail,
body.senderDomain,
body.description,
);
}
}
@@ -508,4 +508,39 @@ export class EmailSpamService {
return result;
}
//==============================================
/**
* Add domain to allowlist for a specific user's business
*/
async addDomainToAllowlistForUser(recipientEmail: string, senderDomain: string, description?: string) {
this.logger.log(`Adding domain ${senderDomain} to allowlist for user ${recipientEmail}`);
// Find user by email
const user = await this.userRepository.findOne({ emailAddress: recipientEmail, deletedAt: null }, { populate: ["business"] });
if (!user) {
throw new Error(`User with email ${recipientEmail} not found`);
}
const domainTag = user.business?.id || "default";
const finalDescription = description || `Manually allowlisted for ${recipientEmail}`;
try {
const result = await this.addDomainToAllowlist(senderDomain, finalDescription, domainTag);
this.logger.log(`Successfully added ${senderDomain} to allowlist for domain tag: ${domainTag}`);
return {
success: true,
message: `Domain ${senderDomain} has been added to allowlist for ${recipientEmail}`,
domainTag,
domain: senderDomain,
description: finalDescription,
result,
};
} catch (error) {
this.logger.error(`Failed to add domain to allowlist: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
}