Add suggestion and getsuggestion

This commit is contained in:
paritosh18
2025-11-19 13:33:32 +05:30
parent d65fc684db
commit 803a11cdda
5 changed files with 265 additions and 0 deletions

View File

@@ -336,6 +336,36 @@ functions:
- httpApi:
path: /minglaradmin/get-all-invitation-details
method: get
addSuggestion:
handler: src/modules/minglaradmin/handlers/addSuggestion.handler
package:
patterns:
- "src/modules/minglaradmin/**"
- "common/**"
- "src/common/**"
- "node_modules/@prisma/client/**"
- "node_modules/.prisma/**"
events:
- httpApi:
path: /minglaradmin/add-suggestion
method: post
getSuggestion:
handler: src/modules/minglaradmin/handlers/getSuggestion.handler
package:
patterns:
- "src/modules/minglaradmin/**"
- "common/**"
- "src/common/**"
- "node_modules/@prisma/client/**"
- "node_modules/.prisma/**"
events:
- httpApi:
path: /minglaradmin/get-suggestion
method: get
getAllCoadminAndAMDetails:

View File

@@ -21,4 +21,11 @@ export const MINGLAR_INVITATION_STATUS = {
ACCEPTED: "Accepted",
REJECTED: "Rejected",
INVITED: "Invited",
}
export const HOST_SUGGESTION_TITLES = {
SETUP_PROFILE: "Setup Profile",
REVIEW_ACCOUNT: "Review Account",
ADD_PAYMENT_DETAILS: "Add Payment Details",
AGREEMENT: "Agreement"
}

View File

@@ -0,0 +1,91 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
import { PrismaService } from '../../../common/database/prisma.service';
import { MinglarService } from '../services/minglar.service';
import ApiError from '../../../common/utils/helper/ApiError';
import { verifyMinglarAdminToken } from '../../../common/middlewares/jwt/authForMinglarAdmin';
import { HOST_SUGGESTION_TITLES } from '../../../common/utils/constants/minglar.constant';
const prismaService = new PrismaService();
const minglarService = new MinglarService(prismaService);
interface AddSuggestionBody {
hostXid: number;
title: string;
comments: string;
}
/**
* Add suggestion handler for host applications
* Allows Minglar Admin, Co_Admin, and Account Manager to add suggestions
* Types: Setup Profile, Review Account, Add Payment Details, Agreement
*/
export const handler = safeHandler(async (
event: APIGatewayProxyEvent,
context?: Context
): Promise<APIGatewayProxyResult> => {
// Verify authentication token
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token'];
if (!token) {
throw new ApiError(401, 'This is a protected route. Please provide a valid token.');
}
// Verify token and get user info
const userInfo = await verifyMinglarAdminToken(token);
// Get user details
const user = await prismaService.user.findUnique({
where: { id: userInfo.id },
select: { id: true, roleXid: true }
});
if (!user) {
throw new ApiError(404, 'User not found');
}
// Parse request body
let body: AddSuggestionBody;
try {
body = event.body ? JSON.parse(event.body) : {};
} catch (error) {
throw new ApiError(400, 'Invalid JSON in request body');
}
const { hostXid, title, comments } = body;
// Validate required fields
if (!hostXid) {
throw new ApiError(400, 'Host ID is required');
}
if (!title) {
throw new ApiError(400, 'Title is required');
}
if (!comments) {
throw new ApiError(400, 'Comments are required');
}
// Validate title is one of the allowed types
const allowedTitles = Object.values(HOST_SUGGESTION_TITLES);
if (!allowedTitles.includes(title)) {
throw new ApiError(400, `Invalid title. Allowed values: ${allowedTitles.join(', ')}`);
}
// Add suggestion using service
await minglarService.addHostSuggestion(hostXid, title, comments, user.id);
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Suggestion added successfully',
data: null,
}),
};
});

View File

@@ -0,0 +1,53 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
import { PrismaService } from '../../../common/database/prisma.service';
import { MinglarService } from '../services/minglar.service';
import ApiError from '../../../common/utils/helper/ApiError';
import { verifyMinglarAdminToken } from '../../../common/middlewares/jwt/authForMinglarAdmin';
const prismaService = new PrismaService();
const minglarService = new MinglarService(prismaService);
/**
* Get suggestions handler
* Retrieves suggestions based on user's role and host assignments
*/
export const handler = safeHandler(async (
event: APIGatewayProxyEvent,
context?: Context
): Promise<APIGatewayProxyResult> => {
// Verify authentication token
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token'];
if (!token) {
throw new ApiError(401, 'This is a protected route. Please provide a valid token.');
}
// Verify token and get user info
const userInfo = await verifyMinglarAdminToken(token);
// Get user details including role
const user = await prismaService.user.findUnique({
where: { id: userInfo.id },
select: { id: true, roleXid: true }
});
if (!user) {
throw new ApiError(404, 'User not found');
}
// Get suggestions using service
const suggestions = await minglarService.getHostSuggestions(user.id, user.roleXid);
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Suggestions retrieved successfully',
data: suggestions,
}),
};
});

View File

@@ -536,6 +536,90 @@ export class MinglarService {
return true;
}
async addHostSuggestion(hostXid: number, title: string, comments: string, reviewedByXid: number) {
// Check if host exists
const hostHeader = await this.prisma.hostHeader.findUnique({
where: { userXid: hostXid },
select: { id: true }
});
console.log(hostHeader)
if (!hostHeader) {
throw new ApiError(404, 'Host not found');
}
// Create suggestion in host_suggestion table
await this.prisma.hostSuggestion.create({
data: {
hostXid: hostXid,
title: title,
comments: comments,
isparent: false,
isreviewed: false,
reviewedByXid: reviewedByXid,
reviewOn: null,
isActive: true
}
});
return true;
}
async getHostSuggestions(userId: number, userRoleXid: number) {
// Build where clause based on user role
const whereClause: any = {
isActive: true
};
// If user is Co_Admin or Account_Manager, filter by assigned hosts only
if (userRoleXid === ROLE.CO_ADMIN || userRoleXid === ROLE.ACCOUNT_MANAGER) {
whereClause.host = {
accountManagerXid: userId
};
}
// If user is Minglar Admin, show all suggestions (no additional filter needed)
const suggestions = await this.prisma.hostSuggestion.findMany({
where: whereClause,
select: {
id: true,
title: true,
comments: true,
isparent: true,
isreviewed: true,
reviewOn: true,
createdAt: true,
host: {
select: {
id: true,
companyName: true,
user: {
select: {
id: true,
firstName: true,
lastName: true,
emailAddress: true
}
}
}
},
reviewedBy: {
select: {
id: true,
firstName: true,
lastName: true,
emailAddress: true
}
}
},
orderBy: {
createdAt: 'desc'
}
});
return suggestions;
}
}