diff --git a/ebs/src/util/db.ts b/ebs/src/util/db.ts index 0bc1143..b33d828 100644 --- a/ebs/src/util/db.ts +++ b/ebs/src/util/db.ts @@ -39,7 +39,8 @@ export async function setupDb() { await db.query(` CREATE TABLE IF NOT EXISTS prepurchases ( token VARCHAR(255) PRIMARY KEY, - cart JSON NOT NULL + cart JSON NOT NULL, + userId VARCHAR(255) NOT NULL ) `); @@ -75,7 +76,7 @@ export async function addFulfilledTransaction(receipt: string, token: string, us export async function registerPrepurchase(cart: IdentifiableCart): Promise { try { const token = uuid(); - await db.query("INSERT INTO prepurchases (token, cart) VALUES (?, ?)", [token, JSON.stringify(cart)]); + await db.query("INSERT INTO prepurchases (token, cart, userId) VALUES (?, ?, ?)", [token, JSON.stringify(cart), cart.userId]); return token; } catch (e: any) { console.error("Database query failed (registerPrepurchase)"); diff --git a/logger/src/util/db.ts b/logger/src/util/db.ts index 2c130c7..6fe45c1 100644 --- a/logger/src/util/db.ts +++ b/logger/src/util/db.ts @@ -21,8 +21,13 @@ export async function canLog(token: string | null): Promise { export async function getUserIdFromTransactionToken(token: string): Promise { try { - const [rows] = (await db.query("SELECT userId FROM transactions WHERE token = ?", [token])) as [RowDataPacket[], any]; - return rows[0].userId; + try { + const [rows] = (await db.query("SELECT userId FROM prepurchases WHERE token = ?", [token])) as [RowDataPacket[], any]; + return rows[0].userId; + } catch (e: any) { + const [rows] = (await db.query("SELECT userId FROM transactions WHERE token = ?", [token])) as [RowDataPacket[], any]; + return rows[0].userId; + } } catch (e: any) { console.error("Database query failed (getUserIdFromTransactionToken)"); console.error(e);