-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Recent Positions and Questions to User Profile
- Loading branch information
1 parent
2ffd2ff
commit d2c017a
Showing
7 changed files
with
316 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { SchemaResponse } from '@play-money/api-helpers' | ||
import { getPositions } from '@play-money/finance/lib/getPositions' | ||
import { getUserById } from '@play-money/users/lib/getUserById' | ||
import schema from './schema' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export async function GET( | ||
req: Request, | ||
{ params }: { params: unknown } | ||
): Promise<SchemaResponse<typeof schema.GET.responses>> { | ||
try { | ||
const url = new URL(req.url) | ||
const searchParams = new URLSearchParams(url.search) | ||
const urlParams = Object.fromEntries(searchParams) | ||
|
||
const { id, pageSize } = schema.GET.parameters.parse({ ...(params || {}), ...urlParams }) | ||
|
||
const user = await getUserById({ id }) | ||
|
||
const { positions } = await getPositions( | ||
{ | ||
accountId: user.primaryAccountId, | ||
}, | ||
{ field: 'updatedAt', direction: 'desc' }, | ||
{ take: pageSize ?? 25, skip: 0 } | ||
) | ||
|
||
return NextResponse.json({ | ||
positions, | ||
}) | ||
} catch (error) { | ||
console.log(error) // eslint-disable-line no-console -- Log error for debugging | ||
|
||
return NextResponse.json({ error: 'Error processing request' }, { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from 'zod' | ||
import { ServerErrorSchema, createSchema } from '@play-money/api-helpers' | ||
import { MarketOptionPositionSchema, UserSchema } from '@play-money/database' | ||
|
||
export default createSchema({ | ||
GET: { | ||
parameters: UserSchema.pick({ id: true }).extend({ pageSize: z.coerce.number().optional() }), | ||
responses: { | ||
200: z.object({ | ||
positions: z.array(MarketOptionPositionSchema), | ||
}), | ||
404: ServerErrorSchema, | ||
500: ServerErrorSchema, | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import db from '@play-money/database' | ||
import { ExtendedMarketOptionPosition } from '../types' | ||
|
||
interface PositionsFilterOptions { | ||
accountId?: string | ||
} | ||
|
||
interface SortOptions { | ||
field: string | ||
direction: 'asc' | 'desc' | ||
} | ||
|
||
interface PaginationOptions { | ||
skip: number | ||
take: number | ||
} | ||
|
||
export async function getPositions( | ||
filters: PositionsFilterOptions = {}, | ||
sort: SortOptions = { field: 'createdAt', direction: 'desc' }, | ||
pagination: PaginationOptions = { skip: 0, take: 10 } | ||
): Promise<{ positions: Array<ExtendedMarketOptionPosition>; total: number }> { | ||
const [positions, total] = await Promise.all([ | ||
db.marketOptionPosition.findMany({ | ||
where: { | ||
accountId: filters.accountId, | ||
value: { gt: 0 }, | ||
}, | ||
include: { | ||
market: true, | ||
option: true, | ||
}, | ||
orderBy: { | ||
[sort.field]: sort.direction, | ||
}, | ||
skip: pagination.skip, | ||
take: pagination.take, | ||
}), | ||
db.marketOptionPosition.count({ | ||
where: { | ||
accountId: filters.accountId, | ||
value: { gt: 0 }, | ||
}, | ||
}), | ||
]) | ||
|
||
return { positions, total } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.