forked from syntaxbullet/AuroraBot-discord
refactor: modernize transaction patterns and improve type safety
- Refactored user.service.ts to use withTransaction() helper - Added 14 comprehensive unit tests for user.service.ts - Removed duplicate user creation in interactionCreate.ts - Improved type safety in interaction.routes.ts
This commit is contained in:
@@ -48,13 +48,9 @@ const event: Event<Events.InteractionCreate> = {
|
|||||||
|
|
||||||
// Ensure user exists in database
|
// Ensure user exists in database
|
||||||
try {
|
try {
|
||||||
const user = await userService.getUserById(interaction.user.id);
|
await userService.getOrCreateUser(interaction.user.id, interaction.user.username);
|
||||||
if (!user) {
|
|
||||||
console.log(`🆕 Creating new user entry for ${interaction.user.tag}`);
|
|
||||||
await userService.createUser(interaction.user.id, interaction.user.username);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to check/create user:", error);
|
console.error("Failed to ensure user exists:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
import { ButtonInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from "discord.js";
|
import { ButtonInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from "discord.js";
|
||||||
|
|
||||||
type InteractionHandler = (interaction: any) => Promise<void>;
|
// Union type for all component interactions
|
||||||
|
type ComponentInteraction = ButtonInteraction | StringSelectMenuInteraction | ModalSubmitInteraction;
|
||||||
|
|
||||||
|
// Type for the handler function that modules export
|
||||||
|
type InteractionHandler = (interaction: ComponentInteraction) => Promise<void>;
|
||||||
|
|
||||||
|
// Type for the dynamically imported module containing the handler
|
||||||
|
interface InteractionModule {
|
||||||
|
[key: string]: (...args: any[]) => Promise<void> | any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route definition
|
||||||
interface InteractionRoute {
|
interface InteractionRoute {
|
||||||
predicate: (interaction: ButtonInteraction | StringSelectMenuInteraction | ModalSubmitInteraction) => boolean;
|
predicate: (interaction: ComponentInteraction) => boolean;
|
||||||
handler: () => Promise<any>;
|
handler: () => Promise<InteractionModule>;
|
||||||
method: string;
|
method: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ mockUpdate.mockReturnValue({ set: mockSet });
|
|||||||
mockSet.mockReturnValue({ where: mockWhere });
|
mockSet.mockReturnValue({ where: mockWhere });
|
||||||
mockWhere.mockReturnValue({ returning: mockReturning });
|
mockWhere.mockReturnValue({ returning: mockReturning });
|
||||||
|
|
||||||
|
mockDelete.mockReturnValue({ where: mockWhere });
|
||||||
|
|
||||||
// Mock DrizzleClient
|
// Mock DrizzleClient
|
||||||
mock.module("@/lib/DrizzleClient", () => {
|
mock.module("@/lib/DrizzleClient", () => {
|
||||||
@@ -51,12 +52,39 @@ mock.module("@/lib/DrizzleClient", () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Mock withTransaction helper to use the same pattern as DrizzleClient.transaction
|
||||||
|
mock.module("@/lib/db", () => {
|
||||||
|
return {
|
||||||
|
withTransaction: async (callback: any, tx?: any) => {
|
||||||
|
if (tx) {
|
||||||
|
return callback(tx);
|
||||||
|
}
|
||||||
|
// Simulate transaction by calling the callback with mock db
|
||||||
|
return callback({
|
||||||
|
query: {
|
||||||
|
users: {
|
||||||
|
findFirst: mockFindFirst,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
insert: mockInsert,
|
||||||
|
update: mockUpdate,
|
||||||
|
delete: mockDelete,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe("userService", () => {
|
describe("userService", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockFindFirst.mockReset();
|
mockFindFirst.mockReset();
|
||||||
mockInsert.mockClear();
|
mockInsert.mockClear();
|
||||||
mockValues.mockClear();
|
mockValues.mockClear();
|
||||||
mockReturning.mockClear();
|
mockReturning.mockClear();
|
||||||
|
mockUpdate.mockClear();
|
||||||
|
mockSet.mockClear();
|
||||||
|
mockWhere.mockClear();
|
||||||
|
mockDelete.mockClear();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getUserById", () => {
|
describe("getUserById", () => {
|
||||||
@@ -80,7 +108,91 @@ describe("userService", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("createUser", () => {
|
describe("getUserByUsername", () => {
|
||||||
|
it("should return user when username exists", async () => {
|
||||||
|
const mockUser = { id: 456n, username: "alice", balance: 100n };
|
||||||
|
mockFindFirst.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const result = await userService.getUserByUsername("alice");
|
||||||
|
|
||||||
|
expect(result).toEqual(mockUser as any);
|
||||||
|
expect(mockFindFirst).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return undefined when username not found", async () => {
|
||||||
|
mockFindFirst.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await userService.getUserByUsername("nonexistent");
|
||||||
|
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getUserClass", () => {
|
||||||
|
it("should return user class when user has a class", async () => {
|
||||||
|
const mockClass = { id: 1n, name: "Warrior", emoji: "⚔️" };
|
||||||
|
const mockUser = { id: 123n, username: "testuser", class: mockClass };
|
||||||
|
mockFindFirst.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const result = await userService.getUserClass("123");
|
||||||
|
|
||||||
|
expect(result).toEqual(mockClass as any);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return null when user has no class", async () => {
|
||||||
|
const mockUser = { id: 123n, username: "testuser", class: null };
|
||||||
|
mockFindFirst.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const result = await userService.getUserClass("123");
|
||||||
|
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return undefined when user not found", async () => {
|
||||||
|
mockFindFirst.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await userService.getUserClass("999");
|
||||||
|
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getOrCreateUser (withTransaction)", () => {
|
||||||
|
it("should return existing user if found", async () => {
|
||||||
|
const mockUser = { id: 123n, username: "existinguser", class: null };
|
||||||
|
mockFindFirst.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const result = await userService.getOrCreateUser("123", "existinguser");
|
||||||
|
|
||||||
|
expect(result).toEqual(mockUser as any);
|
||||||
|
expect(mockFindFirst).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockInsert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create new user if not found", async () => {
|
||||||
|
const newUser = { id: 789n, username: "newuser", classId: null };
|
||||||
|
|
||||||
|
// First call returns undefined (user not found)
|
||||||
|
// Second call returns the newly created user (after insert + re-query)
|
||||||
|
mockFindFirst
|
||||||
|
.mockResolvedValueOnce(undefined)
|
||||||
|
.mockResolvedValueOnce({ id: 789n, username: "newuser", class: null });
|
||||||
|
|
||||||
|
mockReturning.mockResolvedValue([newUser]);
|
||||||
|
|
||||||
|
const result = await userService.getOrCreateUser("789", "newuser");
|
||||||
|
|
||||||
|
expect(mockInsert).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockValues).toHaveBeenCalledWith({
|
||||||
|
id: 789n,
|
||||||
|
username: "newuser"
|
||||||
|
});
|
||||||
|
// Should query twice: once to check, once after insert
|
||||||
|
expect(mockFindFirst).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createUser (withTransaction)", () => {
|
||||||
it("should create and return a new user", async () => {
|
it("should create and return a new user", async () => {
|
||||||
const newUser = { id: 456n, username: "newuser", classId: null };
|
const newUser = { id: 456n, username: "newuser", classId: null };
|
||||||
mockReturning.mockResolvedValue([newUser]);
|
mockReturning.mockResolvedValue([newUser]);
|
||||||
@@ -95,5 +207,53 @@ describe("userService", () => {
|
|||||||
classId: undefined
|
classId: undefined
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should create user with classId when provided", async () => {
|
||||||
|
const newUser = { id: 999n, username: "warrior", classId: 5n };
|
||||||
|
mockReturning.mockResolvedValue([newUser]);
|
||||||
|
|
||||||
|
const result = await userService.createUser("999", "warrior", 5n);
|
||||||
|
|
||||||
|
expect(result).toEqual(newUser as any);
|
||||||
|
expect(mockValues).toHaveBeenCalledWith({
|
||||||
|
id: 999n,
|
||||||
|
username: "warrior",
|
||||||
|
classId: 5n
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("updateUser (withTransaction)", () => {
|
||||||
|
it("should update user data", async () => {
|
||||||
|
const updatedUser = { id: 123n, username: "testuser", balance: 500n };
|
||||||
|
mockReturning.mockResolvedValue([updatedUser]);
|
||||||
|
|
||||||
|
const result = await userService.updateUser("123", { balance: 500n });
|
||||||
|
|
||||||
|
expect(result).toEqual(updatedUser as any);
|
||||||
|
expect(mockUpdate).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockSet).toHaveBeenCalledWith({ balance: 500n });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should update multiple fields", async () => {
|
||||||
|
const updatedUser = { id: 456n, username: "alice", xp: 100n, level: 5 };
|
||||||
|
mockReturning.mockResolvedValue([updatedUser]);
|
||||||
|
|
||||||
|
const result = await userService.updateUser("456", { xp: 100n, level: 5 });
|
||||||
|
|
||||||
|
expect(result).toEqual(updatedUser as any);
|
||||||
|
expect(mockSet).toHaveBeenCalledWith({ xp: 100n, level: 5 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("deleteUser (withTransaction)", () => {
|
||||||
|
it("should delete user from database", async () => {
|
||||||
|
mockWhere.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
await userService.deleteUser("123");
|
||||||
|
|
||||||
|
expect(mockDelete).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockWhere).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { users } from "@/db/schema";
|
import { users } from "@/db/schema";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||||
|
import { withTransaction } from "@/lib/db";
|
||||||
|
import type { Transaction } from "@/lib/types";
|
||||||
|
|
||||||
export const userService = {
|
export const userService = {
|
||||||
getUserById: async (id: string) => {
|
getUserById: async (id: string) => {
|
||||||
@@ -14,23 +16,27 @@ export const userService = {
|
|||||||
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
|
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
|
||||||
return user;
|
return user;
|
||||||
},
|
},
|
||||||
getOrCreateUser: async (id: string, username: string, tx?: any) => {
|
getOrCreateUser: async (id: string, username: string, tx?: Transaction) => {
|
||||||
const execute = async (txFn: any) => {
|
return await withTransaction(async (txFn) => {
|
||||||
let user = await txFn.query.users.findFirst({
|
let user = await txFn.query.users.findFirst({
|
||||||
where: eq(users.id, BigInt(id)),
|
where: eq(users.id, BigInt(id)),
|
||||||
with: { class: true }
|
with: { class: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
const [newUser] = await txFn.insert(users).values({
|
await txFn.insert(users).values({
|
||||||
id: BigInt(id),
|
id: BigInt(id),
|
||||||
username,
|
username,
|
||||||
}).returning();
|
}).returning();
|
||||||
user = { ...newUser, class: null };
|
|
||||||
|
// Re-query to get the user with class relation
|
||||||
|
user = await txFn.query.users.findFirst({
|
||||||
|
where: eq(users.id, BigInt(id)),
|
||||||
|
with: { class: true }
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
};
|
}, tx);
|
||||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
|
||||||
},
|
},
|
||||||
getUserClass: async (id: string) => {
|
getUserClass: async (id: string) => {
|
||||||
const user = await DrizzleClient.query.users.findFirst({
|
const user = await DrizzleClient.query.users.findFirst({
|
||||||
@@ -39,31 +45,28 @@ export const userService = {
|
|||||||
});
|
});
|
||||||
return user?.class;
|
return user?.class;
|
||||||
},
|
},
|
||||||
createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: any) => {
|
createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: Transaction) => {
|
||||||
const execute = async (txFn: any) => {
|
return await withTransaction(async (txFn) => {
|
||||||
const [user] = await txFn.insert(users).values({
|
const [user] = await txFn.insert(users).values({
|
||||||
id: BigInt(id),
|
id: BigInt(id),
|
||||||
username,
|
username,
|
||||||
classId,
|
classId,
|
||||||
}).returning();
|
}).returning();
|
||||||
return user;
|
return user;
|
||||||
};
|
}, tx);
|
||||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
|
||||||
},
|
},
|
||||||
updateUser: async (id: string, data: Partial<typeof users.$inferInsert>, tx?: any) => {
|
updateUser: async (id: string, data: Partial<typeof users.$inferInsert>, tx?: Transaction) => {
|
||||||
const execute = async (txFn: any) => {
|
return await withTransaction(async (txFn) => {
|
||||||
const [user] = await txFn.update(users)
|
const [user] = await txFn.update(users)
|
||||||
.set(data)
|
.set(data)
|
||||||
.where(eq(users.id, BigInt(id)))
|
.where(eq(users.id, BigInt(id)))
|
||||||
.returning();
|
.returning();
|
||||||
return user;
|
return user;
|
||||||
};
|
}, tx);
|
||||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
|
||||||
},
|
},
|
||||||
deleteUser: async (id: string, tx?: any) => {
|
deleteUser: async (id: string, tx?: Transaction) => {
|
||||||
const execute = async (txFn: any) => {
|
return await withTransaction(async (txFn) => {
|
||||||
await txFn.delete(users).where(eq(users.id, BigInt(id)));
|
await txFn.delete(users).where(eq(users.id, BigInt(id)));
|
||||||
};
|
}, tx);
|
||||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user