91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
import { prismaClient } from '../../../../../common/database/prisma.lambda.service';
|
|
import { verifyMinglarAdminToken } from '../../../../../common/middlewares/jwt/authForMinglarAdmin';
|
|
import { safeHandler } from '../../../../../common/utils/handlers/safeHandler';
|
|
import ApiError from '../../../../../common/utils/helper/ApiError';
|
|
import { MinglarService } from '../../../services/minglar.service';
|
|
|
|
const minglarService = new MinglarService(prismaClient);
|
|
|
|
interface AddSuggestionBody {
|
|
hostXid: number;
|
|
title: string;
|
|
comments: string;
|
|
isParent: boolean;
|
|
}
|
|
|
|
/**
|
|
* 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 prismaClient.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, isParent } = 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, isParent);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Suggestion added successfully',
|
|
data: null,
|
|
}),
|
|
};
|
|
});
|