Skip to content

Commit 76f76f4

Browse files
authored
Merge pull request #578 from WatWowMap/add-more-user-endpoints
Update user api
2 parents f310ffc + 5cc8337 commit 76f76f4

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

server/src/routes/api/v1/sessions.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ router.get('/hasValid/:id', async (req, res) => {
2727
req.headers['react-map-secret'] === api.reactMapSecret
2828
) {
2929
const results = await Session.query().whereRaw(
30-
`json_extract(data, '$.passport.user.id') = ${req.params.id}`,
30+
`json_extract(data, '$.passport.user.id') = ${req.params.id}
31+
OR json_extract(data, '$.passport.user.discordId') = "${req.params.id}"
32+
OR json_extract(data, '$.passport.user.telegramId') = "${req.params.id}"`,
3133
)
3234
res.status(200).json({
33-
valid: Boolean(results.length),
35+
valid: !!results.length,
3436
length: results.length,
3537
})
3638
console.log(`[API] api/v1/sessions/hasValid/${req.params.id}`)
@@ -50,7 +52,11 @@ router.get('/clearSessions/:id', async (req, res) => {
5052
req.headers['react-map-secret'] === api.reactMapSecret
5153
) {
5254
const results = await Session.query()
53-
.whereRaw(`json_extract(data, '$.passport.user.id') = ${req.params.id}`)
55+
.whereRaw(
56+
`json_extract(data, '$.passport.user.id') = ${req.params.id}
57+
OR json_extract(data, '$.passport.user.discordId') = "${req.params.id}"
58+
OR json_extract(data, '$.passport.user.telegramId') = "${req.params.id}"`,
59+
)
5460
.delete()
5561
res.status(200).json({ results })
5662
console.log(`[API] api/v1/sessions/clearSessions/${req.params.id}`)

server/src/routes/api/v1/users.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ router.get('/', async (req, res) => {
1616
console.log('[API] api/v1/users')
1717
} catch (e) {
1818
console.error('[API Error] api/v1/sessions', e)
19-
res.status(500).json({ status: 'ServerError', reason: e.message })
19+
res.status(500).json({ status: 'error', reason: e.message })
2020
}
2121
})
2222

@@ -26,14 +26,57 @@ router.get('/:id', async (req, res) => {
2626
api.reactMapSecret &&
2727
req.headers['react-map-secret'] === api.reactMapSecret
2828
) {
29-
res.status(200).json(await User.query().findById(req.params.id))
29+
const user = await User.query().findById(req.params.id)
30+
res
31+
.status(200)
32+
.json(user || { status: 'error', reason: 'User Not Found' })
3033
} else {
3134
throw new Error('Incorrect or missing API secret')
3235
}
3336
console.log(`[API] api/v1/users/${req.params.id}`)
3437
} catch (e) {
3538
console.error(`[API Error] api/v1/users/${req.params.id}`, e)
36-
res.status(500).json({ status: 'ServerError', reason: e.message })
39+
res.status(500).json({ status: 'error', reason: e.message })
40+
}
41+
})
42+
43+
router.get('/discord/:id', async (req, res) => {
44+
try {
45+
if (
46+
api.reactMapSecret &&
47+
req.headers['react-map-secret'] === api.reactMapSecret
48+
) {
49+
const user = await User.query().where('discordId', req.params.id).first()
50+
res
51+
.status(200)
52+
.json(user || { status: 'error', reason: 'User Not Found' })
53+
} else {
54+
throw new Error('Incorrect or missing API secret')
55+
}
56+
console.log(`[API] api/v1/users/discord/${req.params.id}`)
57+
} catch (e) {
58+
console.error(`[API Error] api/v1/users/discord/${req.params.id}`, e)
59+
res.status(500).json({ status: 'error', reason: e.message })
60+
}
61+
})
62+
63+
router.get('/telegram/:id', async (req, res) => {
64+
try {
65+
if (
66+
api.reactMapSecret &&
67+
req.headers['react-map-secret'] === api.reactMapSecret
68+
) {
69+
const user = await User.query().where('telegramId', req.params.id).first()
70+
res
71+
.status(200)
72+
.json(user || { status: 'error', reason: 'User Not Found' })
73+
} else {
74+
throw new Error('Incorrect or missing API secret')
75+
}
76+
console.log(`[API] api/v1/users/telegram/${req.params.id}`)
77+
} catch (e) {
78+
console.error(`[API Error] api/v1/users/telegram/${req.params.id}`, e)
79+
res.status(500).json({ status: 'error', reason: e.message })
3780
}
3881
})
3982

0 commit comments

Comments
 (0)