import { postgresConnection } from "db";
import type { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  try {
    if (req.method === "POST") {
      const { wallet } = req.body;
      const db = postgresConnection;
      const result = await db.query("SELECT id FROM users WHERE wallet = $1", [wallet]);
      if (result.rowCount > 0) {
        return res.status(200).json({ userId: result.rows[0].id});
      } else {
        return res.status(404).json({ message: "User not found" });
      }
    }
  } catch (error) {
    res.status(500).json(error);
  }
}
