Tracking Friend Reading Progress
Introduction: Sharing Progress with Friends
Welcome back! In the last lessons, you learned how to create friendships between users and how to handle friend requests in your reading tracker app. Now, you will take the next step: allowing users to view the reading progress of their friends. This feature is important because it lets users share their achievements and stay motivated together, but it also needs to respect privacy. Only friends should be able to see each other’s reading progress. In this lesson, you will learn how to enforce this rule in your API.
Where This Lives & What We’ll Implement
You already have modules for users, friends, and reading progress. Users can send and accept friend requests, and each user’s reading sessions are stored in the database.
Now we will stitch together the friends and reading domains::
- Export
ReadingServicefromReadingModuleso other modules can use it. - Import
ReadingModuleinFriendsModuleto access reading data. - Add
findAllForUser(userId)inReadingServiceto fetch a user’s sessions. - Enforce friendship in
FriendsService.getFriendProgress(...). - Expose
GET /friends/:friendId/progressinFriendsController.
Connecting Modules: Using ReadingService in FriendsService
To let users view their friends’ reading progress, the friends module needs to access reading data. In NestJS, this is done by injecting the ReadingService into the FriendsService. However, for this to work, you must export the ReadingService from the ReadingModule and import the ReadingModule into the FriendsModule.
Here’s how this connection looks in code:
By exporting and importing the right modules, you allow the friends module to use the reading service’s methods. This is a common pattern in NestJS for sharing logic between modules.
Enforcing Friendship Before Sharing Progress
