feat: Implement userService.getOrCreateUser and integrate it across commands, remove old utility scripts, and fix daily bonus calculation.

This commit is contained in:
syntaxbullet
2025-12-08 10:29:40 +01:00
parent 29c0a4752d
commit 866cfab03e
12 changed files with 42 additions and 94 deletions

View File

@@ -1,7 +1,6 @@
import { users, transactions, cooldowns } from "@/db/schema";
import { eq, sql, and, gt } from "drizzle-orm";
import { eq, sql, and } from "drizzle-orm";
import { DrizzleClient } from "@/lib/DrizzleClient";
import { userService } from "@/modules/user/user.service";
const DAILY_REWARD_AMOUNT = 100n;
const STREAK_BONUS = 10n;
@@ -113,7 +112,7 @@ export const economyService = {
streak = 1;
}
const bonus = BigInt(streak) * STREAK_BONUS;
const bonus = (BigInt(streak) - 1n) * STREAK_BONUS;
const totalReward = DAILY_REWARD_AMOUNT + bonus;

View File

@@ -14,6 +14,24 @@ export const userService = {
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
return user;
},
getOrCreateUser: async (id: string, username: string, tx?: any) => {
const execute = async (txFn: any) => {
let user = await txFn.query.users.findFirst({
where: eq(users.id, BigInt(id)),
with: { class: true }
});
if (!user) {
const [newUser] = await txFn.insert(users).values({
id: BigInt(id),
username,
}).returning();
user = { ...newUser, class: null };
}
return user;
};
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
},
createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: any) => {
const execute = async (txFn: any) => {
const [user] = await txFn.insert(users).values({