adding in stuff to make search work but still testing and stuff
This commit is contained in:
@@ -3,6 +3,7 @@ import express, { Application, Request, Response } from "express";
|
||||
import { NextFunction } from "express";
|
||||
import { connectToDatabase } from "./services/database.service";
|
||||
import cors from "cors";
|
||||
import getSearchRoutes from "./routes/search";
|
||||
import getUserRoutes from "./routes/user";
|
||||
|
||||
const app = express();
|
||||
@@ -19,6 +20,7 @@ app.get("/", (req: Request, res: Response) => {
|
||||
});
|
||||
|
||||
app.use("/api/users", getUserRoutes());
|
||||
app.use("/api/search", getSearchRoutes());
|
||||
|
||||
app.listen(5000, () => console.log("Example app is listening on port 5000."));
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ export const authCheck = async (
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(401).send("unauthorized");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import express, { Application, Request, Response } from "express";
|
||||
|
||||
import SearchResponse from "@nirvana/core/responses/search.response";
|
||||
import { User } from "@nirvana/core/models";
|
||||
import { UserService } from "../services/user.service";
|
||||
import { authCheck } from "../middleware/auth";
|
||||
|
||||
export default function getSearchRoutes() {
|
||||
const router = express.Router();
|
||||
|
||||
router.use(express.json());
|
||||
|
||||
// get user details based on id token
|
||||
router.get("/", authCheck, handleSearch);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
async function handleSearch(req: Request, res: Response) {
|
||||
try {
|
||||
const { query } = req.query;
|
||||
|
||||
if (!query) {
|
||||
res.status(400).send("No search query provided!");
|
||||
return;
|
||||
}
|
||||
|
||||
// text search on users
|
||||
const users: User[] | null = await UserService.getUsersLikeEmailAndName(
|
||||
query as string
|
||||
);
|
||||
|
||||
const resObj = new SearchResponse(users ?? []);
|
||||
|
||||
res.send(resObj);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).send(`something went wrong`);
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,32 @@ export class UserService {
|
||||
return null;
|
||||
}
|
||||
|
||||
static async getUsersLikeEmailAndName(searchQuery: string) {
|
||||
// based on index defined in Mongo atlas
|
||||
const query = {
|
||||
$search: {
|
||||
index: "default",
|
||||
text: {
|
||||
query: searchQuery,
|
||||
path: {
|
||||
wildcard: "*",
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// const res = await collections.users?.find(query).toArray();
|
||||
|
||||
const res = await collections.users?.aggregate([query]).toArray();
|
||||
|
||||
// exists
|
||||
if (res?.length) {
|
||||
return res as User[];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async createUserIfNotExists(newUser: User) {
|
||||
const exists = (await collections.users?.findOne({ email: newUser.email }))
|
||||
?._id;
|
||||
|
||||
Reference in New Issue
Block a user