Add addActivity handler and createActivity method in HostService

- Implemented addActivity handler to create new activities with validation.
- Added createActivity method in HostService to handle activity creation logic.
This commit is contained in:
paritosh18
2025-11-22 20:48:45 +05:30
parent d21dcacd7b
commit 1c83cc5910
3 changed files with 102 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import { verifyHostToken } from '@/common/middlewares/jwt/authForHost';
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { PrismaService } from '../../../common/database/prisma.service';
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
import ApiError from '../../../common/utils/helper/ApiError';
import { HostService } from '../services/host.service';
const prismaService = new PrismaService();
const hostService = new HostService(prismaService);
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(401, 'This is a protected route. Please provide a valid token.');
const userInfo = await verifyHostToken(token);
let body: any = {};
try {
body = event.body ? JSON.parse(event.body) : {};
} catch (err) {
throw new ApiError(400, 'Invalid JSON in request body');
}
const { activityTypeXid, frequenciesXid } = body;
if (!activityTypeXid) {
throw new ApiError(400, 'activityTypeXid is required');
}
const created = await hostService.createActivity(
userInfo.id,
Number(activityTypeXid),
frequenciesXid ? Number(frequenciesXid) : undefined,
);
return {
statusCode: 201,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Activity created successfully',
data: created,
}),
};
});

View File

@@ -742,4 +742,39 @@ export class HostService {
});
}
async createActivity(
userId: number,
activityTypeXid: number,
frequenciesXid?: number,
) {
// Find host header for this user
const host = await this.prisma.hostHeader.findFirst({ where: { userXid: userId, isActive: true } });
if (!host) {
throw new ApiError(404, 'Host not found for the user');
}
// Validate activityType exists
const activityType = await this.prisma.activityTypes.findUnique({ where: { id: activityTypeXid } });
if (!activityType) {
throw new ApiError(404, 'Activity type not found');
}
// Optionally validate frequency
if (frequenciesXid) {
const freq = await this.prisma.frequencies.findUnique({ where: { id: frequenciesXid } });
if (!freq) throw new ApiError(404, 'Frequency not found');
}
const created = await this.prisma.activities.create({
data: {
hostXid: host.id,
activityTypeXid: activityTypeXid,
frequenciesXid: frequenciesXid || null,
isActive: true,
},
});
return created;
}
}