From ecc0d513072dcd83549c59155f6260bbd9336d98 Mon Sep 17 00:00:00 2001 From: MarketMadi Date: Wed, 20 Nov 2024 10:39:44 +0530 Subject: [PATCH 1/3] adding username to backend request --- server.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/server.ts b/server.ts index 5bbb97b..00c89ac 100755 --- a/server.ts +++ b/server.ts @@ -102,18 +102,13 @@ setInterval(async () => { app.post('/api/login', async (req, res) => { try { const { username, password } = req.body; - const user = await authenticateUser(username, password); - - // Check if the user's registration status is complete - if (user.status !== 'complete') { - return res.status(403).json({ message: 'Registration not complete. Please complete the registration process.' }); - } - const token = generateToken(user); - res.json({ token }); + res.json({ + token, + userId: user.id + }); } catch (error) { - // @ts-expect-error TS(2571): Object is of type 'unknown'. res.status(401).json({ message: 'Login failed', error: error.message }); } }); From 3d93423e1d25fd7f8806880d52d2cb841da4a8e9 Mon Sep 17 00:00:00 2001 From: MarketMadi Date: Wed, 20 Nov 2024 10:50:49 +0530 Subject: [PATCH 2/3] adding username to backend request v2 --- server.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server.ts b/server.ts index 00c89ac..f4031c2 100755 --- a/server.ts +++ b/server.ts @@ -1130,4 +1130,29 @@ app.get('/api/user-npub/:userId', authenticateJWT, async (req, res) => { console.error('Error fetching user npub:', error); res.status(500).json({ error: 'Failed to fetch user npub' }); } +}); + +app.get('/api/orders/:orderId/user-role', authenticateJWT, async (req, res) => { + const { orderId } = req.params; + const userId = req.user.id; + + try { + const order = await prisma.order.findUnique({ + where: { order_id: parseInt(orderId) } + }); + + if (!order) { + return res.status(404).json({ error: 'Order not found' }); + } + + const role = { + isMaker: order.customer_id === userId, + isTaker: order.taker_customer_id === userId, + userId: userId + }; + + res.json(role); + } catch (error) { + res.status(500).json({ error: 'Failed to determine user role' }); + } }); \ No newline at end of file From d7d78e0593f44078df34ddcb790c6a7d8cc61dd9 Mon Sep 17 00:00:00 2001 From: MarketMadi Date: Wed, 20 Nov 2024 10:59:36 +0530 Subject: [PATCH 3/3] adding username to backend request v3 --- server.ts | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/server.ts b/server.ts index f4031c2..10c8915 100755 --- a/server.ts +++ b/server.ts @@ -1145,14 +1145,43 @@ app.get('/api/orders/:orderId/user-role', authenticateJWT, async (req, res) => { return res.status(404).json({ error: 'Order not found' }); } - const role = { - isMaker: order.customer_id === userId, - isTaker: order.taker_customer_id === userId, - userId: userId - }; + // If taker_customer_id is null and user is the customer (maker), they're the maker + const isMaker = parseInt(order.customer_id) === parseInt(userId); + + // Only check taker if there is a taker_customer_id + const isTaker = order.taker_customer_id !== null && + parseInt(order.taker_customer_id) === parseInt(userId); + + console.log('Role Check:', { + orderId: parseInt(orderId), + userId: parseInt(userId), + customer_id: order.customer_id, + taker_customer_id: order.taker_customer_id, + isMaker, + isTaker + }); + + // If there's no taker and this user is the customer, they must be the maker + if (order.taker_customer_id === null && isMaker) { + return res.json({ + isMaker: true, + isTaker: false, + userId: parseInt(userId), + orderId: parseInt(orderId) + }); + } - res.json(role); + res.json({ + isMaker, + isTaker, + userId: parseInt(userId), + orderId: parseInt(orderId) + }); } catch (error) { - res.status(500).json({ error: 'Failed to determine user role' }); + console.error('Role determination error:', error); + res.status(500).json({ + error: 'Failed to determine user role', + details: error.message + }); } }); \ No newline at end of file