init
This commit is contained in:
@@ -40,10 +40,12 @@ export type RunMetrics = {
|
||||
cadenceSpm?: number[];
|
||||
gctMs?: number[];
|
||||
gcbLeftPct?: number[];
|
||||
paceSec?: number[];
|
||||
};
|
||||
|
||||
export type RunningActivity = RunningActivityInput & {
|
||||
_id: ObjectId;
|
||||
userId: string;
|
||||
createdAt: Date;
|
||||
routePoints?: RoutePoint[];
|
||||
elevationProfile?: number[];
|
||||
@@ -56,60 +58,73 @@ const SYNC_STATE_COLLECTION = "sync_state";
|
||||
async function getCollection() {
|
||||
const db = await getDb();
|
||||
const collection = db.collection<RunningActivity>(COLLECTION);
|
||||
await collection.createIndex({ garminActivityId: 1 }, { unique: true });
|
||||
await collection.createIndex({ userId: 1, garminActivityId: 1 }, { unique: true });
|
||||
return collection;
|
||||
}
|
||||
|
||||
export async function upsertRunningActivity(activity: RunningActivityInput): Promise<void> {
|
||||
export async function upsertRunningActivity(
|
||||
userId: string,
|
||||
activity: RunningActivityInput
|
||||
): Promise<void> {
|
||||
const collection = await getCollection();
|
||||
await collection.updateOne(
|
||||
{ garminActivityId: activity.garminActivityId },
|
||||
{ userId, garminActivityId: activity.garminActivityId },
|
||||
{
|
||||
$set: activity,
|
||||
$set: { ...activity, userId },
|
||||
$setOnInsert: { createdAt: new Date() },
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
|
||||
export async function listRunningActivities(): Promise<RunningActivity[]> {
|
||||
export async function listRunningActivities(userId: string): Promise<RunningActivity[]> {
|
||||
const collection = await getCollection();
|
||||
return collection.find().sort({ startTime: -1 }).toArray();
|
||||
return collection.find({ userId }).sort({ startTime: -1 }).toArray();
|
||||
}
|
||||
|
||||
export async function getRunningActivity(id: string): Promise<RunningActivity | null> {
|
||||
export async function getRunningActivity(
|
||||
userId: string,
|
||||
id: string
|
||||
): Promise<RunningActivity | null> {
|
||||
const collection = await getCollection();
|
||||
return collection.findOne({ _id: new ObjectId(id) });
|
||||
return collection.findOne({ _id: new ObjectId(id), userId });
|
||||
}
|
||||
|
||||
export async function setRunningActivityMetrics(
|
||||
userId: string,
|
||||
garminActivityId: number,
|
||||
metrics: RunMetrics
|
||||
): Promise<void> {
|
||||
const collection = await getCollection();
|
||||
await collection.updateOne({ garminActivityId }, { $set: { runMetrics: metrics } });
|
||||
await collection.updateOne({ userId, garminActivityId }, { $set: { runMetrics: metrics } });
|
||||
}
|
||||
|
||||
export async function setRunningActivityRoutePoints(
|
||||
userId: string,
|
||||
garminActivityId: number,
|
||||
points: RoutePoint[],
|
||||
elevationProfile: number[]
|
||||
): Promise<void> {
|
||||
const collection = await getCollection();
|
||||
await collection.updateOne({ garminActivityId }, { $set: { routePoints: points, elevationProfile } });
|
||||
await collection.updateOne(
|
||||
{ userId, garminActivityId },
|
||||
{ $set: { routePoints: points, elevationProfile } }
|
||||
);
|
||||
}
|
||||
|
||||
type SyncState = { _id: "garmin"; lastSyncAt: Date };
|
||||
type SyncState = { _id: string; lastSyncAt: Date };
|
||||
|
||||
export async function getLastSyncAt(): Promise<Date | null> {
|
||||
export async function getLastSyncAt(userId: string): Promise<Date | null> {
|
||||
const db = await getDb();
|
||||
const state = await db.collection<SyncState>(SYNC_STATE_COLLECTION).findOne({ _id: "garmin" });
|
||||
const state = await db
|
||||
.collection<SyncState>(SYNC_STATE_COLLECTION)
|
||||
.findOne({ _id: userId });
|
||||
return state?.lastSyncAt ?? null;
|
||||
}
|
||||
|
||||
export async function setLastSyncAt(date: Date): Promise<void> {
|
||||
export async function setLastSyncAt(userId: string, date: Date): Promise<void> {
|
||||
const db = await getDb();
|
||||
await db
|
||||
.collection<SyncState>(SYNC_STATE_COLLECTION)
|
||||
.updateOne({ _id: "garmin" }, { $set: { lastSyncAt: date } }, { upsert: true });
|
||||
.updateOne({ _id: userId }, { $set: { lastSyncAt: date } }, { upsert: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user