Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 159x 2x 2x 22x 22x 22x 22x 22x 22x 14x 14x 2x 2x 2x 13x 13x 2x 2x 14x 2x 2x 2x 2x 22x 22x 22x 22x 22x 22x 22x 18x 18x 18x 64x 18x 64x 18x 18x 61x 18x 61x 18x 17x 17x 59x 59x 17x 59x 17x 17x 17x 17x 17x 59x 58x 58x 17x 17x 17x 56x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 56x 56x 22x 22x 22x 22x 22x 22x 22x 22x 18x 18x 18x 18x 18x 18x 18x 18x 62x 18x 62x 61x 61x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 59x 59x 59x 19x 19x 19x 19x 19x 19x 19x 19x 59x 19x 19x 58x 19x 58x 19x 19x 19x 19x 54x 22x 22x 22x 22x 22x 22x 22x 22x 54x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 22x 22x 22x 22x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 306x 306x 90x 90x 90x 90x 90x 90x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 305x 1x 1x 304x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 9x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 8x 1x 7x 2x 1x 1x 5x 22x 22x 22x 22x 1x 1x 1x 4x 1x 3x 2x 1x 22x 22x 22x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 35x 35x 35x 35x 2x 3x 22x 22x 22x 22x 22x 22x 22x 22x 22x 2x 2x 2x 2x 101x 101x 2x 2x 1x 22x 22x 5x 5x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 22x 22x 22x 22x 22x 22x 10x 1x 10x 8x 22x 22x 22x 22x 2x 1x 22x 22x 22x 22x 22x 4x 4x 3x 1x 22x 22x 22x 22x 1x 2x 2x 1x 22x 22x 22x 22x 22x 22x 22x 22x 3x 3x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 2x 2x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 3x 3x 2x 3x 2x 1x 5x 5x 5x 2x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 2x 2x 4x 4x 4x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 2x 2x 2x 2x | // src/services/db/user.db.ts
import { Pool, PoolClient } from 'pg';
import { getPool } from './connection.db';
import type { Logger } from 'pino';
import { NotFoundError, handleDbError } from './errors.db';
import {
Profile,
MasterGroceryItem,
ShoppingList,
ActivityLogItem,
UserProfile,
SearchQuery,
User,
} from '../../types';
import { ShoppingRepository } from './shopping.db';
import { PersonalizationRepository } from './personalization.db';
import { withTransaction } from './connection.db';
/**
* Defines the structure of a user object as returned from the database.
*/
interface DbUser {
user_id: string;
email: string;
// The password_hash can be null for users who signed up via OAuth.
password_hash: string | null;
refresh_token?: string | null;
failed_login_attempts: number;
last_failed_login: string | null; // This will be a date string from the DB
created_at: string;
updated_at: string;
}
export class UserRepository {
private db: Pool | PoolClient;
constructor(db: Pool | PoolClient = getPool()) {
this.db = db;
}
/**
* Finds a user by their email in the public.users table.
* @param email The email of the user to find.
* @returns A promise that resolves to the user object or undefined if not found.
*/
async findUserByEmail(email: string, logger: Logger): Promise<DbUser | undefined> {
logger.debug({ email }, `[DB findUserByEmail] Searching for user.`);
try {
const res = await this.db.query<DbUser>(
'SELECT user_id, email, password_hash, refresh_token, failed_login_attempts, last_failed_login, created_at, updated_at FROM public.users WHERE email = $1',
[email],
);
const userFound = res.rows[0];
logger.debug(
`[DB findUserByEmail] Query for ${email} result: ${userFound ? `FOUND user ID ${userFound.user_id}` : 'NOT FOUND'}`,
);
return res.rows[0];
} catch (error) {
handleDbError(
error,
logger,
'Database error in findUserByEmail',
{ email },
{
defaultMessage: 'Failed to retrieve user from database.',
},
);
}
}
/**
* The internal logic for creating a user. This method assumes it is being run
* within a database transaction and operates on a single PoolClient.
*/
private async _createUser(
dbClient: PoolClient,
email: string,
passwordHash: string | null,
profileData: { full_name?: string; avatar_url?: string },
logger: Logger,
): Promise<UserProfile> {
logger.debug(`[DB _createUser] Starting user creation for email: ${email}`);
await dbClient.query("SELECT set_config('my_app.user_metadata', $1, true)", [
JSON.stringify(profileData ?? {}),
]);
logger.debug(`[DB _createUser] Session metadata set for ${email}.`);
const userInsertRes = await dbClient.query<{ user_id: string; email: string }>(
'INSERT INTO public.users (email, password_hash) VALUES ($1, $2) RETURNING user_id, email',
[email, passwordHash],
);
const newUserId = userInsertRes.rows[0].user_id;
logger.debug(`[DB _createUser] Inserted into users table. New user ID: ${newUserId}`);
const profileQuery = `
SELECT u.user_id, u.email, u.created_at as user_created_at, u.updated_at as user_updated_at, p.full_name, p.avatar_url, p.role, p.points, p.preferences, p.created_at, p.updated_at
FROM public.users u
JOIN public.profiles p ON u.user_id = p.user_id
WHERE u.user_id = $1;
`;
const finalProfileRes = await dbClient.query(profileQuery, [newUserId]);
const flatProfile = finalProfileRes.rows[0];
if (!flatProfile) {
throw new Error('Failed to create or retrieve user profile after registration.');
}
const fullUserProfile: UserProfile = {
user: {
user_id: flatProfile.user_id,
email: flatProfile.email,
created_at: flatProfile.user_created_at,
updated_at: flatProfile.user_updated_at,
},
full_name: flatProfile.full_name,
avatar_url: flatProfile.avatar_url,
role: flatProfile.role,
points: flatProfile.points,
preferences: flatProfile.preferences,
created_at: flatProfile.created_at,
updated_at: flatProfile.updated_at,
};
logger.debug({ user: fullUserProfile }, `[DB _createUser] Fetched full profile for new user:`);
return fullUserProfile;
}
/**
* Creates a new user in the public.users table.
* This method expects to be run within a transaction, so it requires a PoolClient.
* @param email The user's email.
* @param passwordHash The bcrypt hashed password.
* @param profileData An object containing optional full_name and avatar_url for the profile.
* @returns A promise that resolves to the newly created user object (id, email).
*/
async createUser(
email: string,
passwordHash: string | null,
profileData: { full_name?: string; avatar_url?: string },
logger: Logger,
): Promise<UserProfile> {
// This method is now a wrapper that ensures the core logic runs within a transaction.
try {
// If this.db has a 'connect' method, it's a Pool. We must start a transaction.
if ('connect' in this.db) {
return await withTransaction(async (client) => {
return this._createUser(client, email, passwordHash, profileData, logger);
});
} else {
// If this.db is already a PoolClient, we're inside a transaction. Use it directly.
return await this._createUser(
this.db as PoolClient,
email,
passwordHash,
profileData,
logger,
);
}
} catch (error) {
handleDbError(
error,
logger,
'Error during createUser',
{ email },
{
uniqueMessage: 'A user with this email address already exists.',
defaultMessage: 'Failed to create user in database.',
},
);
}
}
/**
* Finds a user by their email and joins their profile data.
* This is used by the LocalStrategy to get all necessary data for authentication and session creation in one query.
* @param email The email of the user to find.
* @returns A promise that resolves to the combined user and profile object or undefined if not found.
*/
async findUserWithProfileByEmail(
email: string,
logger: Logger,
): Promise<(UserProfile & Omit<DbUser, 'user_id' | 'email'>) | undefined> {
logger.debug({ email }, `[DB findUserWithProfileByEmail] Searching for user.`);
try {
const query = `
SELECT
u.user_id, u.email, u.created_at as user_created_at, u.updated_at as user_updated_at, u.password_hash, u.refresh_token, u.failed_login_attempts, u.last_failed_login,
p.full_name, p.avatar_url, p.role, p.points, p.preferences, p.address_id,
p.created_at, p.updated_at
FROM public.users u
JOIN public.profiles p ON u.user_id = p.user_id
WHERE u.email = $1;
`;
const res = await this.db.query<
DbUser & Profile & { user_created_at: string; user_updated_at: string }
>(query, [email]);
const flatUser = res.rows[0];
if (!flatUser) {
return undefined;
}
// Manually construct the nested UserProfile object and add auth fields
const authableProfile: UserProfile & Omit<DbUser, 'user_id' | 'email'> = {
full_name: flatUser.full_name,
avatar_url: flatUser.avatar_url,
role: flatUser.role,
points: flatUser.points,
preferences: flatUser.preferences,
address_id: flatUser.address_id,
created_at: flatUser.created_at,
updated_at: flatUser.updated_at,
user: {
user_id: flatUser.user_id,
email: flatUser.email,
created_at: flatUser.user_created_at,
updated_at: flatUser.user_updated_at,
},
password_hash: flatUser.password_hash,
failed_login_attempts: flatUser.failed_login_attempts,
last_failed_login: flatUser.last_failed_login,
refresh_token: flatUser.refresh_token,
};
return authableProfile;
} catch (error) {
handleDbError(
error,
logger,
'Database error in findUserWithProfileByEmail',
{ email },
{
defaultMessage: 'Failed to retrieve user with profile from database.',
},
);
}
}
/**
* Finds a user by their ID. Used by the JWT strategy to validate tokens.
* @param id The UUID of the user to find.
* @returns A promise that resolves to the user object (id, email) or undefined if not found.
*/
// prettier-ignore
async findUserById(userId: string, logger: Logger): Promise<User> {
try {
const res = await this.db.query<User>(
'SELECT user_id, email, created_at, updated_at FROM public.users WHERE user_id = $1',
[userId]
);
if (res.rowCount === 0) {
throw new NotFoundError(`User with ID ${userId} not found.`);
}
return res.rows[0];
} catch (error) {
if (error instanceof NotFoundError) throw error;
handleDbError(error, logger, 'Database error in findUserById', { userId }, {
defaultMessage: 'Failed to retrieve user by ID from database.',
});
}
}
/**
* Finds a user by their ID, including their password hash.
* This should only be used in contexts where password verification is required, like account deletion.
* @param id The UUID of the user to find.
* @returns A promise that resolves to the user object (id, email, password_hash) or undefined if not found.
*/
// prettier-ignore
async findUserWithPasswordHashById(userId: string, logger: Logger): Promise<User & { password_hash: string | null }> {
try {
const res = await this.db.query<User & { password_hash: string | null }>(
'SELECT user_id, email, password_hash, created_at, updated_at FROM public.users WHERE user_id = $1',
[userId]
);
if ((res.rowCount ?? 0) === 0) {
throw new NotFoundError(`User with ID ${userId} not found.`);
}
return res.rows[0];
} catch (error) {
if (error instanceof NotFoundError) throw error;
handleDbError(error, logger, 'Database error in findUserWithPasswordHashById', { userId }, {
defaultMessage: 'Failed to retrieve user with sensitive data by ID from database.',
});
}
}
/**
* Finds a user's profile by their user ID.
* @param id The UUID of the user.
* @returns A promise that resolves to the user's profile object or undefined if not found.
*/
// prettier-ignore
async findUserProfileById(userId: string, logger: Logger): Promise<UserProfile> {
try {
const res = await this.db.query<UserProfile>(
`SELECT p.full_name, p.avatar_url, p.address_id, p.preferences, p.role, p.points,
p.created_at, p.updated_at,
json_build_object(
'user_id', u.user_id,
'email', u.email,
'created_at', u.created_at,
'updated_at', u.updated_at
) as user,
CASE
WHEN a.address_id IS NOT NULL THEN json_build_object(
'address_id', a.address_id,
'address_line_1', a.address_line_1,
'address_line_2', a.address_line_2,
'city', a.city,
'province_state', a.province_state,
'postal_code', a.postal_code,
'country', a.country
)
ELSE NULL
END as address
FROM public.profiles p
JOIN public.users u ON p.user_id = u.user_id
LEFT JOIN public.addresses a ON p.address_id = a.address_id
WHERE p.user_id = $1`,
[userId]
);
if (res.rowCount === 0) {
throw new NotFoundError('Profile not found for this user.');
}
return res.rows[0];
} catch (error) {
if (error instanceof NotFoundError) {
throw error;
}
handleDbError(error, logger, 'Database error in findUserProfileById', { userId }, {
defaultMessage: 'Failed to retrieve user profile from database.',
});
}
}
/**
* Updates the profile for a given user.
* @param id The UUID of the user.
* @param profileData The profile data to update (e.g., full_name, avatar_url).
* @returns A promise that resolves to the updated profile object.
*/
// prettier-ignore
async updateUserProfile(userId: string, profileData: Partial<Pick<Profile, 'full_name' | 'avatar_url' | 'address_id'>>, logger: Logger): Promise<Profile> {
try {
const { full_name, avatar_url, address_id } = profileData;
const fieldsToUpdate = [];
const values = [];
let paramIndex = 1;
if (full_name !== undefined) { fieldsToUpdate.push(`full_name = $${paramIndex++}`); values.push(full_name); }
if (avatar_url !== undefined) { fieldsToUpdate.push(`avatar_url = $${paramIndex++}`); values.push(avatar_url); }
if (address_id !== undefined) { fieldsToUpdate.push(`address_id = $${paramIndex++}`); values.push(address_id); }
if (fieldsToUpdate.length === 0) {
return this.findUserProfileById(userId, logger) as Promise<Profile>;
}
values.push(userId);
const query = `
UPDATE public.profiles
SET ${fieldsToUpdate.join(', ')}, updated_at = now()
WHERE user_id = $${paramIndex}
RETURNING *;
`;
const res = await this.db.query<Profile>(
query, values
);
if (res.rowCount === 0) {
throw new NotFoundError('User not found or user does not have permission to update.');
}
return res.rows[0];
} catch (error) {
if (error instanceof NotFoundError) {
throw error;
}
handleDbError(error, logger, 'Database error in updateUserProfile', { userId, profileData }, {
fkMessage: 'The specified address does not exist.',
defaultMessage: 'Failed to update user profile in database.',
});
}
}
/**
* Updates the preferences for a given user.
* @param id The UUID of the user.
* @param preferences The preferences object to save.
* @returns A promise that resolves to the updated profile object.
*/
// prettier-ignore
async updateUserPreferences(userId: string, preferences: Profile['preferences'], logger: Logger): Promise<Profile> {
try {
const res = await this.db.query<Profile>(
`UPDATE public.profiles
SET preferences = COALESCE(preferences, '{}'::jsonb) || $1, updated_at = now()
WHERE user_id = $2
RETURNING user_id, full_name, avatar_url, preferences, role`,
[preferences, userId]
);
if (res.rowCount === 0) {
throw new NotFoundError('User not found or user does not have permission to update.');
}
return res.rows[0];
} catch (error) {
if (error instanceof NotFoundError) {
throw error;
}
handleDbError(error, logger, 'Database error in updateUserPreferences', { userId, preferences }, {
defaultMessage: 'Failed to update user preferences in database.',
});
}
}
/**
* Updates the password hash for a given user.
* @param id The UUID of the user.
* @param passwordHash The new bcrypt hashed password.
*/
// prettier-ignore
async updateUserPassword(userId: string, passwordHash: string, logger: Logger): Promise<void> {
try {
await this.db.query(
'UPDATE public.users SET password_hash = $1 WHERE user_id = $2',
[passwordHash, userId]
);
} catch (error) {
handleDbError(error, logger, 'Database error in updateUserPassword', { userId }, {
defaultMessage: 'Failed to update user password in database.',
});
}
}
/**
* Deletes a user from the database by their ID.
* @param id The UUID of the user to delete.
*/
// prettier-ignore
async deleteUserById(userId: string, logger: Logger): Promise<void> {
try {
const res = await this.db.query('DELETE FROM public.users WHERE user_id = $1', [userId]);
if (res.rowCount === 0) {
throw new NotFoundError(`User with ID ${userId} not found.`);
}
} catch (error) {
if (error instanceof NotFoundError) throw error;
handleDbError(error, logger, 'Database error in deleteUserById', { userId }, {
defaultMessage: 'Failed to delete user from database.',
});
}
}
/**
* Saves or updates a refresh token for a user.
* @param userId The UUID of the user.
* @param refreshToken The new refresh token to save.
*/
// prettier-ignore
async saveRefreshToken(userId: string, refreshToken: string, logger: Logger): Promise<void> {
try {
await this.db.query(
'UPDATE public.users SET refresh_token = $1 WHERE user_id = $2',
[refreshToken, userId]
);
} catch (error) {
handleDbError(error, logger, 'Database error in saveRefreshToken', { userId }, {
defaultMessage: 'Failed to save refresh token.',
});
}
}
/**
* Finds a user by their refresh token.
* @param refreshToken The refresh token to look up.
* @returns A promise that resolves to the user object (id, email) or undefined if not found.
*/
async findUserByRefreshToken(refreshToken: string, logger: Logger): Promise<User | undefined> {
try {
const res = await this.db.query<User>(
'SELECT user_id, email, created_at, updated_at FROM public.users WHERE refresh_token = $1',
[refreshToken],
);
if ((res.rowCount ?? 0) === 0) {
return undefined;
}
return res.rows[0];
} catch (error) {
handleDbError(
error,
logger,
'Database error in findUserByRefreshToken',
{},
{
defaultMessage: 'Failed to find user by refresh token.',
},
);
}
}
/**
* Deletes a refresh token from the database by setting it to NULL.
* @param refreshToken The refresh token to delete.
*/
async deleteRefreshToken(refreshToken: string, logger: Logger): Promise<void> {
try {
await this.db.query('UPDATE public.users SET refresh_token = NULL WHERE refresh_token = $1', [
refreshToken,
]);
} catch (error) {
// This is a non-critical operation, so we just log the error and continue.
logger.error({ err: error }, 'Database error in deleteRefreshToken');
}
}
/**
* Creates a password reset token for a user.
* @param userId The UUID of the user.
* @param tokenHash The hashed version of the reset token.
* @param expiresAt The timestamp when the token expires.
*/
// prettier-ignore
async createPasswordResetToken(userId: string, tokenHash: string, expiresAt: Date, logger: Logger, client: PoolClient): Promise<void> {
try {
// First, remove any existing tokens for the user to ensure they can only have one active reset request.
await client.query('DELETE FROM public.password_reset_tokens WHERE user_id = $1', [userId]);
// Then, insert the new token.
await client.query(
'INSERT INTO public.password_reset_tokens (user_id, token_hash, expires_at) VALUES ($1, $2, $3)',
[userId, tokenHash, expiresAt]
);
} catch (error) {
handleDbError(error, logger, 'Database error in createPasswordResetToken', { userId }, {
fkMessage: 'The specified user does not exist.',
uniqueMessage: 'A password reset token with this hash already exists.',
defaultMessage: 'Failed to create password reset token.',
});
}
}
/**
* Finds a user and token details by the token hash.
* @returns A promise that resolves to an array of valid token records.
*/
// prettier-ignore
async getValidResetTokens(logger: Logger): Promise<{ user_id: string; token_hash: string; expires_at: Date }[]> {
try {
const res = await this.db.query<{ user_id: string; token_hash: string; expires_at: Date }>(
'SELECT user_id, token_hash, expires_at FROM public.password_reset_tokens WHERE expires_at > NOW()'
);
return res.rows;
} catch (error) {
handleDbError(error, logger, 'Database error in getValidResetTokens', {}, {
defaultMessage: 'Failed to retrieve valid reset tokens.',
});
}
}
/**
* Deletes a password reset token by its hash.
* @param tokenHash The hashed token to delete.
*/
// prettier-ignore
async deleteResetToken(tokenHash: string, logger: Logger): Promise<void> {
try {
await this.db.query('DELETE FROM public.password_reset_tokens WHERE token_hash = $1', [tokenHash]);
} catch (error) {
handleDbError(error, logger, 'Database error in deleteResetToken', { tokenHash }, {
defaultMessage: 'Failed to delete password reset token.',
});
}
}
/**
* Deletes all expired password reset tokens from the database.
* This is intended for a periodic cleanup job.
* @returns A promise that resolves to the number of deleted tokens.
*/
async deleteExpiredResetTokens(logger: Logger): Promise<number> {
try {
const res = await this.db.query(
'DELETE FROM public.password_reset_tokens WHERE expires_at < NOW()',
);
logger.info(
`[DB deleteExpiredResetTokens] Deleted ${res.rowCount ?? 0} expired password reset tokens.`,
);
return res.rowCount ?? 0;
} catch (error) {
handleDbError(
error,
logger,
'Database error in deleteExpiredResetTokens',
{},
{
defaultMessage: 'Failed to delete expired password reset tokens.',
},
);
}
}
/**
* Creates a following relationship between two users.
* @param followerId The ID of the user who is following.
* @param followingId The ID of the user being followed.
*/
async followUser(followerId: string, followingId: string, logger: Logger): Promise<void> {
try {
await this.db.query(
'INSERT INTO public.user_follows (follower_id, following_id) VALUES ($1, $2) ON CONFLICT (follower_id, following_id) DO NOTHING',
[followerId, followingId],
);
} catch (error) {
handleDbError(
error,
logger,
'Database error in followUser',
{ followerId, followingId },
{
fkMessage: 'One or both users do not exist.',
checkMessage: 'A user cannot follow themselves.',
defaultMessage: 'Failed to follow user.',
},
);
}
}
/**
* Removes a following relationship between two users.
* @param followerId The ID of the user who is unfollowing.
* @param followingId The ID of the user being unfollowed.
*/
async unfollowUser(followerId: string, followingId: string, logger: Logger): Promise<void> {
try {
await this.db.query(
'DELETE FROM public.user_follows WHERE follower_id = $1 AND following_id = $2',
[followerId, followingId],
);
} catch (error) {
handleDbError(
error,
logger,
'Database error in unfollowUser',
{ followerId, followingId },
{
defaultMessage: 'Failed to unfollow user.',
},
);
}
}
/**
* Retrieves a personalized activity feed for a user, based on the actions of users they follow.
* @param userId The ID of the user for whom to generate the feed.
* @param limit The number of feed items to retrieve.
* @param offset The number of feed items to skip for pagination.
* @returns A promise that resolves to an array of ActivityLogItem objects.
*/
async getUserFeed(
userId: string,
limit: number,
offset: number,
logger: Logger,
): Promise<ActivityLogItem[]> {
try {
const query = `
SELECT al.*, p.full_name as user_full_name, p.avatar_url as user_avatar_url
FROM public.activity_log al
JOIN public.user_follows uf ON al.user_id = uf.following_id
JOIN public.profiles p ON al.user_id = p.user_id
WHERE uf.follower_id = $1
ORDER BY al.created_at DESC
LIMIT $2 OFFSET $3;
`;
const res = await this.db.query<ActivityLogItem>(query, [userId, limit, offset]);
return res.rows;
} catch (error) {
handleDbError(
error,
logger,
'Database error in getUserFeed',
{ userId, limit, offset },
{
defaultMessage: 'Failed to retrieve user feed.',
},
);
}
}
/**
* Logs a search query made by a user.
* @param queryData The search query data to log.
* @returns A promise that resolves to the created SearchQuery object.
*/
async logSearchQuery(
queryData: Omit<SearchQuery, 'search_query_id' | 'created_at' | 'updated_at'>,
logger: Logger,
): Promise<SearchQuery> {
const { user_id, query_text, result_count, was_successful } = queryData;
try {
const res = await this.db.query<SearchQuery>(
'INSERT INTO public.search_queries (user_id, query_text, result_count, was_successful) VALUES ($1, $2, $3, $4) RETURNING *',
[user_id, query_text, result_count, was_successful],
);
return res.rows[0];
} catch (error) {
handleDbError(
error,
logger,
'Database error in logSearchQuery',
{ queryData },
{
fkMessage: 'The specified user does not exist.',
defaultMessage: 'Failed to log search query.',
},
);
}
}
}
/**
* Gathers all data associated with a specific user for export.
* @param userId The UUID of the user.
* @returns A promise that resolves to an object containing all user data.
*/
// prettier-ignore
export async function exportUserData(userId: string, logger: Logger): Promise<{ profile: Profile; watchedItems: MasterGroceryItem[]; shoppingLists: ShoppingList[] }> {
try {
return await withTransaction(async (client) => {
const userRepo = new UserRepository(client);
const shoppingRepo = new ShoppingRepository(client);
const personalizationRepo = new PersonalizationRepository(client);
// Run queries in parallel for efficiency within the transaction
const profileQuery = userRepo.findUserProfileById(userId, logger);
const watchedItemsQuery = personalizationRepo.getWatchedItems(userId, logger);
const shoppingListsQuery = shoppingRepo.getShoppingLists(userId, logger);
const [profile, watchedItems, shoppingLists] = await Promise.all([profileQuery, watchedItemsQuery, shoppingListsQuery]);
if (!profile) {
// This will be caught by withTransaction and rolled back (though no writes were made)
throw new NotFoundError('User profile not found for data export.');
}
return { profile, watchedItems, shoppingLists };
});
} catch (error) {
handleDbError(error, logger, 'Database error in exportUserData', { userId }, {
defaultMessage: 'Failed to export user data.',
});
}
}
|