Skip to content

Commit

Permalink
fixed search route issues
Browse files Browse the repository at this point in the history
  • Loading branch information
arogya01 committed Sep 5, 2024
1 parent 279708a commit f6c30c5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/modules/search/search.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { SearchQueryInput } from "./search.schema";
import prisma from "../../utils/prisma";

export async function searchUsers(
request: FastifyRequest<{ QueryString: SearchQueryInput }>,
request: FastifyRequest<{ Querystring: SearchQueryInput }>,
reply: FastifyReply
) {
const { username, email, phone } = request.query;

console.log('userName',username);
const whereClause: any = {};

if (username) {
Expand Down
5 changes: 1 addition & 4 deletions src/modules/search/search.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { searchUsers } from "./search.controller";
import { $ref, searchSchemas } from "./search.schema";

export default async function searchRoutes(server: FastifyInstance) {
for (const schema of searchSchemas) {
server.addSchema(schema);
}

server.get(
"api/users/search",
"/search",
{
schema: {
querystring: $ref("searchQuerySchema"),
Expand Down
5 changes: 4 additions & 1 deletion src/modules/search/search.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const searchResponseSchema = z.array(
})
);

export const { schemas: searchSchemas, $ref } = buildJsonSchemas({
export const { schemas: searchSchemas, $ref } = buildJsonSchemas({

searchQuerySchema,
searchResponseSchema,
},{
$id: "searchSchema",
});
15 changes: 13 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { userSchemas } from "./modules/user/user.schema";
import fastifyWebsocket from "@fastify/websocket";
import { chatRoutes } from "./modules/chat/chat.routes";
import { chatSchemas } from "./modules/chat/chat.schema";
import searchRoutes from "./modules/search/search.routes";
import { searchSchemas } from "./modules/search/search.schema";

declare module "fastify" {
interface FastifyRequest {
Expand Down Expand Up @@ -55,9 +57,17 @@ function buildServer() {
return next();
});

for (const schema of [...userSchemas]) {
server.addSchema(schema);
const allSchemas = [...userSchemas, ...chatSchemas, ...searchSchemas];

// Add schemas, avoiding duplicates
const addedSchemas = new Set();
for (const schema of allSchemas) {
if (schema.$id && !addedSchemas.has(schema.$id)) {
server.addSchema(schema);
addedSchemas.add(schema.$id);
}
}


server.get('/', async (request: FastifyRequest, reply: FastifyReply) => {
return {
Expand All @@ -67,6 +77,7 @@ function buildServer() {

server.register(userRoutes, { prefix: "api/users" });
server.register(chatRoutes, { prefix: "api/colloqui" });
server.register(searchRoutes, {prefix:"api/users" });

server.get('/health', async (request: FastifyRequest, reply: FastifyReply) => {
return {
Expand Down

0 comments on commit f6c30c5

Please sign in to comment.