49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { desc, eq } from 'drizzle-orm';
|
|
import type { PortfolioInsight } from '@/lib/types';
|
|
import { db } from '@/lib/server/db';
|
|
import { portfolioInsight } from '@/lib/server/db/schema';
|
|
|
|
type InsightRow = typeof portfolioInsight.$inferSelect;
|
|
|
|
function toInsight(row: InsightRow): PortfolioInsight {
|
|
return {
|
|
id: row.id,
|
|
user_id: row.user_id,
|
|
provider: row.provider,
|
|
model: row.model,
|
|
content: row.content,
|
|
created_at: row.created_at
|
|
};
|
|
}
|
|
|
|
export async function createPortfolioInsight(input: {
|
|
userId: string;
|
|
provider: string;
|
|
model: string;
|
|
content: string;
|
|
}) {
|
|
const [created] = await db
|
|
.insert(portfolioInsight)
|
|
.values({
|
|
user_id: input.userId,
|
|
provider: input.provider,
|
|
model: input.model,
|
|
content: input.content,
|
|
created_at: new Date().toISOString()
|
|
})
|
|
.returning();
|
|
|
|
return toInsight(created);
|
|
}
|
|
|
|
export async function getLatestPortfolioInsight(userId: string) {
|
|
const [row] = await db
|
|
.select()
|
|
.from(portfolioInsight)
|
|
.where(eq(portfolioInsight.user_id, userId))
|
|
.orderBy(desc(portfolioInsight.created_at))
|
|
.limit(1);
|
|
|
|
return row ? toInsight(row) : null;
|
|
}
|