feat: Implement userService.getOrCreateUser and integrate it across commands, remove old utility scripts, and fix daily bonus calculation.
This commit is contained in:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user