Skip to content

Commit

Permalink
changed tap to swtichMap for asynchronous events for user-settings in…
Browse files Browse the repository at this point in the history
…terceptor.

moved user-settings interceptor to login endpoint
  • Loading branch information
Junjiequan committed Sep 18, 2024
1 parent c32b216 commit e476c40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions src/users/interceptors/user-settings.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Logger,
NestInterceptor,
} from "@nestjs/common";
import { Observable, tap } from "rxjs";
import { Observable, switchMap } from "rxjs";
import { CreateUserSettingsDto } from "../dto/create-user-settings.dto";
import { UsersService } from "../users.service";
import {
Expand All @@ -17,20 +17,22 @@ import { UpdateUserSettingsDto } from "../dto/update-user-settings.dto";
@Injectable()
export class UserSettingsInterceptor implements NestInterceptor {
constructor(private usersService: UsersService) {}
async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<unknown>> {

intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const res = context.switchToHttp().getResponse();
const user = res.req.user;

if (!user) {
return next.handle();
}

const userId = user._id;

return next.handle().pipe(
tap(async () => {
const res = context.switchToHttp().getResponse();
const user = res.req.user;
if (!user) {
return;
}
const userId = user._id;
switchMap(async (payload) => {
const userSettings =
await this.usersService.findByIdUserSettings(userId);

if (!userSettings) {
Logger.log(
`Adding default settings to user ${user.username}`,
Expand All @@ -42,7 +44,7 @@ export class UserSettingsInterceptor implements NestInterceptor {
conditions: [],
columns: [],
};
return this.usersService.createUserSettings(
await this.usersService.createUserSettings(
userId,
createUserSettingsDto,
);
Expand All @@ -54,15 +56,13 @@ export class UserSettingsInterceptor implements NestInterceptor {
`Reset default settings to user ${user.username}`,
"UserSettingsInterceptor",
);
return await this.resetUserSettings(userId, userSettings);
await this.resetUserSettings(userId, userSettings);
}
}

return;
return payload;
}),
);
}

private async resetUserSettings(userId: string, userSettings: UserSettings) {
// NOTE: The extra functions ensure filters in user setting record match the FilterComponentType format.
// If not, reset the user settings to maintain consistency.
Expand Down
2 changes: 1 addition & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class UsersController {

@ApiBody({ type: CredentialsDto })
@AllowAny()
@UseInterceptors(UserSettingsInterceptor)
@UseGuards(LocalAuthGuard)
@Post("login")
@ApiOperation({
Expand Down Expand Up @@ -232,7 +233,6 @@ export class UsersController {
}

@UseGuards(AuthenticatedPoliciesGuard)
@UseInterceptors(UserSettingsInterceptor)
@CheckPolicies(
"users",
(ability: AppAbility) =>
Expand Down

0 comments on commit e476c40

Please sign in to comment.