Files
encoach_frontend/src/pages/api/users/controller.ts

47 lines
1.1 KiB
TypeScript

import { sessionOptions } from '@/lib/session';
import { withIronSessionApiRoute } from 'iron-session/next';
import type { NextApiRequest, NextApiResponse } from 'next'
import client from "@/lib/mongodb";
const db = client.db(process.env.MONGODB_DB);
export default withIronSessionApiRoute(handler, sessionOptions);
async function handler(req: NextApiRequest, res: NextApiResponse) {
const { op } = req.query
if (req.method === 'GET') {
switch (op) {
default:
res.status(400).json({ error: 'Invalid operation!' })
}
}
else if (req.method === 'POST') {
switch (op) {
case 'crossRefEmails':
res.status(200).json(await crossRefEmails(req.body.emails))
break;
default:
res.status(400).json({ error: 'Invalid operation!' })
}
} else {
res.status(400).end(`Method ${req.method} Not Allowed`)
}
}
async function crossRefEmails(emails: string[]) {
return await db.collection("users").aggregate([
{
$match: {
email: { $in: emails }
}
},
{
$project: {
_id: 0,
email: 1
}
}
]).toArray();
}