Make it so this endpoint can be used as profile one
This commit is contained in:
@@ -1231,7 +1231,9 @@ app.get('/berrydash/account', context => berryDashAccountGetHandler(context), {
|
|||||||
tags: ['Berry Dash', 'Accounts']
|
tags: ['Berry Dash', 'Accounts']
|
||||||
},
|
},
|
||||||
query: t.Object({
|
query: t.Object({
|
||||||
username: t.String()
|
username: t.Optional(t.String()),
|
||||||
|
id: t.Optional(t.String()),
|
||||||
|
exact: t.Optional(t.String())
|
||||||
}),
|
}),
|
||||||
headers: t.Object({
|
headers: t.Object({
|
||||||
'x-forwarded-for': t.Optional(
|
'x-forwarded-for': t.Optional(
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
} from '../../../lib/util'
|
} from '../../../lib/util'
|
||||||
import { berryDashUserData, users } from '../../../lib/tables'
|
import { berryDashUserData, users } from '../../../lib/tables'
|
||||||
import { eq, sql } from 'drizzle-orm'
|
import { eq, sql } from 'drizzle-orm'
|
||||||
|
import { isDataURI } from 'validator'
|
||||||
|
|
||||||
export const handler = async (context: Context) => {
|
export const handler = async (context: Context) => {
|
||||||
const dbInfo0 = getDatabaseConnection(0)
|
const dbInfo0 = getDatabaseConnection(0)
|
||||||
@@ -20,6 +21,21 @@ export const handler = async (context: Context) => {
|
|||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
const usernameToSearch = context.query.username
|
const usernameToSearch = context.query.username
|
||||||
|
const idToSearch = Number(context.query.id ?? '0')
|
||||||
|
const exactSearch = context.query.exact ?? ''.toLowerCase() == 'true'
|
||||||
|
if (!usernameToSearch && !(idToSearch > 0 && exactSearch)) {
|
||||||
|
connection0.end()
|
||||||
|
connection1.end()
|
||||||
|
return jsonResponse(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message:
|
||||||
|
'Either `username`, or `id` and `exact=true` params are required',
|
||||||
|
data: null
|
||||||
|
},
|
||||||
|
400
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const userRows = await db0
|
const userRows = await db0
|
||||||
.select({
|
.select({
|
||||||
@@ -29,10 +45,13 @@ export const handler = async (context: Context) => {
|
|||||||
})
|
})
|
||||||
.from(users)
|
.from(users)
|
||||||
.where(
|
.where(
|
||||||
sql`LOWER(${
|
idToSearch > 0 && exactSearch
|
||||||
users.username
|
? eq(users.id, idToSearch)
|
||||||
}) LIKE ${`%${usernameToSearch.toLowerCase()}%`}`
|
: sql`LOWER(${
|
||||||
|
users.username
|
||||||
|
}) LIKE ${`%${usernameToSearch.toLowerCase()}%`}`
|
||||||
)
|
)
|
||||||
|
.limit(idToSearch > 0 && exactSearch ? 1 : 100)
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
const result = await Promise.all(
|
const result = await Promise.all(
|
||||||
@@ -76,6 +95,9 @@ export const handler = async (context: Context) => {
|
|||||||
totalAntiBerries: BigInt(
|
totalAntiBerries: BigInt(
|
||||||
savedata?.gameStore?.totalAntiBerries ?? 0
|
savedata?.gameStore?.totalAntiBerries ?? 0
|
||||||
).toString(),
|
).toString(),
|
||||||
|
totalGoldenBerries: BigInt(
|
||||||
|
savedata?.gameStore?.totalGoldenBerries ?? 0
|
||||||
|
).toString(),
|
||||||
coins: BigInt(savedata?.bird?.customIcon?.balance ?? 0).toString()
|
coins: BigInt(savedata?.bird?.customIcon?.balance ?? 0).toString()
|
||||||
}
|
}
|
||||||
return row
|
return row
|
||||||
@@ -85,5 +107,12 @@ export const handler = async (context: Context) => {
|
|||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|
||||||
return jsonResponse({ success: true, message: null, data: result }, 200)
|
return jsonResponse(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: null,
|
||||||
|
data: idToSearch > 0 && exactSearch ? result[0] : result
|
||||||
|
},
|
||||||
|
200
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user