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 ReadingService from ReadingModule so other modules can use it.
  • Import ReadingModule in FriendsModule to access reading data.
  • Add findAllForUser(userId) in ReadingService to fetch a user’s sessions.
  • Enforce friendship in FriendsService.getFriendProgress(...).
  • Expose GET /friends/:friendId/progress in FriendsController.

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:

TypeScript
// In reading.module.ts
@Module({
  providers: [ReadingService, ...],
  exports: [ReadingService], // Make ReadingService available to other modules
  ...
})
export class ReadingModule {}

// In friends.module.ts
@Module({
  imports: [ReadingModule], // Import ReadingModule to use ReadingService
  providers: [FriendsService, ...],
  ...
})
export class FriendsModule {}

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

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal