-
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.
Merge pull request #102 from casesandberg/feature/new-profile
New profile
- Loading branch information
Showing
34 changed files
with
611 additions
and
211 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,25 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { SchemaResponse } from '@play-money/api-helpers' | ||
import { getBalance, transformMarketBalancesToNumbers } from '@play-money/finance/lib/getBalances' | ||
import { getUserPrimaryAccount } from '@play-money/users/lib/getUserPrimaryAccount' | ||
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 { id } = schema.get.parameters.parse(params) | ||
const userAccount = await getUserPrimaryAccount({ userId: id }) | ||
const balance = await getBalance({ accountId: userAccount.id, assetType: 'CURRENCY', assetId: 'PRIMARY' }) | ||
|
||
return NextResponse.json({ | ||
balance: transformMarketBalancesToNumbers([balance])[0], | ||
}) | ||
} 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' | ||
|
||
export default createSchema({ | ||
get: { | ||
parameters: z.object({ id: z.string() }), | ||
responses: { | ||
200: z.object({ | ||
// TODO: Hookup with NetBalance | ||
balance: z.object({}), | ||
}), | ||
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
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
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
11 changes: 11 additions & 0 deletions
11
packages/database/migrations/20240916222328_fix_updated_at/migration.sql
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,11 @@ | ||
-- AlterTable | ||
ALTER TABLE "Account" ALTER COLUMN "updatedAt" DROP DEFAULT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Balance" ALTER COLUMN "updatedAt" DROP DEFAULT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "MarketOptionPosition" ALTER COLUMN "updatedAt" DROP DEFAULT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Transaction" ALTER COLUMN "updatedAt" DROP DEFAULT; |
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
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
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
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
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
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.