From 81994d97fffa5be086b37669a1048fcf5b0f1635 Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Thu, 19 Feb 2026 20:41:00 +0530 Subject: [PATCH] added validation for entry count of the user's connect details and made removeConnectionDetails api --- serverless/functions/user.yml | 15 ++++ .../connections/addSchoolCompanyDetail.ts | 8 ++- .../connections/removeConnectionDetails.ts | 68 +++++++++++++++++++ src/modules/user/services/user.service.ts | 33 +++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/modules/user/handlers/connections/removeConnectionDetails.ts diff --git a/serverless/functions/user.yml b/serverless/functions/user.yml index 46cd536..93377eb 100644 --- a/serverless/functions/user.yml +++ b/serverless/functions/user.yml @@ -243,6 +243,21 @@ addSchoolCompanyDetail: path: /user/connections/add-school-company method: post +removeConnectionDetails: + handler: src/modules/user/handlers/connections/removeConnectionDetails.handler + memorySize: 384 + package: + patterns: + - 'src/modules/user/handlers/connections/**' + - ${file(./serverless/patterns/base.yml):pattern1} + - ${file(./serverless/patterns/base.yml):pattern2} + - ${file(./serverless/patterns/base.yml):pattern3} + - ${file(./serverless/patterns/base.yml):pattern4} + events: + - httpApi: + path: /user/connections/remove-connection-details + method: post + getAllConnectionOfUser: handler: src/modules/user/handlers/connections/getAllConnectionDetailsOfUser.handler memorySize: 384 diff --git a/src/modules/user/handlers/connections/addSchoolCompanyDetail.ts b/src/modules/user/handlers/connections/addSchoolCompanyDetail.ts index 91f489a..f5287c9 100644 --- a/src/modules/user/handlers/connections/addSchoolCompanyDetail.ts +++ b/src/modules/user/handlers/connections/addSchoolCompanyDetail.ts @@ -75,9 +75,15 @@ export const handler = safeHandler( throw new ApiError(400, 'cityXid is required and must be a number'); } + const recordCount = await userService.getConnectionCountOfUser(userId); + + if(recordCount >= 10) { + throw new ApiError(400, 'You have reached the maximum number of connections (10). Please remove an existing connection before adding a new one.'); + } + // Create DTO const dto = new AddSchoolCompanyDetailDTO( - schoolCompanyName.trim(), + schoolCompanyName.trim().toLowerCase(), isSchool, cityXid, ); diff --git a/src/modules/user/handlers/connections/removeConnectionDetails.ts b/src/modules/user/handlers/connections/removeConnectionDetails.ts new file mode 100644 index 0000000..d284396 --- /dev/null +++ b/src/modules/user/handlers/connections/removeConnectionDetails.ts @@ -0,0 +1,68 @@ +import { + APIGatewayProxyEvent, + APIGatewayProxyResult, + Context, +} from 'aws-lambda'; +import { prismaClient } from '../../../../common/database/prisma.lambda.service'; +import { verifyUserToken } from '../../../../common/middlewares/jwt/authForUser'; +import { safeHandler } from '../../../../common/utils/handlers/safeHandler'; +import ApiError from '../../../../common/utils/helper/ApiError'; +import { AddSchoolCompanyDetailDTO } from '../../dto/addSchoolCompanyDetail.dto'; +import { UserService } from '../../services/user.service'; + +const userService = new UserService(prismaClient); + +export const handler = safeHandler( + async ( + event: APIGatewayProxyEvent, + context?: Context, + ): Promise => { + // Extract 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.', + ); + } + + // Verify token and get user info + const userInfo = await verifyUserToken(token); + const userId = Number(userInfo.id); + + if (!userId || isNaN(userId)) { + throw new ApiError(400, 'Invalid user ID'); + } + + // Extract body parameters + let body; + try { + body = JSON.parse(event.body || '{}'); + } catch (error) { + throw new ApiError(400, 'Invalid JSON in request body'); + } + + const { connectDetailsXid } = body; + + if (!connectDetailsXid) { + throw new ApiError(400, 'connectDetailsXid is required'); + } + + // Call service to add or find school/company + const result = await userService.deleteConnectDetails(userId, Number(connectDetailsXid)); + + return { + statusCode: 200, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + }, + body: JSON.stringify({ + success: true, + message: 'Connection details removed successfully', + data: null, + }), + }; + }, +); diff --git a/src/modules/user/services/user.service.ts b/src/modules/user/services/user.service.ts index 64b3337..21b96c2 100644 --- a/src/modules/user/services/user.service.ts +++ b/src/modules/user/services/user.service.ts @@ -2583,4 +2583,37 @@ export class UserService { }); } + async getConnectionCountOfUser(userXid: number) { + return await this.prisma.connectDetails.count({ + where: { + userXid, + isActive: true, + }, + }); + } + + async deleteConnectDetails(userXid: number, connectDetailsXid: number) { + + if (!connectDetailsXid || isNaN(connectDetailsXid)) { + throw new ApiError(400, 'Invalid connection detail ID'); + } + + const existing = await this.prisma.connectDetails.findFirst({ + where: { + id: connectDetailsXid, + userXid, + isActive: true, + }, + }); + if (!existing) { + throw new ApiError(404, 'Connection detail not found'); + } + await this.prisma.connectDetails.delete({ + where: { + id: connectDetailsXid + } + }); + return true; + } + } \ No newline at end of file