이미 클라이언트가 로그인된 상태에서 세션을 업그레이드하려는 경우 발생하던 500 오류 수정

This commit is contained in:
static
2025-07-08 19:38:49 +09:00
parent 983cb2cc57
commit 6e14b45656
3 changed files with 35 additions and 40 deletions

View File

@@ -5,31 +5,22 @@ import db from "./kysely";
export const createSession = async (
userId: number,
clientId: number | null,
sessionId: string,
ip: string | null,
agent: string | null,
) => {
try {
const now = new Date();
await db
.insertInto("session")
.values({
id: sessionId,
user_id: userId,
client_id: clientId,
created_at: now,
last_used_at: now,
last_used_by_ip: ip || null,
last_used_by_agent: agent || null,
})
.execute();
} catch (e) {
if (e instanceof pg.DatabaseError && e.code === "23505") {
throw new IntegrityError("Session already exists");
}
throw e;
}
const now = new Date();
await db
.insertInto("session")
.values({
id: sessionId,
user_id: userId,
created_at: now,
last_used_at: now,
last_used_by_ip: ip || null,
last_used_by_agent: agent || null,
})
.execute();
};
export const refreshSession = async (
@@ -56,14 +47,21 @@ export const refreshSession = async (
};
export const upgradeSession = async (sessionId: string, clientId: number) => {
const res = await db
.updateTable("session")
.set({ client_id: clientId })
.where("id", "=", sessionId)
.where("client_id", "is", null)
.executeTakeFirst();
if (res.numUpdatedRows === 0n) {
throw new IntegrityError("Session not found");
try {
const res = await db
.updateTable("session")
.set({ client_id: clientId })
.where("id", "=", sessionId)
.where("client_id", "is", null)
.executeTakeFirst();
if (res.numUpdatedRows === 0n) {
throw new IntegrityError("Session not found");
}
} catch (e) {
if (e instanceof pg.DatabaseError && e.code === "23505") {
throw new IntegrityError("Session already exists");
}
throw e;
}
};