Skip to content

Be42 recipe platform community review overhaul#265

Merged
TienNguyen3711 merged 3 commits into
masterfrom
BE37-Recipe-Platform-Community-Review-Overhaul
May 12, 2026
Merged

Be42 recipe platform community review overhaul#265
TienNguyen3711 merged 3 commits into
masterfrom
BE37-Recipe-Platform-Community-Review-Overhaul

Conversation

@datnq2001
Copy link
Copy Markdown
Collaborator

BE37: Recipe Platform & Community Review Overhaul API

Summary

This PR upgrades the backend recipe platform to support a more complete Recipe Library, Community Review, Admin Management, Import/Enrichment, and Role Management workflow.

The main focus of this PR is to:

  • Build recipe_library as the central recipe data model for public, private, community, moderation, trash, import, enrichment, and social features.
  • Add a new recipe_reviews system for both recipe library and community recipes.
  • Synchronise legacy recipes data to support community visibility and publishing flows.
  • Add admin user role management APIs under /api/admin/user-roles.
  • Improve authentication safety by refreshing user role and email from the database on every request.
  • Fix runtime issues that previously prevented the frontend from fetching data correctly, especially missing route mounts and profile/query edge cases.

1. New and Updated API Surface

Method Endpoint Auth Purpose
GET /api/recipe-library/public Public Get public recipe list
GET /api/recipe-library/community Public Get published community recipes
GET /api/recipe-library/my User Get current user’s recipe library
GET /api/recipe-library/add-meal User Get recipe source for Add Meal flow
POST /api/recipe-library User Create a private recipe in the library
GET /api/recipe-library/:id User Get recipe detail based on permission
PATCH /api/recipe-library/:id User Update recipe
DELETE /api/recipe-library/:id User/Admin Move recipe to Trash
POST /api/recipe-library/:id/share-community User Submit recipe for community review
POST /api/recipe-library/:id/like User Like recipe
POST /api/recipe-library/:id/save User Save recipe
POST /api/recipe-library/:id/comments User Add recipe comment
POST /api/recipe-library/:id/report User Report recipe
GET /api/recipe-library/admin Admin Get admin recipe library list
GET /api/recipe-library/admin/pending-community Admin Get pending community review queue
POST /api/recipe-library/admin/:id/approve-community Admin Approve community recipe
POST /api/recipe-library/admin/:id/reject-community Admin Reject community recipe
POST /api/recipe-library/admin/:id/publish-catalog Admin Publish catalog recipe
POST /api/recipe-library/admin/:id/unpublish-catalog Admin Unpublish catalog recipe
POST /api/recipe-library/admin/:id/recover Admin Recover recipe from Trash
DELETE /api/recipe-library/admin/:id/permanent-delete Admin Permanently delete recipe
POST /api/recipe-library/admin/import-names Admin Import dish names into queue
GET /api/recipe-library/admin/import-template Admin Download import template
POST /api/recipe-library/admin/import-file Admin Upload CSV/XLSX import file
POST /api/recipe-library/admin/import-rows Admin Import raw rows
GET /api/recipe-library/admin/import-queue Admin List import queue
PATCH /api/recipe-library/admin/import-queue/:id Admin Update queue row
POST /api/recipe-library/admin/import-queue/:id/trash Admin Move queue row to Trash
POST /api/recipe-library/admin/import-queue/:id/recover Admin Recover queue row
DELETE /api/recipe-library/admin/import-queue/:id Admin Permanently delete queue row
POST /api/recipe-library/admin/enrich-batch Admin Run batch AI enrichment
POST /api/recipe-library/admin/import-queue/approve Admin Approve queue rows into recipes
POST /api/recipe-library/admin/fetch-images Admin Fetch missing recipe images
GET /api/recipe-reviews/feed Public Get review feed
GET /api/recipe-reviews Public List reviews by source and recipe
POST /api/recipe-reviews/summary Public Get batch review summaries
POST /api/recipe-reviews User Create or update review
DELETE /api/recipe-reviews/:id Admin Hide review
GET /api/admin/user-roles Admin List users and roles
PATCH /api/admin/user-roles/:userId Admin Update user role
GET /api/recipe/admin/all Admin Get legacy user recipes with protected auth
PATCH /api/recipe/admin/:id/visibility Admin Update legacy recipe visibility with protected auth

2. Backend File Changes

File Type Main Changes
controller/recipeLibraryController.js New Added full controller for recipe library, import queue, moderation, trash, enrichment, and social actions.
controller/recipeReviewController.js New Added controller for recipe reviews, feed, summary, and hide review actions.
controller/adminUserRoleController.js New Added user role listing and role update logic, including guard to prevent admins from changing their own role.
services/recipeLibraryService.js New Added core business logic for recipe library, moderation, trash, enrichment, import, and counters.
services/recipeReviewService.js New Added review logic by source_type, aggregate summaries, and review feed.
routes/recipeLibrary.js New Added route mapping for recipe library APIs.
routes/recipeReviews.js New Added route mapping for recipe review APIs.
routes/adminUserRole.js New Added route mapping for admin user role APIs.
routes/index.js Updated Mounted /api/recipe-library, /api/recipe-reviews, and /api/admin.
routes/recipe.js Updated Added and adjusted admin/community recipe routes, with stronger admin auth protection.
routes/routeGroups.js Updated Added route groups for recipe library and recipe reviews.
controller/recipeController.js Major Updated Updated legacy recipe community flow including admin/community listing, share/unshare, visibility update, and notifications.
model/createRecipe.js Major Updated Added new default visibility user_private, saved ingredient cost, and improved image parsing/upload safety.
model/getUserRecipes.js Updated Standardised fetch helper and added support for reusable image lookup.
model/deleteUserRecipes.js Updated Added relationship cleanup, related recipe_library cleanup, and image cleanup.
model/mealPlan.js Updated Fixed query issue caused by non-existing recipe_meal column.
model/getUserProfile.js Updated Added resilient decrypt handling for mixed plaintext/encrypted legacy data.
model/updateUserProfile.js Updated Added the same resilient handling as getUserProfile to prevent API crashes.
controller/notificationController.js Updated Changed select/order field from created_at to timestamp.
routes/notifications.js Updated Added endpoint to mark all unread notifications as read.
services/apiResponseService.js Updated Added createdAt fallback from timestamp.
middleware/authenticateToken.js Updated Refreshes user role and email from DB on every request to prevent stale JWT role claims.
routes/systemRoutes.js Major Updated Added admin token/recovery key IP unblock flow and adjusted system audit routes.
services/securityEvents/securityResponseService.js Updated Added getActiveBlocks, unblockIp, and standardised IP blocking behaviour.
test/securityResponseService.test.js Updated Added tests for IP unblock logic.
technical_docs/recipe-library-flow.md New Added business flow documentation for recipe library.
database/*.sql New Added schema and migration scripts for recipe library, reviews, and visibility sync.
package.json, package-lock.json Updated Synced dependencies, test scripts, and runtime requirements.

3. Database Schema and Migration Changes

Script Target Changes
database/recipe_library_schema.sql recipe_library Created central recipe table with visibility, source, data status, moderation, trash, classification, nutrition, image, social, and AI metadata.
database/recipe_library_schema.sql recipe_library_import_queue Added import, enrichment, and lifecycle queue table.
database/recipe_library_schema.sql recipe_library_likes, recipe_library_saves, recipe_library_comments, recipe_library_reports Added social interaction tables and counter support.
database/recipe_library_schema.sql published_recipe_library view Added view for valid published recipes.
database/2026-05-08_sync_meal_type_constraints.sql recipe_library, recipe_library_import_queue Normalised legacy meal_type values to other and recreated constraints.
database/2026-05-08_sync_recipe_library_source_constraint.sql recipe_library Normalised source enum from admin_manual to admin_created.
database/2026-05-10_recipe_reviews.sql recipe_reviews Created review table, trigger, indexes, and RLS policy.
database/2026-05-10_sync_user_recipe_visibility.sql recipes legacy table Added visibility, is_published, published_at, backfill logic, and indexes.

4. Important Behaviour and Contract Changes

Area Before After
Recipe library routing Recipe library routes were not fully mounted in the app router. Routes are fully mounted and available for frontend calls.
Admin recipe endpoints Some admin endpoints existed but did not fully enforce admin auth at route level. Admin endpoints now require authenticateToken and authorizeRoles('admin').
Role authorisation Authorisation depended heavily on role claims inside JWT. Role and email are refreshed from the database on every request.
Review system No standard API or storage existed for recipe reviews. Full review API, review table, summary endpoint, and feed endpoint are added.
Notification timestamp Used created_at. Uses timestamp, with response fallback mapping.
Profile decrypt handling API could crash when legacy data contained mixed plaintext/encrypted values. Profile parse/decrypt is now safer and prevents API crashes.

5. Runtime Data Tables Affected

Table Impact
recipes Visibility and publish state are synced for the community flow.
recipe_library New central recipe data source.
recipe_library_import_queue Supports import and enrichment workflow.
recipe_library_likes, recipe_library_saves, recipe_library_comments, recipe_library_reports Stores social interaction signals.
recipe_reviews Stores recipe ratings and comments.
notifications Supports events for share, approve, reject, private, and read-all actions.
images, storage images Handles recipe image upload, resolve, and cleanup.
users, user_roles Used by auth middleware to refresh runtime roles.

6. Risks and Compatibility Notes

Risk Description Mitigation
Migration dependency SQL scripts must be applied in the correct order. Run migrations before rolling out the API.
Route contract changes Some old recipe routes may no longer behave the same way as before. Frontend has been moved to the new endpoints, but older clients should be rechecked.
Authorisation behaviour change Role changes can take effect immediately without waiting for JWT expiry. This is an intentional security improvement.
Local runtime artifacts .DS_Store, logs/, and uploads/... are local runtime files. These files should not be committed.

7. Suggested Testing Checklist

  • GET /api/recipe-library/public returns recipe data and count.
  • GET /api/recipe-library/admin returns 200 and list data with a valid admin token.
  • GET /api/recipe-library/admin returns 401/403 without a valid token.
  • POST /api/recipe-reviews/summary returns the correct summary map by key.
  • GET /api/admin/user-roles lists users and roles with correct filters.
  • PATCH /api/admin/user-roles/:userId updates user role successfully.
  • Admin cannot update their own role.
  • Legacy GET /api/recipe/admin/all is accessible only by admin.
  • Profile API no longer returns 500 with mixed legacy plaintext/encrypted data.
  • Notifications read-all endpoint marks unread notifications as read successfully.
  • Meal plan read flow no longer fails due to a non-existing column error.

8. Expected Outcome

After this PR, the backend provides a more complete, secure, and maintainable recipe platform foundation.

The system now supports centralised recipe library management, community publishing and moderation, recipe reviews, import and enrichment workflows, admin role management, improved auth safety, and stronger compatibility with the updated frontend recipe flows.

@TienNguyen3711 TienNguyen3711 changed the title Be37 recipe platform community review overhaul Be42 recipe platform community review overhaul May 12, 2026
Copy link
Copy Markdown
Collaborator

@TienNguyen3711 TienNguyen3711 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything works well on my device. Approved

@TienNguyen3711 TienNguyen3711 merged commit 8215229 into master May 12, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants