Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit cb0f9c5

Browse files
authored
fix(currency): Fix tests + make errors more consistent (#58)
2 parents 84e2926 + 596c668 commit cb0f9c5

File tree

8 files changed

+14
-20
lines changed

8 files changed

+14
-20
lines changed

modules/currency/module.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ scripts:
1313
errors:
1414
INVALID_USER: {}
1515
INVALID_AMOUNT: {}
16-
INVALID_BALANCE: {}
1716
NOT_ENOUGH_FUNDS: {}
1817
dependencies:
1918
rate_limit: {}

modules/currency/scripts/deposit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export async function run(
1616
ctx: ScriptContext,
1717
req: Request,
1818
): Promise<Response> {
19-
await ctx.call("rate_limit", "throttle", { requests: 25 });
19+
await ctx.modules.rateLimit.throttle({ requests: 25 });
2020

21-
if (req.amount < 0) throw new RuntimeError("INVALID_AMOUNT");
21+
if (req.amount < 0 || !Number.isFinite(req.amount)) throw new RuntimeError("INVALID_AMOUNT");
2222

2323
return ctx.db.$transaction(async (tx) => {
2424
const balance = await getBalance(tx, req.userId);
@@ -30,7 +30,7 @@ export async function run(
3030
try {
3131
await setBalance(tx, req.userId, updatedBalance);
3232
} catch {
33-
throw new RuntimeError("INVALID_BALANCE");
33+
throw new RuntimeError("INVALID_AMOUNT");
3434
}
3535

3636
return {

modules/currency/scripts/get_balance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function run(
1414
ctx: ScriptContext,
1515
req: Request,
1616
): Promise<Response> {
17-
await ctx.call("rate_limit", "throttle", { requests: 25 });
17+
await ctx.modules.rateLimit.throttle({ requests: 25 });
1818

1919
return {
2020
balance: await getBalance(ctx.db, req.userId),

modules/currency/scripts/get_balance_by_token.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@ export async function run(
1313
ctx: ScriptContext,
1414
req: Request,
1515
): Promise<Response> {
16-
await ctx.call("rate_limit", "throttle", { requests: 25 });
16+
await ctx.modules.rateLimit.throttle({ requests: 25 });
1717

18-
const { userId } = await ctx.call("users", "validate_token", {
19-
userToken: req.userToken,
20-
}) as any;
21-
22-
const { balance } = await ctx.call("currency", "get_balance", {
23-
userId,
24-
}) as any;
18+
const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken });
19+
const { balance } = await ctx.modules.currency.getBalance({ userId });
2520

2621
return {
2722
userId,

modules/currency/scripts/set_balance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export async function run(
1616
): Promise<Response> {
1717
await ctx.modules.rateLimit.throttle({ requests: 25 });
1818

19-
if (req.balance < 0) throw new RuntimeError("INVALID_AMOUNT");
19+
if (req.balance < 0 || !Number.isFinite(req.balance)) throw new RuntimeError("INVALID_AMOUNT");
2020

2121
try {
2222
await setBalance(ctx.db, req.userId, req.balance);
2323
} catch {
24-
throw new RuntimeError("INVALID_BALANCE");
24+
throw new RuntimeError("INVALID_AMOUNT");
2525
}
2626

2727
return {};

modules/currency/scripts/withdraw.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export async function run(
1919
ctx: ScriptContext,
2020
req: Request,
2121
): Promise<Response> {
22-
await ctx.call("rate_limit", "throttle", { requests: 25 });
22+
await ctx.modules.rateLimit.throttle({ requests: 25 });
2323

24-
if (req.amount < 0) throw new RuntimeError("INVALID_AMOUNT");
24+
if (req.amount < 0 || !Number.isFinite(req.amount)) throw new RuntimeError("INVALID_AMOUNT");
2525

2626
return ctx.db.$transaction(async (tx) => {
2727
const balance = await getBalance(tx, req.userId);
@@ -33,7 +33,7 @@ export async function run(
3333
try {
3434
await setBalance(tx, req.userId, updatedBalance);
3535
} catch {
36-
throw new RuntimeError("INVALID_BALANCE");
36+
throw new RuntimeError("INVALID_AMOUNT");
3737
}
3838

3939
return {

modules/currency/tests/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test (
66
"get balance for nonexistent user",
77
async (ctx: TestContext) => {
88
const { balance } = await ctx.modules.currency.getBalance({
9-
userId: "Not a real user",
9+
userId: "00000000-0000-0000-0000-000000000000",
1010
});
1111

1212
assertEquals(balance, 0);

modules/currency/utils/set_balance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const setBalance = async (
1010
userId: string,
1111
balance: number,
1212
) => {
13-
if (balance < 0) throw new RuntimeError("INVALID_BALANCE");
13+
if (balance < 0) throw new RuntimeError("INVALID_AMOUNT");
1414

1515
await db.userWallet.upsert({
1616
where: {

0 commit comments

Comments
 (0)