Added remove api from interested
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
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: { activityXid: number; isBucket: boolean; bucketTypeName: string; };
|
||||
|
||||
try {
|
||||
body = event.body ? JSON.parse(event.body) : {};
|
||||
} catch (error) {
|
||||
throw new ApiError(400, 'Invalid JSON in request body');
|
||||
}
|
||||
|
||||
const { activityXid, isBucket, bucketTypeName } = body;
|
||||
|
||||
// Validate required fields
|
||||
if (
|
||||
typeof activityXid !== 'number' ||
|
||||
typeof isBucket !== 'boolean' ||
|
||||
!bucketTypeName
|
||||
) {
|
||||
throw new ApiError(400, 'Required fields missing or invalid');
|
||||
}
|
||||
|
||||
// Remove from bucket/interested
|
||||
const counts = await userService.removeFromBucketInterested(userId, isBucket, bucketTypeName, activityXid);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: `Activity removed from ${isBucket ? 'bucket' : 'interested'} successfully`,
|
||||
data: {
|
||||
bucketCount: counts.bucketCount,
|
||||
interestedCount: counts.interestedCount,
|
||||
}
|
||||
}),
|
||||
};
|
||||
});
|
||||
@@ -2629,6 +2629,19 @@ export class UserService {
|
||||
select: {
|
||||
id: true,
|
||||
schoolCompanyName: true,
|
||||
cities: {
|
||||
select: {
|
||||
id: true,
|
||||
cityName: true,
|
||||
states: {
|
||||
select: {
|
||||
id: true,
|
||||
stateName: true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
isSchool: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
@@ -3814,4 +3827,56 @@ export class UserService {
|
||||
};
|
||||
}
|
||||
|
||||
async removeFromBucketInterested(
|
||||
userXid: number,
|
||||
isBucket: boolean,
|
||||
bucketTypeName: string,
|
||||
activityXid: number
|
||||
) {
|
||||
const activityExists = await this.prisma.activities.findFirst({
|
||||
where: { id: activityXid, isActive: true },
|
||||
});
|
||||
|
||||
if (!activityExists) {
|
||||
throw new ApiError(404, 'Activity not found');
|
||||
}
|
||||
|
||||
const existing = await this.prisma.userBucketInterested.findFirst({
|
||||
where: { userXid, activityXid, isActive: true },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
throw new ApiError(400, 'Activity not found in bucket/interested list');
|
||||
}
|
||||
|
||||
await this.prisma.userBucketInterested.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
isActive: false,
|
||||
},
|
||||
});
|
||||
|
||||
// Get updated counts
|
||||
const [bucketCount, interestedCount] = await Promise.all([
|
||||
this.prisma.userBucketInterested.count({
|
||||
where: {
|
||||
userXid,
|
||||
isBucket: true,
|
||||
isActive: true,
|
||||
},
|
||||
}),
|
||||
this.prisma.userBucketInterested.count({
|
||||
where: {
|
||||
userXid,
|
||||
isBucket: false,
|
||||
isActive: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
bucketCount,
|
||||
interestedCount,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user