92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
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;
|
|
activity_pqq_header_xid:number
|
|
}
|
|
|
|
/**
|
|
* 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 { title, comments , activity_pqq_header_xid} = body;
|
|
|
|
if (!title) {
|
|
throw new ApiError(400, 'Title is required');
|
|
}
|
|
|
|
if (!comments) {
|
|
throw new ApiError(400, 'Comments are required');
|
|
}
|
|
|
|
if(!activity_pqq_header_xid){
|
|
throw new ApiError(400 , "Activity Pqq HeaderXid 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.addPqqSuggestion(title, comments, activity_pqq_header_xid);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Suggestion added successfully',
|
|
data: null,
|
|
}),
|
|
};
|
|
});
|