feat: add setUserInterests functionality to manage user interests
This commit is contained in:
@@ -60,4 +60,19 @@ setPasscodeForMobile:
|
|||||||
events:
|
events:
|
||||||
- httpApi:
|
- httpApi:
|
||||||
path: /user/set-passcode
|
path: /user/set-passcode
|
||||||
|
method: post
|
||||||
|
|
||||||
|
setUserInterest:
|
||||||
|
handler: src/modules/user/handlers/authentication/SetuserInterest.handler
|
||||||
|
memorySize: 384
|
||||||
|
package:
|
||||||
|
patterns:
|
||||||
|
- 'src/modules/user/**'
|
||||||
|
- ${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/set-interests
|
||||||
method: post
|
method: post
|
||||||
64
src/modules/user/handlers/authentication/SetuserInterest.ts
Normal file
64
src/modules/user/handlers/authentication/SetuserInterest.ts
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
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 { UserService } from '../../services/user.service';
|
||||||
|
|
||||||
|
const userService = new UserService(prismaClient);
|
||||||
|
|
||||||
|
export const handler = safeHandler(async (
|
||||||
|
event: APIGatewayProxyEvent,
|
||||||
|
context?: Context
|
||||||
|
): Promise<APIGatewayProxyResult> => {
|
||||||
|
// Extract token from headers
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Authenticate user using verifyUserToken
|
||||||
|
const userInfo = await verifyUserToken(token);
|
||||||
|
const userId = userInfo.id;
|
||||||
|
|
||||||
|
if (Number.isNaN(userId)) {
|
||||||
|
throw new ApiError(400, 'User id must be a number');
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await userService.getUserById(userId);
|
||||||
|
if (!user) {
|
||||||
|
throw new ApiError(404, 'User not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse request body
|
||||||
|
let body: { interest_Xid?: number[]; };
|
||||||
|
|
||||||
|
try {
|
||||||
|
body = event.body ? JSON.parse(event.body) : {};
|
||||||
|
} catch (error) {
|
||||||
|
throw new ApiError(400, 'Invalid JSON in request body');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { interest_Xid } = body;
|
||||||
|
|
||||||
|
// Validate required fields
|
||||||
|
if (!interest_Xid || !Array.isArray(interest_Xid)) {
|
||||||
|
throw new ApiError(400, 'interest_Xid must be a non-empty array of numbers');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Set the interests
|
||||||
|
await userService.setUserInterests(userId, interest_Xid);
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
message: 'Interests set successfully',
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
@@ -121,4 +121,22 @@ export class UserService {
|
|||||||
|
|
||||||
return updatedUser;
|
return updatedUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setUserInterests(userId: number, interest_Xid: number[]): Promise<void> {
|
||||||
|
// Remove existing interests
|
||||||
|
await this.prisma.userInterests.deleteMany({
|
||||||
|
where: { userXid: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add new interests
|
||||||
|
const interestRecords = interest_Xid.map((interestId) => ({
|
||||||
|
userXid: userId,
|
||||||
|
interestXid: interestId,
|
||||||
|
}));
|
||||||
|
|
||||||
|
await this.prisma.userInterests.createMany({
|
||||||
|
data: interestRecords,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user