Skip to content

Commit ff43083

Browse files
committed
fix: resolve frontend/backend env validation and DB schema fallback for time control columns
1 parent 145304e commit ff43083

7 files changed

Lines changed: 47 additions & 17 deletions

File tree

backend/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ dist
1212
dist-ssr
1313
*.local
1414
.env
15-
database/
1615

1716
# Editor directories and files
1817
.vscode/*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Migration: Add time_control_preset, time_increment_seconds, and undo_request columns to games table
2+
ALTER TABLE games ADD COLUMN IF NOT EXISTS time_control_preset VARCHAR(20);
3+
ALTER TABLE games ADD COLUMN IF NOT EXISTS time_increment_seconds INTEGER DEFAULT 0;
4+
ALTER TABLE games ADD COLUMN IF NOT EXISTS undo_request VARCHAR(10);
5+
ALTER TABLE games ADD COLUMN IF NOT EXISTS undo_request_at TIMESTAMP WITH TIME ZONE;
6+
7+
-- DOWN
8+
ALTER TABLE games DROP COLUMN IF EXISTS time_control_preset;
9+
ALTER TABLE games DROP COLUMN IF EXISTS time_increment_seconds;
10+
ALTER TABLE games DROP COLUMN IF EXISTS undo_request;
11+
ALTER TABLE games DROP COLUMN IF EXISTS undo_request_at;

backend/models/gameModel.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,38 @@ class GameModel {
2121
if (!gameCode) gameCode = this.generateGameCode();
2222
const initialBoard = chessEngine.initBoard();
2323

24-
const { data, error } = await supabase
24+
const insertPayload = {
25+
game_code: gameCode,
26+
game_type: gameType,
27+
board_state: initialBoard,
28+
current_turn: "white",
29+
status: "waiting",
30+
wager_amount: wagerAmount,
31+
player_white_address: playerWhiteAddress,
32+
escrow_status: wagerAmount ? "pending" : null,
33+
time_control_seconds: timeControlSeconds,
34+
time_control_preset: timeControlPreset,
35+
time_increment_seconds: timeIncrementSeconds || 0,
36+
};
37+
38+
let { data, error } = await supabase
2539
.from("games")
26-
.insert({
27-
game_code: gameCode,
28-
game_type: gameType,
29-
board_state: initialBoard,
30-
current_turn: "white",
31-
status: "waiting",
32-
wager_amount: wagerAmount,
33-
player_white_address: playerWhiteAddress,
34-
escrow_status: wagerAmount ? "pending" : null,
35-
time_control_seconds: timeControlSeconds,
36-
time_control_preset: timeControlPreset,
37-
time_increment_seconds: timeIncrementSeconds || 0,
38-
})
40+
.insert(insertPayload)
3941
.select()
4042
.single();
4143

44+
if (error && error.message && (error.message.includes("time_control_preset") || error.message.includes("time_increment_seconds"))) {
45+
delete insertPayload.time_control_preset;
46+
delete insertPayload.time_increment_seconds;
47+
const retry = await supabase
48+
.from("games")
49+
.insert(insertPayload)
50+
.select()
51+
.single();
52+
data = retry.data;
53+
error = retry.error;
54+
}
55+
4256
if (error) throw error;
4357
return data;
4458
}

backend/tests/envValidator.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("Backend Environment Validator (envValidator)", () => {
3131
it("throws error when SUPABASE_KEY is missing", () => {
3232
process.env.SUPABASE_URL = "https://example.supabase.co";
3333
delete process.env.SUPABASE_KEY;
34+
delete process.env.SUPABASE_ANON_KEY;
3435
process.env.SOROBAN_RPC_URL = "https://soroban-testnet.stellar.org";
3536

3637
expect(() => validateEnv({ skipExit: true })).toThrow(/SUPABASE_KEY/);
@@ -40,6 +41,7 @@ describe("Backend Environment Validator (envValidator)", () => {
4041
process.env.SUPABASE_URL = "https://example.supabase.co";
4142
process.env.SUPABASE_KEY = "test-key-123";
4243
delete process.env.SOROBAN_RPC_URL;
44+
delete process.env.STELLAR_RPC_URL;
4345

4446
expect(() => validateEnv({ skipExit: true })).toThrow(/SOROBAN_RPC_URL/);
4547
});

frontend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
VITE_CONTRACT_ID=your_contract_address
12
VITE_ESCROW_CONTRACT_ADDRESS=your_contract_address
23
VITE_BACKEND_URL=http://localhost:3000/
34
VITE_STELLAR_RPC_URL=https://soroban-testnet.stellar.org

frontend/src/services/stellarService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { signTransaction } from "@stellar/freighter-api";
33

44
const RPC_URL = import.meta.env.VITE_STELLAR_RPC_URL || "https://soroban-testnet.stellar.org";
55
const NETWORK_PASSPHRASE = import.meta.env.VITE_STELLAR_NETWORK_PASSPHRASE || Networks.TESTNET;
6-
const ESCROW_ADDRESS = import.meta.env.VITE_ESCROW_CONTRACT_ADDRESS || "";
6+
const ESCROW_ADDRESS = import.meta.env.VITE_ESCROW_CONTRACT_ADDRESS || import.meta.env.VITE_CONTRACT_ID || "";
77

88
const server = new rpc.Server(RPC_URL);
99

frontend/src/utils/envValidator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function validateFrontendEnv(): boolean {
2323
const missing: RequiredFrontendEnv[] = [];
2424

2525
for (const envVar of REQUIRED_FRONTEND_VARS) {
26-
const value = import.meta.env[envVar.key];
26+
let value = import.meta.env[envVar.key];
27+
if (!value && envVar.key === "VITE_CONTRACT_ID") {
28+
value = import.meta.env.VITE_ESCROW_CONTRACT_ADDRESS;
29+
}
2730
if (!value || String(value).trim() === "") {
2831
missing.push(envVar);
2932
}

0 commit comments

Comments
 (0)