feat(api): create an endpoint to retrieve users with the same ip address as another user
This commit is contained in:
parent
681d90dc6f
commit
e8bd88e58c
27
server/api/user/[id]/shared-ip.get.ts
Normal file
27
server/api/user/[id]/shared-ip.get.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { messages } from "~/lib/server/constants";
|
||||||
|
import isAdmin from "~/lib/server/middlewareButNotReally/isAdmin";
|
||||||
|
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
|
||||||
|
import { User } from "~/models/user";
|
||||||
|
|
||||||
|
export default eventHandler(async (ev) => {
|
||||||
|
isLoggedIn(ev);
|
||||||
|
isAdmin(ev);
|
||||||
|
let idee = parseInt(getRouterParam(ev, "id")!);
|
||||||
|
if (Number.isNaN(idee)) throw createError({ statusCode: 400 });
|
||||||
|
const user = await User.findById(idee);
|
||||||
|
if (!user) throw createError({ statusCode: 404, message: messages[404] });
|
||||||
|
let fin: any = {};
|
||||||
|
|
||||||
|
for (const entry of user?.ipLog) {
|
||||||
|
fin[entry.ip] = (
|
||||||
|
await User.find({
|
||||||
|
"ipLog.ip": {
|
||||||
|
$in: [entry.ip],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.select("username _id")
|
||||||
|
.exec()
|
||||||
|
).map((a) => a.toObject());
|
||||||
|
}
|
||||||
|
return fin;
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user