Nest.js Code Refactoring

63 views
Skip to first unread message

Dream Catcher

unread,
Oct 7, 2023, 11:14:06 AM10/7/23
to Clean Code Discussion
I want to refactor these lines. Any advice would be appreciated.
```
@Controller('monday')
export class MondayController {
  constructor(private readonly mondayService: MondayService) {}

 
  @Get()
  async getPortals(@AuthContext() { user }: Passport) {
    return this.mondayService.getPortals(user.id);
  }

  @Get('projects/all')
  @UseGuards(IsAuth)
  async getAllProjects(@AuthContext() { user }: Passport) {
    return this.mondayService.getAllProjects(user.id);
  }
}

@Controller('zoho')
export class ZohoController {
  constructor(private readonly zohoService: ZohoService) {}

  @Get()
  async getPortals(@AuthContext() { user }: Passport) {
    return this.zohoService.getPortals(user.id);
  }

  @Get('projects/all')
  @UseGuards(IsAuth)
  async getAllProjects(@AuthContext() { user }: Passport) {
    return this.zohoService.getAllProjects(user.id);
}


@Injectable()
@UseGuards(IsAuth)
export class ZohoService {
  private readonly baseUrl = 'https://projectsapi.zoho.com.location/restapi';

  constructor(
    @Inject(forwardRef(() => ZohoAuthService))
    private readonly zohoAuthService: ZohoAuthService,
  ) {}


  async getPortals(userId: string): Promise<AxiosResponse<any>> {
    const MAX_RETRY = 2;
    let retryCount = 0;
    ... content A
 }
}

@Injectable()
@UseGuards(IsAuth)
export class MondayService {

  constructor(
    @Inject(forwardRef(() => MondayAuthService))
    private readonly mondayAuthService: MondayAuthService,
  ) {}

  async getPortals(userId: string): Promise<AxiosResponse<any>> {
    const MAX_RETRY = 2;
    let retryCount = 0;
    ... content B
  }
}
```
Reply all
Reply to author
Forward
0 new messages