diff --git a/server/src/routes/chat.ts b/server/src/routes/chat.ts index 7485336..addc22c 100644 --- a/server/src/routes/chat.ts +++ b/server/src/routes/chat.ts @@ -61,16 +61,27 @@ router.get('/conversations/:id/messages', authenticate, async (req, res, next) = router.post('/conversations', authenticate, async (req, res, next) => { try { const { recipientId, listingId, message } = req.body; + if (!recipientId) throw new AppError(400, 'Recipient ID is required'); if (recipientId === req.userId) throw new AppError(400, 'Cannot message yourself'); const [id1, id2] = [req.userId!, recipientId].sort(); + const listingIdValue = listingId || undefined; + let conversation = await prisma.conversation.findFirst({ - where: { user1Id: id1, user2Id: id2, listingId: listingId || null }, + where: { + user1Id: id1, + user2Id: id2, + ...(listingIdValue ? { listingId: listingIdValue } : { listingId: null }), + }, }); if (!conversation) { conversation = await prisma.conversation.create({ - data: { user1Id: id1, user2Id: id2, listingId: listingId || null }, + data: { + user1Id: id1, + user2Id: id2, + ...(listingIdValue ? { listingId: listingIdValue } : {}), + }, }); } diff --git a/server/src/routes/listing.ts b/server/src/routes/listing.ts index 2f9e870..f97b519 100644 --- a/server/src/routes/listing.ts +++ b/server/src/routes/listing.ts @@ -119,6 +119,25 @@ router.put('/:id', authenticate, validate(updateListingSchema), async (req, res, } }); +// Activate listing (bypasses Stripe in dev, requires payment in prod) +router.post('/:id/activate', authenticate, async (req, res, next) => { + try { + const existing = await prisma.listing.findUnique({ where: { id: req.params.id } }); + if (!existing) throw new AppError(404, 'Listing not found'); + if (existing.sellerId !== req.userId) throw new AppError(403, 'Not authorized'); + if (existing.status !== 'DRAFT') throw new AppError(400, 'Listing is not in draft status'); + + const listing = await prisma.listing.update({ + where: { id: req.params.id }, + data: { status: 'ACTIVE' }, + select: listingSelect, + }); + res.json(listing); + } catch (error) { + next(error); + } +}); + router.delete('/:id', authenticate, async (req, res, next) => { try { const existing = await prisma.listing.findUnique({ where: { id: req.params.id } });