52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { config } from '../config/index.js';
|
|
import logger from '../utils/logger.js';
|
|
import { ORDER_CONTAINER, queryDocuments } from './tradingRecordStore.js';
|
|
|
|
type OrderActivityDocument = {
|
|
id?: string;
|
|
order_id?: string | null;
|
|
user_id?: string | null;
|
|
profile_id?: string | null;
|
|
symbol?: string | null;
|
|
type?: string | null;
|
|
side?: string | null;
|
|
qty?: number | string | null;
|
|
quantity?: number | string | null;
|
|
price?: number | string | null;
|
|
status?: string | null;
|
|
timestamp?: number | string | null;
|
|
filled_at?: string | null;
|
|
created_at?: string | null;
|
|
trade_id?: string | null;
|
|
action?: string | null;
|
|
source?: string | null;
|
|
stop_loss?: number | string | null;
|
|
take_profit?: number | string | null;
|
|
sub_tag?: string | null;
|
|
};
|
|
|
|
export async function listRecentOrders(options: {
|
|
userId?: string;
|
|
limit?: number;
|
|
}): Promise<any[]> {
|
|
try {
|
|
const filters = ['c.productId = @productId', 'c.type = @type'];
|
|
const parameters: Array<{ name: string; value: unknown }> = [
|
|
{ name: '@productId', value: config.PRODUCT_ID },
|
|
{ name: '@type', value: 'trade_order' },
|
|
];
|
|
|
|
if (options.userId) {
|
|
filters.push('c.user_id = @userId');
|
|
parameters.push({ name: '@userId', value: options.userId });
|
|
}
|
|
|
|
const limit = Math.max(1, Math.min(5000, Math.floor(Number(options.limit || 5000))));
|
|
const query = `SELECT TOP ${limit} * FROM c WHERE ${filters.join(' AND ')} ORDER BY c.created_at DESC`;
|
|
return await queryDocuments<OrderActivityDocument>(ORDER_CONTAINER, query, parameters);
|
|
} catch (error: any) {
|
|
logger.error(`[OrderActivityRepo] Recent order lookup failed: ${error.message}`);
|
|
return [];
|
|
}
|
|
}
|