first commit
This commit is contained in:
40
src/repositories/recommendationRepo.ts
Normal file
40
src/repositories/recommendationRepo.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { db, type RecommendationSnapshotRow } from "@/src/db/appDb";
|
||||
import type {
|
||||
RecommendationSegment,
|
||||
RecommendationSnapshot,
|
||||
} from "@/src/recommendations/types";
|
||||
|
||||
const DEFAULT_USER_ID = "anon";
|
||||
|
||||
function key(userId: string, date: string, segment: RecommendationSegment) {
|
||||
return `${userId}:${date}:${segment}`;
|
||||
}
|
||||
|
||||
export async function getRecommendationSnapshot(
|
||||
date: string,
|
||||
segment: RecommendationSegment,
|
||||
userId = DEFAULT_USER_ID,
|
||||
) {
|
||||
return db.recommendationSnapshots.get(key(userId, date, segment));
|
||||
}
|
||||
|
||||
export async function upsertRecommendationSnapshot(
|
||||
snapshot: RecommendationSnapshot,
|
||||
userId = DEFAULT_USER_ID,
|
||||
) {
|
||||
const row: RecommendationSnapshotRow = {
|
||||
id: key(userId, snapshot.date, snapshot.segment),
|
||||
userId,
|
||||
date: snapshot.date,
|
||||
segment: snapshot.segment,
|
||||
generatedAt: snapshot.generatedAt,
|
||||
screeningVersion: snapshot.screeningVersion,
|
||||
data: snapshot,
|
||||
};
|
||||
await db.recommendationSnapshots.put(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
export function rowToSnapshot(row: RecommendationSnapshotRow): RecommendationSnapshot {
|
||||
return row.data as RecommendationSnapshot;
|
||||
}
|
||||
Reference in New Issue
Block a user