feat: add homeowner from dropdown — any user can add inline
This commit is contained in:
@@ -21,3 +21,30 @@ router.get('/', async (req, res) => {
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
// POST /api/homeowners — any authenticated user can add a homeowner
|
||||
router.post("/", async (req, res) => {
|
||||
try {
|
||||
const { name } = req.body;
|
||||
if (!name || typeof name !== "string" || name.trim().length < 2) {
|
||||
return res.status(400).json({ error: "Name must be at least 2 characters" });
|
||||
}
|
||||
const trimmed = name.trim();
|
||||
// Check for duplicate
|
||||
const existing = await req.prisma.homeowner.findFirst({
|
||||
where: { name: { equals: trimmed, mode: "insensitive" } },
|
||||
});
|
||||
if (existing) {
|
||||
// Return existing rather than error — convenient for the UI
|
||||
return res.json({ homeowner: existing, existing: true });
|
||||
}
|
||||
const homeowner = await req.prisma.homeowner.create({
|
||||
data: { name: trimmed },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
res.status(201).json({ homeowner });
|
||||
} catch (err) {
|
||||
console.error("Create homeowner error:", err);
|
||||
res.status(500).json({ error: "Failed to create homeowner" });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user