Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1 +1,83 @@
|
||||
//#do your code here
|
||||
import {
|
||||
APIGatewayProxyEvent,
|
||||
APIGatewayProxyResult,
|
||||
Context,
|
||||
} from 'aws-lambda';
|
||||
import { prismaClient } from '../../../../common/database/prisma.lambda.service';
|
||||
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 query parameters
|
||||
const searchQuery = event.queryStringParameters?.searchQuery?.trim();
|
||||
const isSchool = event.queryStringParameters?.isSchool?.toLowerCase();
|
||||
|
||||
// Validate inputs
|
||||
if (!searchQuery || searchQuery.length === 0) {
|
||||
throw new ApiError(400, 'Search query is required');
|
||||
}
|
||||
|
||||
if (searchQuery.length < 2) {
|
||||
throw new ApiError(
|
||||
400,
|
||||
'Search query must be at least 2 characters long',
|
||||
);
|
||||
}
|
||||
|
||||
// Validate isSchool parameter
|
||||
if (!isSchool || !['true', 'false'].includes(isSchool)) {
|
||||
throw new ApiError(
|
||||
400,
|
||||
'isSchool parameter must be either "true" (for schools) or "false" (for companies)',
|
||||
);
|
||||
}
|
||||
|
||||
// Convert isSchool to boolean
|
||||
const filterBySchool = isSchool === 'true';
|
||||
|
||||
// Call service to search
|
||||
const results = await userService.searchSchoolsAndCompanies(
|
||||
searchQuery,
|
||||
filterBySchool,
|
||||
);
|
||||
|
||||
// Check if any results found
|
||||
if (results.length === 0) {
|
||||
const type = filterBySchool ? 'school' : 'company';
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: `No ${type}s found matching your search`,
|
||||
data: [],
|
||||
count: 0,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: `${filterBySchool ? 'Schools' : 'Companies'} found successfully`,
|
||||
data: results,
|
||||
count: results.length,
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
64
src/modules/user/handlers/connections/searchCities.ts
Normal file
64
src/modules/user/handlers/connections/searchCities.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
APIGatewayProxyEvent,
|
||||
APIGatewayProxyResult,
|
||||
Context,
|
||||
} from 'aws-lambda';
|
||||
import { prismaClient } from '../../../../common/database/prisma.lambda.service';
|
||||
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 query parameters
|
||||
const searchQuery = event.queryStringParameters?.searchQuery?.trim();
|
||||
|
||||
// Validate inputs
|
||||
if (!searchQuery || searchQuery.length === 0) {
|
||||
throw new ApiError(400, 'Search query is required');
|
||||
}
|
||||
|
||||
if (searchQuery.length < 2) {
|
||||
throw new ApiError(400, 'Search query must be at least 2 characters long');
|
||||
}
|
||||
|
||||
// Call service to search cities
|
||||
const results = await userService.searchCities(searchQuery);
|
||||
|
||||
// Check if any results found
|
||||
if (results.length === 0) {
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'No cities found matching your search',
|
||||
data: [],
|
||||
count: 0,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'Cities found successfully',
|
||||
data: results,
|
||||
count: results.length,
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user