+21
-4
packages/api/routes/api/v1/currency.ts
+21
-4
packages/api/routes/api/v1/currency.ts
···
33
33
});
34
34
35
35
currency.post("/deposit", isAuthenticated, async (c) => {
36
-
const serviceType = c.req.query("serviceType");
37
-
const serviceId = c.req.query("serviceId");
36
+
const { serviceType, serviceId, amount } = await c.req.json();
38
37
39
38
if (!serviceId || !serviceType) return c.json({ error: "Missing serviceId or serviceType" }, 400);
39
+
if (!amount) return c.json({ error: "Missing amount" }, 400);
40
40
41
41
const userId = await findUserId(serviceType, serviceId);
42
42
if (!userId) return c.json({ error: "User not found" }, 404);
···
44
44
const userCurrency = await UserCurrency.findOne({ userId, type: UserCurrencyType.BITS });
45
45
if (!userCurrency) return c.json({ error: "User currency not found" }, 404);
46
46
47
-
const amount = c.req.query("amount");
47
+
userCurrency.amount += parseInt(amount);
48
+
await userCurrency.save();
49
+
50
+
return c.json(userCurrency);
51
+
});
52
+
53
+
currency.post("/withdraw", isAuthenticated, async (c) => {
54
+
const { serviceType, serviceId, amount } = await c.req.json();
55
+
56
+
if (!serviceId || !serviceType) return c.json({ error: "Missing serviceId or serviceType" }, 400);
48
57
if (!amount) return c.json({ error: "Missing amount" }, 400);
49
58
50
-
userCurrency.amount += parseInt(amount);
59
+
const userId = await findUserId(serviceType, serviceId);
60
+
if (!userId) return c.json({ error: "User not found" }, 404);
61
+
62
+
const userCurrency = await UserCurrency.findOne({ userId, type: UserCurrencyType.BITS });
63
+
if (!userCurrency) return c.json({ error: "User currency not found" }, 404);
64
+
65
+
if (userCurrency.amount < parseInt(amount)) return c.json({ error: "Insufficient balance" }, 400);
66
+
67
+
userCurrency.amount -= parseInt(amount);
51
68
await userCurrency.save();
52
69
53
70
return c.json(userCurrency);