Add new endpoints for activity types and frequencies, and implement email notifications for AM assignments

This commit is contained in:
paritosh18
2025-11-22 19:25:07 +05:30
parent 15c85686c6
commit d0b2de3f18
9 changed files with 353 additions and 84 deletions

View File

@@ -0,0 +1,37 @@
// ...existing code...
import { brevoService } from '@/common/email/brevoApi';
import ApiError from '@/common/utils/helper/ApiError';
export async function sendAMEmailForHostAssign(emailAddress: string): Promise<{
sent: boolean;
// messageId: string
}> {
const subject = 'Minglar Admin: Host Assignment Notification';
const htmlContent = `
<p>Hi,</p>
<p>Youve been assigned the <strong>Host</strong> role by Minglar Admin.</p>
<p>Best regards,<br/>Minglar Admin Team</p>
`;
try {
const result = await brevoService.sendEmail({
recipients: [{ email: emailAddress }],
subject,
htmlContent,
});
console.log('📧 Email sent successfully:', result);
return {
sent: true,
// messageId: result.messageId
};
} catch (err) {
console.error('Brevo email send failed:', err);
throw new ApiError(500, 'Failed to send invitation via email.');
}
}
// ...existing code...

View File

@@ -0,0 +1,34 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../../common/database/prisma.service';
import { sendAMEmailForHostAssign } from './AMEmail.service';
@Injectable()
export class AMNotificationService {
constructor(private prisma: PrismaService) {}
/**
* Fetch account manager email by id and send assignment email.
* Returns true if email was attempted (sent or attempted), false if AM missing or no email.
*/
async notifyAMOfAssignment(accountManagerXid: number, hostXid?: number): Promise<boolean> {
if (!accountManagerXid) return false;
const amUser = await this.prisma.user.findUnique({
where: { id: accountManagerXid },
select: { emailAddress: true, firstName: true, lastName: true },
});
if (!amUser || !amUser.emailAddress) {
console.warn(`AM notification skipped: user not found or missing email for id=${accountManagerXid}`);
return false;
}
try {
await sendAMEmailForHostAssign(amUser.emailAddress);
return true;
} catch (err) {
console.error('Error sending AM assignment email', err);
return false;
}
}
}

View File

@@ -7,6 +7,7 @@ import * as bcrypt from 'bcryptjs';
import { PrismaService } from '../../../common/database/prisma.service';
import ApiError from '../../../common/utils/helper/ApiError';
import { CreateMinglarDto, UpdateMinglarDto } from '../dto/minglar.dto';
import { sendAMEmailForHostAssign } from './AMEmail.service';
@Injectable()
@@ -615,6 +616,32 @@ export class MinglarService {
return true;
}
/**
* Notify Account Manager by email after assignment.
* Encapsulates lookup + email send so handlers can call a single method.
*/
async notifyAMOfAssignment(accountManagerXid: number): Promise<boolean> {
if (!accountManagerXid) return false;
const amUser = await this.prisma.user.findUnique({
where: { id: accountManagerXid ,isActive:true},
select: { emailAddress: true},
});
if (!amUser || !amUser.emailAddress) {
console.warn(`AM notification skipped: user not found or missing email for id=${accountManagerXid}`);
return false;
}
try {
await sendAMEmailForHostAssign(amUser.emailAddress);
return true;
} catch (err) {
console.error('Error sending AM assignment email', err);
return false;
}
}
async addHostSuggestion(hostXid: number, title: string, comments: string, reviewedByXid: number) {
// Check if host exists
const hostHeader = await this.prisma.hostHeader.findUnique({