Files
MinglarBackendNestJS/src/modules/minglaradmin/handlers/hosthub/onboarding/assignAM.ts

84 lines
2.3 KiB
TypeScript

import {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Context,
} from 'aws-lambda';
import { prismaClient } from '../../../../../common/database/prisma.lambda.service';
import { verifyOnlyMinglarAdminToken } from '../../../../../common/middlewares/jwt/authForOnlyMinglarAdmin';
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 assignAMToHostBody {
host_xid: number;
account_manager_xid: number;
}
/**
* Get all host applications handler
* Returns host details with status, submission date, and account manager info
*/
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 verifyOnlyMinglarAdminToken(token);
// Get user details including role
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: assignAMToHostBody;
try {
body = event.body ? JSON.parse(event.body) : {};
} catch (error) {
throw new ApiError(400, 'Invalid JSON in request body');
}
const { host_xid, account_manager_xid } = body;
// Get all host applications from service based on user role
await minglarService.assignAMToHost(user.id, host_xid, account_manager_xid);
try {
await minglarService.notifyAMOfAssignment(account_manager_xid);
} catch (err) {
console.error('Failed to notify AM after assignment:', err);
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'AM assigned to host successfully',
}),
};
},
);