100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
import { prismaClient } from '../../../../../common/database/prisma.lambda.service';
|
|
import { verifyHostToken } from '../../../../../common/middlewares/jwt/authForHost';
|
|
import { safeHandler } from '../../../../../common/utils/handlers/safeHandler';
|
|
import ApiError from '../../../../../common/utils/helper/ApiError';
|
|
import { SchedulingService } from '../../../services/activityScheduling.service';
|
|
import { HostService } from '../../../services/host.service';
|
|
|
|
const schedulingService = new SchedulingService(prismaClient);
|
|
const hostService = new HostService(prismaClient);
|
|
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context
|
|
): Promise<APIGatewayProxyResult> => {
|
|
// Extract token from headers
|
|
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.');
|
|
}
|
|
|
|
// Authenticate user using the shared authForHost function
|
|
const userInfo = await verifyHostToken(token);
|
|
const hostId = userInfo.id;
|
|
|
|
if (Number.isNaN(hostId)) {
|
|
throw new ApiError(400, 'Host id must be a number');
|
|
}
|
|
|
|
const host = await hostService.getHostIdByUserXid(hostId);
|
|
if (!host) {
|
|
throw new ApiError(404, 'Host not found');
|
|
}
|
|
|
|
let body: {
|
|
activityXid: number;
|
|
venueXid: number;
|
|
cancellations: {
|
|
scheduleHeaderXid: number;
|
|
occurenceDate: string;
|
|
startTime: string;
|
|
endTime: string;
|
|
cancellationReason: string
|
|
}[]
|
|
};
|
|
|
|
try {
|
|
body = event.body ? JSON.parse(event.body) : {};
|
|
} catch {
|
|
throw new ApiError(400, 'Invalid JSON payload');
|
|
}
|
|
|
|
if (!body.activityXid || !body.venueXid || !Array.isArray(body.cancellations) || body.cancellations.length === 0) {
|
|
throw new ApiError(400, 'Missing required fields');
|
|
}
|
|
|
|
const activity = await schedulingService.getActivityByXid(body.activityXid);
|
|
if (!activity) {
|
|
throw new ApiError(404, "Activity not found");
|
|
}
|
|
|
|
const venueExists = await schedulingService.getVenueFromVenueXid(
|
|
body.venueXid,
|
|
body.activityXid
|
|
);
|
|
|
|
if (!venueExists) {
|
|
throw new ApiError(
|
|
404,
|
|
`Venue not found for this activity`
|
|
)
|
|
}
|
|
|
|
|
|
await schedulingService.cancelMultipleSlotsForActivity(
|
|
body.cancellations.map((item: any) => ({
|
|
scheduleHeaderXid: Number(item.scheduleHeaderXid),
|
|
occurenceDate: item.occurenceDate,
|
|
startTime: item.startTime,
|
|
endTime: item.endTime,
|
|
cancellationReason: item.cancellationReason
|
|
}))
|
|
);
|
|
|
|
|
|
const result = await schedulingService.getVenueDurationByAct(Number(body.activityXid), Number(hostId));
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Slot blocked successfully',
|
|
data: result
|
|
}),
|
|
};
|
|
}); |