Files
MinglarBackendNestJS/src/modules/host/handlers/Activity_Hub/Scheduling/getSchedulingOfAct.ts

65 lines
2.3 KiB
TypeScript
Raw Normal View History

import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { prismaClient } from '../../../../../common/database/prisma.lambda.service';
import { verifyMinglarAdminHostToken } from '../../../../../common/middlewares/jwt/authForMinglarAdminHost';
import { safeHandler } from '../../../../../common/utils/handlers/safeHandler';
import ApiError from '../../../../../common/utils/helper/ApiError';
import { SchedulingService } from '../../../services/activityScheduling.service';
import { ACTIVITY_INTERNAL_STATUS } from '../../../../../common/utils/constants/host.constant';
const schedulingService = new SchedulingService(prismaClient);
/**
* GET /activities
* Query Parameters:
* - status: Listed | Unlisted | Not_Listed (optional - if not provided, returns all)
* - hostId: ID of host (required from token)
*
* Returns activities based on status filter
*/
export const handler = safeHandler(async (
event: APIGatewayProxyEvent,
context?: Context
): Promise<APIGatewayProxyResult> => {
// Get and verify token
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 verifyMinglarAdminHostToken(token);
const userId = Number(userInfo.id);
// Get status filter from query parameters
const status = event.queryStringParameters?.status as string | undefined;
const hostId = await schedulingService.getHostIdByUserId(userId);
// Validate status if provided
const validStatuses = [ACTIVITY_INTERNAL_STATUS.ACTIVITY_APPROVED, ACTIVITY_INTERNAL_STATUS.ACTIVITY_UNLISTED, ACTIVITY_INTERNAL_STATUS.ACTIVITY_LISTED];
if (status && !validStatuses.includes(status)) {
throw new ApiError(400, `Invalid status. Must be one of: ${validStatuses.join(', ')}`);
}
// Get activities from service
const activities = await schedulingService.getActivitiesByStatus(hostId, status);
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Activities retrieved successfully',
data: {
total: activities.length,
activities: activities,
filter: {
status: status || 'All',
},
},
}),
};
});