| 484 | }; |
| 485 | |
| 486 | export const updateUser = async (userId: string, userData: Partial<User>) => { |
| 487 | // Validate email if it's being updated |
| 488 | if (userData.email !== undefined) { |
| 489 | if (!userData.email || userData.email.trim() === "") { |
| 490 | throw new Error("Email is required and cannot be empty"); |
| 491 | } |
| 492 | |
| 493 | // Basic email format validation |
| 494 | const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 495 | if (!emailRegex.test(userData.email)) { |
| 496 | throw new Error("Please enter a valid email address"); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | const userResult = await db |
| 501 | .update(user) |
| 502 | .set({ |
| 503 | ...userData, |
| 504 | }) |
| 505 | .where(eq(user.id, userId)) |
| 506 | .returning() |
| 507 | .then((res) => res[0]); |
| 508 | |
| 509 | return userResult; |
| 510 | }; |
| 511 | |
| 512 | export const createApiKey = async ( |
| 513 | userId: string, |