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:
51
src/modules/host/handlers/addActivity.ts
Normal file
51
src/modules/host/handlers/addActivity.ts
Normal 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,
|
||||
}),
|
||||
};
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user