unit test fixes + error refactor
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 11m53s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 11m53s
This commit is contained in:
@@ -107,6 +107,9 @@ export class AdminRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in updateSuggestedCorrection:', { error, correctionId });
|
||||
throw new Error('Failed to update suggested correction.');
|
||||
}
|
||||
@@ -254,6 +257,9 @@ export class AdminRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in updateRecipeCommentStatus:', { error, commentId, status });
|
||||
throw new Error('Failed to update recipe comment status.');
|
||||
}
|
||||
@@ -307,6 +313,9 @@ export class AdminRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in updateRecipeStatus:', { error, recipeId, status });
|
||||
throw new Error('Failed to update recipe status.');
|
||||
}
|
||||
@@ -479,7 +488,7 @@ export class AdminRepository {
|
||||
[status, receiptId]
|
||||
);
|
||||
if (res.rowCount === 0) {
|
||||
throw new Error(`Receipt with ID ${receiptId} not found.`);
|
||||
throw new NotFoundError(`Receipt with ID ${receiptId} not found.`);
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
@@ -518,6 +527,9 @@ export class AdminRepository {
|
||||
if (error instanceof Error && 'code' in error && error.code === '23503') {
|
||||
throw new ForeignKeyConstraintError('The specified user does not exist.');
|
||||
}
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
throw error; // Re-throw to be handled by the route
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,8 +162,11 @@ export class FlyerRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in getFlyerById:', { error, flyerId });
|
||||
throw new Error('Failed to retrieve flyer from database.');
|
||||
throw new Error(`Failed to retrieve flyer from database. Original error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,11 +286,14 @@ export class FlyerRepository {
|
||||
// The database will handle deleting associated flyer_items, unmatched_flyer_items, etc.
|
||||
const res = await client.query('DELETE FROM public.flyers WHERE flyer_id = $1', [flyerId]);
|
||||
if (res.rowCount === 0) {
|
||||
throw new Error(`Flyer with ID ${flyerId} not found.`);
|
||||
throw new NotFoundError(`Flyer with ID ${flyerId} not found.`);
|
||||
}
|
||||
await client.query('COMMIT');
|
||||
logger.info(`Successfully deleted flyer with ID: ${flyerId}`);
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error; // Propagate NotFoundError without wrapping it
|
||||
}
|
||||
await client.query('ROLLBACK');
|
||||
logger.error('Database transaction error in deleteFlyer:', { error, flyerId });
|
||||
throw new Error('Failed to delete flyer.');
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/services/db/notification.db.ts
|
||||
import type { Pool, PoolClient } from 'pg';
|
||||
import { getPool } from './connection.db';
|
||||
import { ForeignKeyConstraintError } from './errors.db';
|
||||
import { ForeignKeyConstraintError, NotFoundError } from './errors.db';
|
||||
import { logger } from '../logger.server';
|
||||
import { Notification } from '../../types';
|
||||
|
||||
@@ -119,21 +119,21 @@ export class NotificationRepository {
|
||||
* @throws An error if the notification is not found or does not belong to the user.
|
||||
*/
|
||||
async markNotificationAsRead(notificationId: number, userId: string): Promise<Notification> {
|
||||
try {
|
||||
const res = await this.db.query<Notification>(
|
||||
`UPDATE public.notifications SET is_read = true WHERE notification_id = $1 AND user_id = $2 RETURNING *`,
|
||||
[notificationId, userId]
|
||||
);
|
||||
if (res.rowCount === 0) {
|
||||
throw new Error('Notification not found or user does not have permission.');
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.startsWith('Notification not found')) throw error;
|
||||
logger.error('Database error in markNotificationAsRead:', { error, notificationId, userId });
|
||||
throw new Error('Failed to mark notification as read.');
|
||||
}
|
||||
try {
|
||||
const res = await this.db.query<Notification>(
|
||||
`UPDATE public.notifications SET is_read = true WHERE notification_id = $1 AND user_id = $2 RETURNING *`,
|
||||
[notificationId, userId]
|
||||
);
|
||||
if (res.rowCount === 0) {
|
||||
throw new NotFoundError('Notification not found or user does not have permission.');
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) throw error;
|
||||
logger.error('Database error in markNotificationAsRead:', { error, notificationId, userId });
|
||||
throw new Error('Failed to mark notification as read.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes notifications that are older than a specified number of days.
|
||||
|
||||
@@ -87,6 +87,9 @@ export class RecipeRepository {
|
||||
);
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof Error && 'code' in error && error.code === '23503') {
|
||||
throw new ForeignKeyConstraintError('The specified user or recipe does not exist.');
|
||||
}
|
||||
logger.error('Database error in addFavoriteRecipe:', { error, userId, recipeId });
|
||||
throw new Error('Failed to add favorite recipe.');
|
||||
}
|
||||
@@ -104,6 +107,9 @@ export class RecipeRepository {
|
||||
throw new NotFoundError('Favorite recipe not found for this user.');
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in removeFavoriteRecipe:', { error, userId, recipeId });
|
||||
throw new Error('Failed to remove favorite recipe.');
|
||||
}
|
||||
@@ -209,6 +215,9 @@ export class RecipeRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in getRecipeById:', { error, recipeId });
|
||||
throw new Error('Failed to retrieve recipe.');
|
||||
}
|
||||
|
||||
@@ -117,6 +117,9 @@ export class ShoppingRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in getShoppingListById:', { error, listId, userId });
|
||||
throw new Error('Failed to retrieve shopping list.');
|
||||
}
|
||||
|
||||
@@ -220,6 +220,9 @@ export class UserRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in findUserProfileById:', { error });
|
||||
throw new Error('Failed to retrieve user profile from database.');
|
||||
}
|
||||
@@ -263,6 +266,9 @@ export class UserRepository {
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in updateUserProfile:', { error });
|
||||
throw new Error('Failed to update user profile in database.');
|
||||
}
|
||||
@@ -285,11 +291,13 @@ export class UserRepository {
|
||||
[preferences, userId]
|
||||
);
|
||||
if (res.rowCount === 0) {
|
||||
throw new Error('User not found or user does not have permission to update.');
|
||||
throw new NotFoundError('User not found or user does not have permission to update.');
|
||||
}
|
||||
return res.rows[0];
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('User not found')) throw error;
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
logger.error('Database error in updateUserPreferences:', { error });
|
||||
throw new Error('Failed to update user preferences in database.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user