-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/trpc/src/routers/private/smart-trade/get-infinite-smart-trades/handler.ts
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,41 @@ | ||
import { xprisma, toSmartTradeEntity } from "@opentrader/db"; | ||
import type { TGetInfiniteSmartTradesSchema } from "./schema.js"; | ||
import type { Context } from "../../../../utils/context.js"; | ||
|
||
type Options = { | ||
ctx: { | ||
user: NonNullable<Context["user"]>; | ||
}; | ||
input: TGetInfiniteSmartTradesSchema; | ||
}; | ||
|
||
export async function getInfiniteSmartTrades({ ctx, input }: Options) { | ||
const { cursor } = input; | ||
const limit = input.limit ?? 50; | ||
const items = await xprisma.smartTrade.findMany({ | ||
take: limit + 1, // get an extra item at the end which we'll use as next cursor | ||
cursor: cursor ? { id: cursor } : undefined, | ||
orderBy: { | ||
updatedAt: "desc", | ||
}, | ||
where: { | ||
owner: { id: ctx.user.id }, | ||
bot: { id: input.botId }, | ||
}, | ||
include: { | ||
orders: true, | ||
exchangeAccount: true, | ||
}, | ||
}); | ||
|
||
let nextCursor: typeof cursor | undefined = undefined; | ||
if (items.length > limit) { | ||
const nextItem = items.pop(); | ||
nextCursor = nextItem!.id; | ||
} | ||
|
||
return { | ||
items: items.map((smartTrade) => toSmartTradeEntity(smartTrade)), | ||
nextCursor, | ||
}; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/trpc/src/routers/private/smart-trade/get-infinite-smart-trades/schema.ts
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,9 @@ | ||
import { z } from "zod"; | ||
|
||
export const ZGetInfiniteSmartTradesSchema = z.object({ | ||
botId: z.number().optional(), | ||
limit: z.number().min(1).max(100).nullish(), | ||
cursor: z.number().nullish(), // <-- "cursor" needs to exist, but can be any type | ||
}); | ||
|
||
export type TGetInfiniteSmartTradesSchema = z.infer<typeof ZGetInfiniteSmartTradesSchema>; |
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