feat: Introduce new modules for class, inventory, leveling, and quests with expanded schema, refactor user service, and add verification scripts.
This commit is contained in:
63
src/modules/class/class.service.ts
Normal file
63
src/modules/class/class.service.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
import { classes, users } from "@/db/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
|
||||
export const classService = {
|
||||
getAllClasses: async () => {
|
||||
return await DrizzleClient.query.classes.findMany();
|
||||
},
|
||||
|
||||
assignClass: async (userId: string, classId: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const cls = await txFn.query.classes.findFirst({
|
||||
where: eq(classes.id, classId),
|
||||
});
|
||||
|
||||
if (!cls) throw new Error("Class not found");
|
||||
|
||||
const [user] = await txFn.update(users)
|
||||
.set({ classId: classId })
|
||||
.where(eq(users.id, BigInt(userId)))
|
||||
.returning();
|
||||
|
||||
return user;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
modifyClassBalance: async (classId: bigint, amount: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const cls = await txFn.query.classes.findFirst({
|
||||
where: eq(classes.id, classId),
|
||||
});
|
||||
|
||||
if (!cls) throw new Error("Class not found");
|
||||
|
||||
if (amount < 0n && (cls.balance ?? 0n) < -amount) {
|
||||
throw new Error("Insufficient class funds");
|
||||
}
|
||||
|
||||
const [updatedClass] = await txFn.update(classes)
|
||||
.set({
|
||||
balance: sql`${classes.balance} + ${amount}`,
|
||||
})
|
||||
.where(eq(classes.id, classId))
|
||||
.returning();
|
||||
|
||||
return updatedClass;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
updateClass: async (id: bigint, data: Partial<typeof classes.$inferInsert>, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const [updatedClass] = await txFn.update(classes)
|
||||
.set(data)
|
||||
.where(eq(classes.id, id))
|
||||
.returning();
|
||||
return updatedClass;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}
|
||||
};
|
||||
@@ -1,18 +1,201 @@
|
||||
import { DrizzleClient } from "@lib/DrizzleClient";
|
||||
import { users } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { users, transactions, cooldowns } from "@/db/schema";
|
||||
import { eq, sql, and, gt } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { userService } from "@/modules/user/user.service";
|
||||
|
||||
export async function getUserBalance(userId: string) {
|
||||
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.userId, userId) });
|
||||
return user?.balance ?? 0;
|
||||
}
|
||||
const DAILY_REWARD_AMOUNT = 100n;
|
||||
const STREAK_BONUS = 10n;
|
||||
const DAILY_COOLDOWN = 24 * 60 * 60 * 1000; // 24 hours in ms
|
||||
|
||||
export async function setUserBalance(userId: string, balance: number) {
|
||||
await DrizzleClient.update(users).set({ balance }).where(eq(users.userId, userId));
|
||||
}
|
||||
export const economyService = {
|
||||
transfer: async (fromUserId: string, toUserId: string, amount: bigint, tx?: any) => {
|
||||
if (amount <= 0n) {
|
||||
throw new Error("Amount must be positive");
|
||||
}
|
||||
|
||||
export async function addUserBalance(userId: string, amount: number) {
|
||||
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.userId, userId) });
|
||||
if (!user) return;
|
||||
await DrizzleClient.update(users).set({ balance: user.balance + amount }).where(eq(users.userId, userId));
|
||||
}
|
||||
if (fromUserId === toUserId) {
|
||||
throw new Error("Cannot transfer to self");
|
||||
}
|
||||
|
||||
const execute = async (txFn: any) => {
|
||||
// Check sender balance
|
||||
const sender = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(fromUserId)),
|
||||
});
|
||||
|
||||
if (!sender) {
|
||||
throw new Error("Sender not found");
|
||||
}
|
||||
|
||||
if ((sender.balance ?? 0n) < amount) {
|
||||
throw new Error("Insufficient funds");
|
||||
}
|
||||
|
||||
// Deduct from sender
|
||||
await txFn.update(users)
|
||||
.set({
|
||||
balance: sql`${users.balance} - ${amount}`,
|
||||
})
|
||||
.where(eq(users.id, BigInt(fromUserId)));
|
||||
|
||||
// Add to receiver
|
||||
await txFn.update(users)
|
||||
.set({
|
||||
balance: sql`${users.balance} + ${amount}`,
|
||||
})
|
||||
.where(eq(users.id, BigInt(toUserId)));
|
||||
|
||||
// Create transaction records
|
||||
// 1. Debit for sender
|
||||
await txFn.insert(transactions).values({
|
||||
userId: BigInt(fromUserId),
|
||||
amount: -amount,
|
||||
type: 'TRANSFER_OUT',
|
||||
description: `Transfer to ${toUserId}`,
|
||||
});
|
||||
|
||||
// 2. Credit for receiver
|
||||
await txFn.insert(transactions).values({
|
||||
userId: BigInt(toUserId),
|
||||
amount: amount,
|
||||
type: 'TRANSFER_IN',
|
||||
description: `Transfer from ${fromUserId}`,
|
||||
});
|
||||
|
||||
return { success: true, amount };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
claimDaily: async (userId: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const now = new Date();
|
||||
const startOfDay = new Date(now);
|
||||
startOfDay.setHours(0, 0, 0, 0);
|
||||
|
||||
// Check cooldown
|
||||
const cooldown = await txFn.query.cooldowns.findFirst({
|
||||
where: and(
|
||||
eq(cooldowns.userId, BigInt(userId)),
|
||||
eq(cooldowns.actionKey, 'daily')
|
||||
),
|
||||
});
|
||||
|
||||
if (cooldown && cooldown.readyAt > now) {
|
||||
throw new Error(`Daily already claimed. Ready at ${cooldown.readyAt}`);
|
||||
}
|
||||
|
||||
// Get user for streak logic
|
||||
const user = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(userId)),
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new Error("User not found");
|
||||
}
|
||||
|
||||
let streak = (user.dailyStreak || 0) + 1;
|
||||
|
||||
// If previous cooldown exists and expired more than 24h ago (meaning >48h since last claim), reset streak
|
||||
if (cooldown) {
|
||||
const timeSinceReady = now.getTime() - cooldown.readyAt.getTime();
|
||||
if (timeSinceReady > 24 * 60 * 60 * 1000) {
|
||||
streak = 1;
|
||||
}
|
||||
} else {
|
||||
streak = 1;
|
||||
}
|
||||
|
||||
const bonus = BigInt(streak) * STREAK_BONUS;
|
||||
|
||||
const totalReward = DAILY_REWARD_AMOUNT + bonus;
|
||||
|
||||
// Update User w/ Economy Service (reuse modifyUserBalance if we split it out, but here manual is fine for atomic combined streak update)
|
||||
// Actually, we can just update directly here as we are already refining specific fields like streak.
|
||||
await txFn.update(users)
|
||||
.set({
|
||||
balance: sql`${users.balance} + ${totalReward}`,
|
||||
dailyStreak: streak,
|
||||
xp: sql`${users.xp} + 10`, // Small XP reward for daily
|
||||
})
|
||||
.where(eq(users.id, BigInt(userId)));
|
||||
|
||||
// Set new cooldown (now + 24h)
|
||||
const nextReadyAt = new Date(now.getTime() + DAILY_COOLDOWN);
|
||||
|
||||
await txFn.insert(cooldowns)
|
||||
.values({
|
||||
userId: BigInt(userId),
|
||||
actionKey: 'daily',
|
||||
readyAt: nextReadyAt,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [cooldowns.userId, cooldowns.actionKey],
|
||||
set: { readyAt: nextReadyAt },
|
||||
});
|
||||
|
||||
// Log Transaction
|
||||
await txFn.insert(transactions).values({
|
||||
userId: BigInt(userId),
|
||||
amount: totalReward,
|
||||
type: 'DAILY_REWARD',
|
||||
description: `Daily reward (Streak: ${streak})`,
|
||||
});
|
||||
|
||||
return { claimed: true, amount: totalReward, streak, nextReadyAt };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
modifyUserBalance: async (id: string, amount: bigint, type: string, description: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
if (amount < 0n) {
|
||||
// Check sufficient funds if removing
|
||||
const user = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id))
|
||||
});
|
||||
if (!user || (user.balance ?? 0n) < -amount) {
|
||||
throw new Error("Insufficient funds");
|
||||
}
|
||||
}
|
||||
|
||||
const [user] = await txFn.update(users)
|
||||
.set({
|
||||
balance: sql`${users.balance} + ${amount}`,
|
||||
})
|
||||
.where(eq(users.id, BigInt(id)))
|
||||
.returning();
|
||||
|
||||
await txFn.insert(transactions).values({
|
||||
userId: BigInt(id),
|
||||
amount: amount,
|
||||
type: type,
|
||||
description: description,
|
||||
});
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
124
src/modules/inventory/inventory.service.ts
Normal file
124
src/modules/inventory/inventory.service.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
|
||||
import { inventory, items, users } from "@/db/schema";
|
||||
import { eq, and, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { economyService } from "@/modules/economy/economy.service";
|
||||
|
||||
export const inventoryService = {
|
||||
addItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
// Check if item exists in inventory
|
||||
const existing = await txFn.query.inventory.findFirst({
|
||||
where: and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
eq(inventory.itemId, itemId)
|
||||
),
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
const [entry] = await txFn.update(inventory)
|
||||
.set({
|
||||
quantity: sql`${inventory.quantity} + ${quantity}`,
|
||||
})
|
||||
.where(and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
eq(inventory.itemId, itemId)
|
||||
))
|
||||
.returning();
|
||||
return entry;
|
||||
} else {
|
||||
const [entry] = await txFn.insert(inventory)
|
||||
.values({
|
||||
userId: BigInt(userId),
|
||||
itemId: itemId,
|
||||
quantity: quantity,
|
||||
})
|
||||
.returning();
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
removeItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const existing = await txFn.query.inventory.findFirst({
|
||||
where: and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
eq(inventory.itemId, itemId)
|
||||
),
|
||||
});
|
||||
|
||||
if (!existing || (existing.quantity ?? 0n) < quantity) {
|
||||
throw new Error("Insufficient item quantity");
|
||||
}
|
||||
|
||||
if ((existing.quantity ?? 0n) === quantity) {
|
||||
// Delete if quantity becomes 0
|
||||
await txFn.delete(inventory)
|
||||
.where(and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
eq(inventory.itemId, itemId)
|
||||
));
|
||||
return { itemId, quantity: 0n, userId: BigInt(userId) };
|
||||
} else {
|
||||
const [entry] = await txFn.update(inventory)
|
||||
.set({
|
||||
quantity: sql`${inventory.quantity} - ${quantity}`,
|
||||
})
|
||||
.where(and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
eq(inventory.itemId, itemId)
|
||||
))
|
||||
.returning();
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
getInventory: async (userId: string) => {
|
||||
return await DrizzleClient.query.inventory.findMany({
|
||||
where: eq(inventory.userId, BigInt(userId)),
|
||||
with: {
|
||||
item: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
buyItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const item = await txFn.query.items.findFirst({
|
||||
where: eq(items.id, itemId),
|
||||
});
|
||||
|
||||
if (!item) throw new Error("Item not found");
|
||||
if (!item.price) throw new Error("Item is not for sale");
|
||||
|
||||
const totalPrice = item.price * quantity;
|
||||
|
||||
// Deduct Balance using economy service (passing tx ensures atomicity)
|
||||
await economyService.modifyUserBalance(userId, -totalPrice, 'PURCHASE', `Bought ${quantity}x ${item.name}`, txFn);
|
||||
|
||||
// Add Item (using local logic to keep in same tx, or could refactor addItem to take tx too and call it)
|
||||
// Let's refactor addItem below to accept tx, then call it here?
|
||||
// Since we are modifying buyItem, we can just inline the item addition or call addItem if we update it.
|
||||
// Let's assume we update addItem next. For now, inline the add logic but cleaner.
|
||||
|
||||
const existingInv = await txFn.query.inventory.findFirst({
|
||||
where: and(eq(inventory.userId, BigInt(userId)), eq(inventory.itemId, itemId)),
|
||||
});
|
||||
|
||||
if (existingInv) {
|
||||
await txFn.update(inventory).set({ quantity: sql`${inventory.quantity} + ${quantity}` })
|
||||
.where(and(eq(inventory.userId, BigInt(userId)), eq(inventory.itemId, itemId)));
|
||||
} else {
|
||||
await txFn.insert(inventory).values({ userId: BigInt(userId), itemId, quantity });
|
||||
}
|
||||
|
||||
return { success: true, item, totalPrice };
|
||||
};
|
||||
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}
|
||||
};
|
||||
58
src/modules/leveling/leveling.service.ts
Normal file
58
src/modules/leveling/leveling.service.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { users } from "@/db/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
|
||||
// Simple configurable curve: Base * (Level ^ Exponent)
|
||||
const XP_BASE = 1000;
|
||||
const XP_EXPONENT = 1.5;
|
||||
|
||||
export const levelingService = {
|
||||
// Calculate XP required for a specific level
|
||||
getXpForLevel: (level: number) => {
|
||||
return Math.floor(XP_BASE * Math.pow(level, XP_EXPONENT));
|
||||
},
|
||||
|
||||
addXp: async (id: string, amount: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
// Get current state
|
||||
const user = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id)),
|
||||
});
|
||||
|
||||
if (!user) throw new Error("User not found");
|
||||
|
||||
let newXp = (user.xp ?? 0n) + amount;
|
||||
let currentLevel = user.level ?? 1;
|
||||
let levelUp = false;
|
||||
|
||||
// Check for level up loop
|
||||
let xpForNextLevel = BigInt(levelingService.getXpForLevel(currentLevel));
|
||||
|
||||
while (newXp >= xpForNextLevel) {
|
||||
newXp -= xpForNextLevel;
|
||||
currentLevel++;
|
||||
levelUp = true;
|
||||
xpForNextLevel = BigInt(levelingService.getXpForLevel(currentLevel));
|
||||
}
|
||||
|
||||
// Update user
|
||||
const [updatedUser] = await txFn.update(users)
|
||||
.set({
|
||||
xp: newXp,
|
||||
level: currentLevel,
|
||||
})
|
||||
.where(eq(users.id, BigInt(id)))
|
||||
.returning();
|
||||
|
||||
return { user: updatedUser, levelUp, currentLevel };
|
||||
}
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
})
|
||||
}
|
||||
},
|
||||
};
|
||||
89
src/modules/quest/quest.service.ts
Normal file
89
src/modules/quest/quest.service.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
|
||||
import { quests, userQuests, users } from "@/db/schema";
|
||||
import { eq, and, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { economyService } from "@/modules/economy/economy.service";
|
||||
import { levelingService } from "@/modules/leveling/leveling.service";
|
||||
|
||||
export const questService = {
|
||||
assignQuest: async (userId: string, questId: number, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
return await txFn.insert(userQuests)
|
||||
.values({
|
||||
userId: BigInt(userId),
|
||||
questId: questId,
|
||||
progress: 0,
|
||||
})
|
||||
.onConflictDoNothing() // Ignore if already assigned
|
||||
.returning();
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
updateProgress: async (userId: string, questId: number, progress: number, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
return await txFn.update(userQuests)
|
||||
.set({ progress: progress })
|
||||
.where(and(
|
||||
eq(userQuests.userId, BigInt(userId)),
|
||||
eq(userQuests.questId, questId)
|
||||
))
|
||||
.returning();
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
completeQuest: async (userId: string, questId: number, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const userQuest = await txFn.query.userQuests.findFirst({
|
||||
where: and(
|
||||
eq(userQuests.userId, BigInt(userId)),
|
||||
eq(userQuests.questId, questId)
|
||||
),
|
||||
with: {
|
||||
quest: true,
|
||||
}
|
||||
});
|
||||
|
||||
if (!userQuest) throw new Error("Quest not assigned");
|
||||
if (userQuest.completedAt) throw new Error("Quest already completed");
|
||||
|
||||
// Mark completed
|
||||
await txFn.update(userQuests)
|
||||
.set({ completedAt: new Date() })
|
||||
.where(and(
|
||||
eq(userQuests.userId, BigInt(userId)),
|
||||
eq(userQuests.questId, questId)
|
||||
));
|
||||
|
||||
// Distribute Rewards
|
||||
const rewards = userQuest.quest.rewards as { xp?: number, balance?: number };
|
||||
const results = { xp: 0n, balance: 0n };
|
||||
|
||||
if (rewards?.balance) {
|
||||
const bal = BigInt(rewards.balance);
|
||||
await economyService.modifyUserBalance(userId, bal, 'QUEST_REWARD', `Reward for quest ${questId}`, txFn);
|
||||
results.balance = bal;
|
||||
}
|
||||
|
||||
if (rewards?.xp) {
|
||||
const xp = BigInt(rewards.xp);
|
||||
await levelingService.addXp(userId, xp, txFn);
|
||||
results.xp = xp;
|
||||
}
|
||||
|
||||
return { success: true, rewards: results };
|
||||
};
|
||||
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
|
||||
getUserQuests: async (userId: string) => {
|
||||
return await DrizzleClient.query.userQuests.findMany({
|
||||
where: eq(userQuests.userId, BigInt(userId)),
|
||||
with: {
|
||||
quest: true,
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
44
src/modules/user/user.service.ts
Normal file
44
src/modules/user/user.service.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { users } from "@/db/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
|
||||
export const userService = {
|
||||
getUserById: async (id: string) => {
|
||||
const user = await DrizzleClient.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id)),
|
||||
with: { class: true }
|
||||
});
|
||||
return user;
|
||||
},
|
||||
getUserByUsername: async (username: string) => {
|
||||
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
|
||||
return user;
|
||||
},
|
||||
createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const [user] = await txFn.insert(users).values({
|
||||
id: BigInt(id),
|
||||
username,
|
||||
classId,
|
||||
}).returning();
|
||||
return user;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
updateUser: async (id: string, data: Partial<typeof users.$inferInsert>, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const [user] = await txFn.update(users)
|
||||
.set(data)
|
||||
.where(eq(users.id, BigInt(id)))
|
||||
.returning();
|
||||
return user;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
deleteUser: async (id: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
await txFn.delete(users).where(eq(users.id, BigInt(id)));
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
import { DrizzleClient } from "@lib/DrizzleClient";
|
||||
import { users } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export async function getUserById(userId: string) {
|
||||
return await DrizzleClient.query.users.findFirst({ where: eq(users.userId, userId) });
|
||||
}
|
||||
|
||||
export async function createUser(userId: string) {
|
||||
return (await DrizzleClient.insert(users).values({ userId }).returning())[0]!;
|
||||
}
|
||||
|
||||
export async function updateUserDaily(userId: string, lastDaily: Date, dailyStreak: number) {
|
||||
await DrizzleClient.update(users).set({ lastDaily, dailyStreak }).where(eq(users.userId, userId));
|
||||
}
|
||||
Reference in New Issue
Block a user