Fix user createdAt being updatedAt instead

This commit is contained in:
Kalle 2025-06-24 15:26:20 +03:00
parent cd9a98de7c
commit 02e5d8fb46
3 changed files with 70 additions and 2 deletions

View File

@ -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")

View File

@ -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);
});
});

View File

@ -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();
})();
}