added validation for entry count of the user's connect details and made removeConnectionDetails api
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
@@ -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<APIGatewayProxyResult> => {
|
||||
// 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,
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user