From 1c83cc5910d6753f6badc1309ac156c71d9cbc4e Mon Sep 17 00:00:00 2001 From: paritosh18 Date: Sat, 22 Nov 2025 20:48:45 +0530 Subject: [PATCH] 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. --- serverless.yml | 16 +++++++ src/modules/host/handlers/addActivity.ts | 51 +++++++++++++++++++++++ src/modules/host/services/host.service.ts | 35 ++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/modules/host/handlers/addActivity.ts diff --git a/serverless.yml b/serverless.yml index 3ea59f7..86cfa45 100644 --- a/serverless.yml +++ b/serverless.yml @@ -176,6 +176,22 @@ functions: path: /host/add-payment-details method: post + addActivity: + handler: src/modules/host/handlers/addActivity.handler + package: + patterns: + - 'src/modules/host/handlers/addActivity.*' + - 'src/modules/host/services/**' + - 'common/**' + - 'src/common/**' + - 'node_modules/@prisma/client/**' + - 'node_modules/.prisma/**' + + events: + - httpApi: + path: /host/add-activity + method: post + getHostById: handler: src/modules/host/handlers/getbyidhandler.handler package: diff --git a/src/modules/host/handlers/addActivity.ts b/src/modules/host/handlers/addActivity.ts new file mode 100644 index 0000000..bd1bc70 --- /dev/null +++ b/src/modules/host/handlers/addActivity.ts @@ -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 => { + 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, + }), + }; +}); diff --git a/src/modules/host/services/host.service.ts b/src/modules/host/services/host.service.ts index 62ffd65..7af8344 100644 --- a/src/modules/host/services/host.service.ts +++ b/src/modules/host/services/host.service.ts @@ -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; + } + }