Skip to content

Commit

Permalink
refactor(user-settings): consolidate user settings structure and remo…
Browse files Browse the repository at this point in the history
…ve deprecated fields
  • Loading branch information
Junjiequan committed Sep 18, 2024
1 parent b1d47bd commit 53daa02
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 58 deletions.
8 changes: 3 additions & 5 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,12 @@ export class AuthService {

if (!userSettings) {
Logger.log(
`Adding default settings to user ${user.username}`,
"UserSettingsInterceptor",
`Adding default settings to user ${user.username} with userId: ${user._id}`,
"postLoginTasks",
);
const createUserSettingsDto: CreateUserSettingsDto = {
userId,
filters: [],
conditions: [],
columns: [],
frontendSettings: {},
};
await this.usersService.createUserSettings(userId, createUserSettingsDto);
}
Expand Down
22 changes: 9 additions & 13 deletions src/users/dto/update-user-settings.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { ApiProperty, PartialType } from "@nestjs/swagger";
import { ScientificCondition } from "../schemas/user-settings.schema";
import { IsArray, IsNumber } from "class-validator";
import { IsNumber, IsObject, IsOptional } from "class-validator";

export class UpdateUserSettingsDto {
@ApiProperty()
@IsArray()
readonly columns: Record<string, unknown>[];

@ApiProperty({ type: Number, required: false, default: 25 })
@IsNumber()
readonly datasetCount?: number;
Expand All @@ -15,13 +10,14 @@ export class UpdateUserSettingsDto {
@IsNumber()
readonly jobCount?: number;

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

@ApiProperty()
@IsArray()
readonly conditions: ScientificCondition[];
@ApiProperty({
type: "object",
additionalProperties: { type: "array", items: {} },
required: false,
})
@IsOptional()
@IsObject()
readonly frontendSettings?: Record<string, unknown[]>;
}

export class PartialUpdateUserSettingsDto extends PartialType(
Expand Down
30 changes: 6 additions & 24 deletions src/users/schemas/user-settings.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ export class UserSettings {

id?: string;

@ApiProperty({
type: [Object],
default: [],
description: "Array of the users preferred columns in dataset table",
})
@Prop({ type: [Object], default: [] })
columns: Record<string, unknown>[];

@ApiProperty({
type: Number,
default: 25,
Expand All @@ -52,23 +44,13 @@ export class UserSettings {
userId: string;

@ApiProperty({
type: [Object],
default: [],
description: "Array of filters the user has set",
})
@Prop({
type: [{ type: Object }],
default: [],
})
filters: Record<string, unknown>[];

@ApiProperty({
type: [Object],
default: [],
description: "Array of conditions the user has set",
type: "object",
additionalProperties: { type: "array", items: {} },
default: {},
description: "users preferred ui settings in dataset table",
})
@Prop({ type: [{ type: Object }], default: [] })
conditions: ScientificCondition[];
@Prop({ type: Object, default: {}, required: false })
frontendSettings?: Record<string, unknown[]>;
}

export const UserSettingsSchema = SchemaFactory.createForClass(UserSettings);
44 changes: 28 additions & 16 deletions src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UsersController } from "./users.controller";
import { UsersService } from "./users.service";
import { UpdateUserSettingsDto } from "./dto/update-user-settings.dto";
import { Request } from "express";
import { UserSettings } from "./schemas/user-settings.schema";

class UsersServiceMock {
findByIdUserIdentity(id: string) {
Expand All @@ -26,11 +27,13 @@ class UsersServiceMock {
const mockUserSettings = {
_id: "user1",
userId: "user1",
columns: [],
datasetCount: 25,
jobCount: 25,
filters: [{ LocationFilter: true }, { PidFilter: true }],
conditions: [{ field: "status", value: "active", operator: "equals" }],
frontendSettings: {
filters: [{ LocationFilter: true }, { PidFilter: true }],
conditions: [{ field: "status", value: "active", operator: "equals" }],
columns: [],
},
};

class AuthServiceMock {}
Expand Down Expand Up @@ -72,11 +75,11 @@ describe("UsersController", () => {
const result = await controller.getSettings(mockRequest as Request, userId);

// Assert
expect(result).toEqual(mockUserSettings);
expect(result?.filters).toBeDefined();
expect(result?.filters.length).toBeGreaterThan(0);
expect(result?.conditions).toBeDefined();
expect(result?.conditions.length).toBeGreaterThan(0);
expect(result?.frontendSettings).toEqual(mockUserSettings);
expect(result?.frontendSettings?.filters).toBeDefined();
expect(result?.frontendSettings?.filters.length).toBeGreaterThan(0);
expect(result?.frontendSettings?.conditions).toBeDefined();
expect(result?.frontendSettings?.conditions.length).toBeGreaterThan(0);
});

it("should update user settings with filters and conditions", async () => {
Expand All @@ -85,17 +88,26 @@ describe("UsersController", () => {

const updatedSettings = {
...mockUserSettings,
filters: [{ PidFilter: true }],
conditions: [{ field: "status", value: "inactive", operator: "equals" }],
frontendSettings: {
filters: [{ PidFilter: true }],
conditions: [
{ field: "status", value: "inactive", operator: "equals" },
],
columns: [],
},
};

const mockRequest: Partial<Request> = {
user: { _id: userId },
};

const expectedResponse = {
const expectedResponse: UserSettings = {
...updatedSettings,
_id: userId,
userId: userId, // Ensure all required properties are included
datasetCount: updatedSettings.datasetCount,
jobCount: updatedSettings.jobCount,
frontendSettings: updatedSettings.frontendSettings,
};

jest
Expand All @@ -108,10 +120,10 @@ describe("UsersController", () => {
updatedSettings,
);

expect(result).toEqual(updatedSettings);
expect(result?.filters).toBeDefined();
expect(result?.filters.length).toBe(1);
expect(result?.conditions).toBeDefined();
expect(result?.conditions.length).toBe(1);
expect(result?.frontendSettings).toEqual(updatedSettings);
expect(result?.frontendSettings?.filters).toBeDefined();
expect(result?.frontendSettings?.filters.length).toBe(1);
expect(result?.frontendSettings?.conditions).toBeDefined();
expect(result?.frontendSettings?.conditions.length).toBe(1);
});
});

0 comments on commit 53daa02

Please sign in to comment.