Skip to content

Commit c3c997b

Browse files
test: fix existing tests
1 parent bf7f456 commit c3c997b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Diff for: migrations/create-tables.js.sql

+7
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ CREATE TABLE IF NOT EXISTS gp_tokens (
4444
expire DATE,
4545
date_last_used DATE
4646
);
47+
48+
CREATE TABLE IF NOT EXISTS gp_credits (
49+
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
50+
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
51+
AMOUNT INT NOT NULL,
52+
user_id VARCHAR(36) NOT NULL
53+
);

Diff for: src/lib/credits.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export class Credits {
99

1010
async consume (userId: string, credits: number) {
1111
try {
12-
await this.sql(CREDITS_TABLE).where({ user_id: userId }).update({ amount: this.sql.raw('amount - ?', [ credits ]) });
12+
const result = await this.sql(CREDITS_TABLE).where({ user_id: userId }).update({ amount: this.sql.raw('amount - ?', [ credits ]) });
13+
14+
if (result === 1) {
15+
return true;
16+
}
1317
} catch (error) {
1418
if (error && (error as Error & {errno?: number}).errno === ER_CONSTRAINT_FAILED_CODE) {
1519
return false;
@@ -18,7 +22,7 @@ export class Credits {
1822
throw error;
1923
}
2024

21-
return true;
25+
return false;
2226
}
2327
}
2428

0 commit comments

Comments
 (0)