diff --git a/api/src/auth.ts b/api/src/auth.ts index ababee0..706974f 100644 --- a/api/src/auth.ts +++ b/api/src/auth.ts @@ -20,6 +20,15 @@ export const defaultSdk = SpotifyApi.withClientCredentials( ); export const auth = betterAuth({ + user: { + additionalFields: { + lastSyncAt: { + type: "date", + required: false, + input: false, + }, + }, + }, database: drizzleAdapter(db, { provider: "pg", schema, diff --git a/api/src/db/auth-schema.ts b/api/src/db/auth-schema.ts index 708da79..a836233 100644 --- a/api/src/db/auth-schema.ts +++ b/api/src/db/auth-schema.ts @@ -12,6 +12,7 @@ export const user = pgTable("user", { .defaultNow() .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), + lastSyncAt: timestamp("last_sync_at"), }); export const session = pgTable( diff --git a/api/src/workflows/sync.ts b/api/src/workflows/sync.ts index f6ca7d2..913c84f 100644 --- a/api/src/workflows/sync.ts +++ b/api/src/workflows/sync.ts @@ -16,6 +16,7 @@ import { savedTrack, topArtist, topTrack, + user, } from "../db/schema"; import { upsertFollowedArtists, @@ -45,6 +46,7 @@ export class SpotifySyncWorkflow extends ConfiguredInstance { const data = await this.fetchSpotifyData(userId); console.log("Sync data fetched"); await this.persistSpotifyData(userId, data); + await this.updateSyncTime(userId); console.log("Synced"); return { ok: true }; } @@ -142,6 +144,14 @@ export class SpotifySyncWorkflow extends ConfiguredInstance { }); } + @DBOS.step() + private async updateSyncTime(userId: string) { + await db + .update(user) + .set({ lastSyncAt: new Date() }) + .where(eq(user.id, userId)); + } + @DBOS.step() private async fetchTopArtists( userId: string, diff --git a/web/src/lib/auth.serverfn.ts b/web/src/lib/auth.serverfn.ts index cdd15f6..505a2bb 100644 --- a/web/src/lib/auth.serverfn.ts +++ b/web/src/lib/auth.serverfn.ts @@ -1,15 +1,8 @@ import { createServerFn } from "@tanstack/react-start"; +import type { User } from "../../../api/src/party-types"; export interface AuthSession { - user: { - id: string; - createdAt: Date; - updatedAt: Date; - email: string; - emailVerified: boolean; - name: string; - image?: string | null | undefined; - }; + user: User; session: { id: string; createdAt: Date;