feat: add setUserLocationDetails functionality and corresponding API endpoint

This commit is contained in:
paritosh18
2026-02-02 17:09:42 +05:30
parent f414bc42f9
commit 558cb214c0
3 changed files with 189 additions and 1 deletions

View File

@@ -75,4 +75,19 @@ setUserInterest:
events:
- httpApi:
path: /user/set-interests
method: post
setUserLocationss:
handler: src/modules/user/handlers/authentication/SetLocationofUser.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-location-user
method: post

View File

@@ -0,0 +1,92 @@
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: {
countryName?: string;
stateName?: string;
cityName?: string;
pinCode?: string;
latitude?: number;
longitude?: number;
locationName?: string;
locationAddress?: string;
};
try {
body = event.body ? JSON.parse(event.body) : {};
} catch (error) {
throw new ApiError(400, 'Invalid JSON in request body');
}
const { countryName, stateName, cityName, pinCode, latitude, longitude, locationName, locationAddress } = body;
// Validate required fields
if (!countryName || !stateName || !cityName) {
throw new ApiError(400, 'Country name, state name, and city name are required');
}
// Set the location details
await userService.setUserLocationDetails(
userId,
countryName,
stateName,
cityName,
pinCode,
latitude,
longitude,
locationName,
locationAddress,
);
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Location details set successfully',
}),
};
},
);

View File

@@ -1,8 +1,9 @@
import { Injectable } from '@nestjs/common';
import { PrismaClient, User } from '@prisma/client';
import { PrismaClient, User, UserAddressDetails } from '@prisma/client';
import ApiError from '@/common/utils/helper/ApiError';
import * as bcrypt from 'bcryptjs';
import { UserPersonalInfoSchema } from '@/common/utils/validation/user/addPersonalInfo.validation';
import { create } from 'domain';
@Injectable()
export class UserService {
constructor(private prisma: PrismaClient) { }
@@ -139,4 +140,84 @@ export class UserService {
});
}
async setUserLocationDetails(
userId: number,
countryName: string,
stateName: string,
cityName: string,
pinCode: string,
latitude?: number,
longitude?: number,
locationName?: string,
locationAddress?: string
): Promise<UserAddressDetails> {
return this.prisma.$transaction(async (tx) => {
// 1⃣ Country: find or create
let country = await tx.countries.findUnique({
where: { countryName },
select: { id: true },
});
if (!country) {
country = await tx.countries.create({
data: {
countryName,
countryCode: countryName.slice(0, 3).toUpperCase(),
countryFlag: '',
},
select: { id: true },
});
}
// 2⃣ State: find or create (GLOBAL UNIQUE)
let state = await tx.states.findUnique({
where: { stateName },
select: { id: true },
});
if (!state) {
state = await tx.states.create({
data: {
stateName,
countryXid: country.id,
},
select: { id: true },
});
}
// 3⃣ City: find or create (GLOBAL UNIQUE)
let city = await tx.cities.findUnique({
where: { cityName },
select: { id: true },
});
if (!city) {
city = await tx.cities.create({
data: {
cityName,
stateXid: state.id,
},
select: { id: true },
});
}
// 4⃣ Update user address
return tx.userAddressDetails.update({
where: { id: userId },
data: {
countryXid: country.id,
stateXid: state.id,
cityXid: city.id,
pinCode,
locationName: locationName ?? null,
locationAddress: locationAddress ?? null,
locationLat: latitude ?? null,
locationLong: longitude ?? null,
},
});
});
}
}