diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 2acdbccc8..fa201b7b5 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -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); } diff --git a/src/users/dto/update-user-settings.dto.ts b/src/users/dto/update-user-settings.dto.ts index d0159c39b..a9cca316e 100644 --- a/src/users/dto/update-user-settings.dto.ts +++ b/src/users/dto/update-user-settings.dto.ts @@ -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[]; - @ApiProperty({ type: Number, required: false, default: 25 }) @IsNumber() readonly datasetCount?: number; @@ -15,13 +10,14 @@ export class UpdateUserSettingsDto { @IsNumber() readonly jobCount?: number; - @ApiProperty() - @IsArray() - readonly filters: Record[]; - - @ApiProperty() - @IsArray() - readonly conditions: ScientificCondition[]; + @ApiProperty({ + type: "object", + additionalProperties: { type: "array", items: {} }, + required: false, + }) + @IsOptional() + @IsObject() + readonly frontendSettings?: Record; } export class PartialUpdateUserSettingsDto extends PartialType( diff --git a/src/users/schemas/user-settings.schema.ts b/src/users/schemas/user-settings.schema.ts index b6ebf6a7d..9a629abe7 100644 --- a/src/users/schemas/user-settings.schema.ts +++ b/src/users/schemas/user-settings.schema.ts @@ -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[]; - @ApiProperty({ type: Number, default: 25, @@ -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[]; - - @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; } export const UserSettingsSchema = SchemaFactory.createForClass(UserSettings); diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index 1d8f16b33..580e154cb 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -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) { @@ -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 {} @@ -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 () => { @@ -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 = { 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 @@ -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); }); });