99 lines
3.2 KiB
TypeScript
99 lines
3.2 KiB
TypeScript
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
import { prismaClient } from '../../../../common/database/prisma.lambda.service';
|
|
import { verifyUserToken } from '../../../../common/middlewares/jwt/authForUser';
|
|
import { safeHandler } from '../../../../common/utils/handlers/safeHandler';
|
|
import ApiError from '../../../../common/utils/helper/ApiError';
|
|
import { ItineraryService } from '../../services/itinerary.service';
|
|
|
|
const itineraryService = new ItineraryService(prismaClient);
|
|
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context,
|
|
): Promise<APIGatewayProxyResult> => {
|
|
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token'];
|
|
if (!token) {
|
|
throw new ApiError(
|
|
400,
|
|
'This is a protected route. Please provide a valid token.',
|
|
);
|
|
}
|
|
|
|
const userInfo = await verifyUserToken(token);
|
|
const userId = Number(userInfo.id);
|
|
|
|
if (!userId || Number.isNaN(userId)) {
|
|
throw new ApiError(400, 'Invalid user ID');
|
|
}
|
|
|
|
let body: Record<string, any> = {};
|
|
if (event.body) {
|
|
try {
|
|
body = JSON.parse(event.body);
|
|
} catch {
|
|
throw new ApiError(400, 'Invalid JSON body');
|
|
}
|
|
}
|
|
|
|
const itineraryActivityXid = Number(body.itineraryActivityXid);
|
|
if (!Number.isInteger(itineraryActivityXid) || itineraryActivityXid <= 0) {
|
|
throw new ApiError(400, 'itineraryActivityXid is required.');
|
|
}
|
|
|
|
const selectedEquipmentIds = Array.isArray(body.selectedEquipmentIds)
|
|
? body.selectedEquipmentIds.map((id: unknown) => Number(id))
|
|
: [];
|
|
const selectedFoodTypeIds = Array.isArray(body.selectedFoodTypeIds)
|
|
? body.selectedFoodTypeIds.map((id: unknown) => Number(id))
|
|
: [];
|
|
|
|
if (selectedEquipmentIds.some((id) => !Number.isInteger(id) || id <= 0)) {
|
|
throw new ApiError(400, 'selectedEquipmentIds must contain valid ids.');
|
|
}
|
|
|
|
if (selectedFoodTypeIds.some((id) => !Number.isInteger(id) || id <= 0)) {
|
|
throw new ApiError(400, 'selectedFoodTypeIds must contain valid ids.');
|
|
}
|
|
|
|
const toOptionalId = (value: unknown) => {
|
|
if (value === undefined || value === null || value === '') {
|
|
return null;
|
|
}
|
|
|
|
const parsed = Number(value);
|
|
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
throw new ApiError(400, 'One or more selected option ids are invalid.');
|
|
}
|
|
|
|
return parsed;
|
|
};
|
|
|
|
const result = await itineraryService.saveItineraryActivitySelections(userId, {
|
|
itineraryActivityXid,
|
|
isFoodOpted:
|
|
body.isFoodOpted === undefined ? false : Boolean(body.isFoodOpted),
|
|
selectedFoodTypeIds,
|
|
isTrainerOpted:
|
|
body.isTrainerOpted === undefined ? false : Boolean(body.isTrainerOpted),
|
|
isInActivityNavigationOpted:
|
|
body.isInActivityNavigationOpted === undefined
|
|
? false
|
|
: Boolean(body.isInActivityNavigationOpted),
|
|
selectedNavigationModeXid: toOptionalId(body.selectedNavigationModeXid),
|
|
selectedEquipmentIds,
|
|
});
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Itinerary activity selections saved successfully.',
|
|
data: result,
|
|
}),
|
|
};
|
|
});
|