Skip to content

Commit

Permalink
removed all user filter settings related logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Junjiequan committed Sep 18, 2024
1 parent f0afcaa commit b1d47bd
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 60 deletions.
35 changes: 0 additions & 35 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import { Issuer } from "openid-client";
import { ReturnedAuthLoginDto } from "./dto/returnedLogin.dto";
import { ReturnedUserDto } from "src/users/dto/returned-user.dto";
import { CreateUserSettingsDto } from "src/users/dto/create-user-settings.dto";
import {
FilterComponentType,
UserSettings,
} from "src/users/schemas/user-settings.schema";
import { UpdateUserSettingsDto } from "src/users/dto/update-user-settings.dto";

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -134,8 +129,6 @@ export class AuthService {
*
* - Checks if the user has userSettings record.
* - If user has no userSetting, it creates default userSetting for the user.
* - If userSetting exist but are invalid (filters does not belong to FilterComponentType), it resets the filters to default - empty array.
*
* @param user - The logged-in user (without password).
*/
async postLoginTasks(user: Omit<User, "password">) {
Expand All @@ -157,34 +150,6 @@ export class AuthService {
columns: [],
};
await this.usersService.createUserSettings(userId, createUserSettingsDto);
} else {
const isValidFilters = (userSettings: UserSettings): boolean => {
if (userSettings.filters.length === 0) {
return true;
}
return userSettings.filters.every((filter) => {
const [key, value] = Object.entries(filter)[0];
return (
Object.keys(FilterComponentType).includes(key) &&
typeof value === "boolean"
);
});
};

if (!isValidFilters) {
Logger.log(
`Reset default settings to user ${user.username}`,
"UserSettingsInterceptor",
);
const updateUserSettingsDto: UpdateUserSettingsDto = {
...userSettings,
filters: [],
};
await this.usersService.findOneAndUpdateUserSettings(
userId,
updateUserSettingsDto,
);
}
}
}
}
7 changes: 2 additions & 5 deletions src/users/dto/update-user-settings.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ApiProperty, PartialType } from "@nestjs/swagger";
import {
FilterConfig,
ScientificCondition,
} from "../schemas/user-settings.schema";
import { ScientificCondition } from "../schemas/user-settings.schema";
import { IsArray, IsNumber } from "class-validator";

export class UpdateUserSettingsDto {
Expand All @@ -20,7 +17,7 @@ export class UpdateUserSettingsDto {

@ApiProperty()
@IsArray()
readonly filters: FilterConfig[];
readonly filters: Record<string, unknown>[];

@ApiProperty()
@IsArray()
Expand Down
20 changes: 1 addition & 19 deletions src/users/schemas/user-settings.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ import { Document } from "mongoose";

export type UserSettingsDocument = UserSettings & Document;

// NOTE: PidFilterContains and PidFilterStartsWith filters are not implemented
export enum FilterComponentType {
LocationFilter = "LocationFilter",
PidFilter = "PidFilter",
PidFilterContains = "PidFilterContains",
PidFilterStartsWith = "PidFilterStartsWith",
GroupFilter = "GroupFilter",
TypeFilter = "TypeFilter",
KeywordFilter = "KeywordFilter",
DateRangeFilter = "DateRangeFilter",
TextFilter = "TextFilter",
}

// NOTE: The key is one of FilterComponentType
export type FilterConfig = Partial<{
[K in FilterComponentType]: boolean;
}>;

// Define the Condition interface
export interface ScientificCondition {
field: string;
Expand Down Expand Up @@ -78,7 +60,7 @@ export class UserSettings {
type: [{ type: Object }],
default: [],
})
filters: FilterConfig[];
filters: Record<string, unknown>[];

@ApiProperty({
type: [Object],
Expand Down
1 change: 0 additions & 1 deletion src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { JwtService, JwtSignOptions } from "@nestjs/jwt";
import { JWTUser } from "../auth/interfaces/jwt-user.interface";
import * as fs from "fs";
import {
FilterComponentType,
UserSettings,
UserSettingsDocument,
} from "./schemas/user-settings.schema";
Expand Down

0 comments on commit b1d47bd

Please sign in to comment.