forked from syntaxbullet/AuroraBot-discord
feat: Implement custom error classes, a Drizzle transaction utility, and update Discord.js ephemeral message flags.
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
|
||||
import { inventory, items, users } from "@/db/schema";
|
||||
import { eq, and, sql, count } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { economyService } from "@/modules/economy/economy.service";
|
||||
import { config } from "@/lib/config";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
|
||||
export const inventoryService = {
|
||||
addItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
addItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
// Check if item exists in inventory
|
||||
const existing = await txFn.query.inventory.findFirst({
|
||||
where: and(
|
||||
@@ -39,7 +40,7 @@ export const inventoryService = {
|
||||
.from(inventory)
|
||||
.where(eq(inventory.userId, BigInt(userId)));
|
||||
|
||||
if (inventoryCount.count >= config.inventory.maxSlots) {
|
||||
if (inventoryCount && inventoryCount.count >= config.inventory.maxSlots) {
|
||||
throw new Error(`Inventory full (Max ${config.inventory.maxSlots} slots)`);
|
||||
}
|
||||
|
||||
@@ -56,12 +57,11 @@ export const inventoryService = {
|
||||
.returning();
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
removeItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
removeItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
const existing = await txFn.query.inventory.findFirst({
|
||||
where: and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
@@ -93,8 +93,7 @@ export const inventoryService = {
|
||||
.returning();
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
getInventory: async (userId: string) => {
|
||||
@@ -106,8 +105,8 @@ export const inventoryService = {
|
||||
});
|
||||
},
|
||||
|
||||
buyItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
buyItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
const item = await txFn.query.items.findFirst({
|
||||
where: eq(items.id, itemId),
|
||||
});
|
||||
@@ -123,9 +122,7 @@ export const inventoryService = {
|
||||
await inventoryService.addItem(userId, itemId, quantity, txFn);
|
||||
|
||||
return { success: true, item, totalPrice };
|
||||
};
|
||||
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
getItem: async (itemId: number) => {
|
||||
|
||||
Reference in New Issue
Block a user