diff --git a/app/features/user-page/UserRepository.server.ts b/app/features/user-page/UserRepository.server.ts index b73294c31..a5d27bd93 100644 --- a/app/features/user-page/UserRepository.server.ts +++ b/app/features/user-page/UserRepository.server.ts @@ -722,11 +722,10 @@ export function upsert( ) { return db .insertInto("User") - .values(args) + .values({ ...args, createdAt: databaseTimestampNow() }) .onConflict((oc) => { return oc.column("discordId").doUpdateSet({ ...R.omit(args, ["discordId"]), - createdAt: databaseTimestampNow(), }); }) .returning("id") diff --git a/app/features/user-page/UserRepository.test.ts b/app/features/user-page/UserRepository.test.ts new file mode 100644 index 000000000..1ba385215 --- /dev/null +++ b/app/features/user-page/UserRepository.test.ts @@ -0,0 +1,63 @@ +import { afterEach, describe, expect, test } from "vitest"; +import { dbReset } from "~/utils/Test"; +import * as UserRepository from "./UserRepository.server"; + +describe("UserRepository", () => { + afterEach(() => { + dbReset(); + }); + + test("created user has createdAt field", async () => { + await UserRepository.upsert({ + discordId: "1", + discordName: "TestUser", + discordAvatar: null, + }); + + const user = await UserRepository.findModInfoById(1); + + expect(user).toBeDefined(); + expect(user?.createdAt).toBeDefined(); + }); + + test("updates user name when upserting", async () => { + await UserRepository.upsert({ + discordId: "1", + discordName: "TestUser", + discordAvatar: null, + }); + + const user = await UserRepository.findLayoutDataByIdentifier("1"); + + expect(user?.username).toBe("TestUser"); + + await UserRepository.upsert({ + discordId: "1", + discordName: "UpdatedUser", + discordAvatar: null, + }); + + const updatedUser = await UserRepository.findLayoutDataByIdentifier("1"); + expect(updatedUser?.username).toBe("UpdatedUser"); + }); + + test("updating a user doesn't change the createdAt field", async () => { + await UserRepository.upsert({ + discordId: "1", + discordName: "TestUser", + discordAvatar: null, + }); + + const user = await UserRepository.findModInfoById(1); + const createdAt = user?.createdAt; + + await UserRepository.upsert({ + discordId: "1", + discordName: "UpdatedUser", + discordAvatar: null, + }); + + const updatedUser = await UserRepository.findModInfoById(1); + expect(updatedUser?.createdAt).toEqual(createdAt); + }); +}); diff --git a/migrations/092-user-created-at-null.js b/migrations/092-user-created-at-null.js new file mode 100644 index 000000000..4d45b84a5 --- /dev/null +++ b/migrations/092-user-created-at-null.js @@ -0,0 +1,6 @@ +export function up(db) { + db.transaction(() => { + // there was a bug in the past where createdAt was set when account was updated instead + db.prepare(/* sql */ `update "User" set "createdAt" = null`).run(); + })(); +}