Skip to content

Commit

Permalink
refactor(user-settings): update user settings structure and remove de…
Browse files Browse the repository at this point in the history
…precated fields

This commit refactors the user settings structure by updating the field names and removing deprecated fields. The `frontendSettings` field has been renamed to `externalSettings` to better reflect its purpose. Additionally, the deprecated fields have been removed from the user settings schema.
  • Loading branch information
Junjiequan committed Sep 18, 2024
1 parent 53daa02 commit 0e73c70
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class AuthService {
);
const createUserSettingsDto: CreateUserSettingsDto = {
userId,
frontendSettings: {},
externalSettings: {},
};
await this.usersService.createUserSettings(userId, createUserSettingsDto);
}
Expand Down
6 changes: 3 additions & 3 deletions src/users/dto/update-user-settings.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export class UpdateUserSettingsDto {
readonly jobCount?: number;

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

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

@ApiProperty({
type: "object",
additionalProperties: { type: "array", items: {} },
default: {},
description: "users preferred ui settings in dataset table",
description:
"A customizable object for storing the user's external settings, which can contain various nested properties and configurations.",
})
@Prop({ type: Object, default: {}, required: false })
frontendSettings?: Record<string, unknown[]>;
externalSettings?: Record<string, unknown>;
}

export const UserSettingsSchema = SchemaFactory.createForClass(UserSettings);
35 changes: 22 additions & 13 deletions src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const mockUserSettings = {
userId: "user1",
datasetCount: 25,
jobCount: 25,
frontendSettings: {
externalSettings: {
filters: [{ LocationFilter: true }, { PidFilter: true }],
conditions: [{ field: "status", value: "active", operator: "equals" }],
columns: [],
Expand Down Expand Up @@ -75,11 +75,15 @@ describe("UsersController", () => {
const result = await controller.getSettings(mockRequest as Request, userId);

// Assert
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);
expect(result?.externalSettings).toEqual(mockUserSettings);
expect(result?.externalSettings?.filters).toBeDefined();
expect(
(result?.externalSettings?.filters as Record<string, unknown>).length,
).toBeGreaterThan(0);
expect(result?.externalSettings?.conditions).toBeDefined();
expect(
(result?.externalSettings?.conditions as Record<string, unknown>).length,
).toBeGreaterThan(0);
});

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

const updatedSettings = {
...mockUserSettings,
frontendSettings: {
externalSettings: {
filters: [{ PidFilter: true }],
conditions: [
{ field: "status", value: "inactive", operator: "equals" },
Expand All @@ -107,7 +111,7 @@ describe("UsersController", () => {
userId: userId, // Ensure all required properties are included
datasetCount: updatedSettings.datasetCount,
jobCount: updatedSettings.jobCount,
frontendSettings: updatedSettings.frontendSettings,
externalSettings: updatedSettings.externalSettings,
};

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

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);
expect(result?.externalSettings).toEqual(updatedSettings);
expect(result?.externalSettings?.filters).toBeDefined();
expect(
(result?.externalSettings?.filters as Record<string, unknown[]>).length,
).toBe(1);
expect(result?.externalSettings?.conditions).toBeDefined();
expect(
(result?.externalSettings?.conditions as Record<string, unknown[]>)
.length,
).toBe(1);
});
});
28 changes: 26 additions & 2 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,43 @@ export class UsersController {
async patchSettings(
@Req() request: Request,
@Param("id") id: string,
updateUserSettingsDto: PartialUpdateUserSettingsDto,
@Body() updateUserSettingsDto: PartialUpdateUserSettingsDto,
): Promise<UserSettings | null> {
await this.checkUserAuthorization(
request,
[Action.UserUpdateAny, Action.UserUpdateOwn],
id,
);
return this.usersService.findOneAndUpdateUserSettings(
return this.usersService.findOneAndPatchUserSettings(
id,
updateUserSettingsDto,
);
}

@UseGuards(AuthenticatedPoliciesGuard)
@CheckPolicies(
"users",
(ability: AppAbility) =>
ability.can(Action.UserUpdateOwn, User) ||
ability.can(Action.UserUpdateAny, User),
)
@Patch("/:id/settings/external")
async patchExternalSettings(
@Req() request: Request,
@Param("id") id: string,
@Body() externalSettings: Record<string, unknown>,
): Promise<UserSettings | null> {
await this.checkUserAuthorization(
request,
[Action.UserUpdateAny, Action.UserUpdateOwn],
id,
);
return this.usersService.findOneAndPatchUserExternalSettings(
id,
externalSettings,
);
}

@UseGuards(AuthenticatedPoliciesGuard)
@CheckPolicies(
"users",
Expand Down
28 changes: 28 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,34 @@ export class UsersService implements OnModuleInit {
return result;
}

async findOneAndPatchUserSettings(
userId: string,
updateUserSettingsDto: UpdateUserSettingsDto | PartialUpdateUserSettingsDto,
): Promise<UserSettings | null> {
const result = await this.userSettingsModel
.findOneAndUpdate(
{ userId },
{ $set: updateUserSettingsDto },
{ new: true },
)
.exec();
return result;
}

async findOneAndPatchUserExternalSettings(
userId: string,
externalSettings: Record<string, unknown>,
): Promise<UserSettings | null> {
const updateQuery: Record<string, unknown> = {};

for (const [key, value] of Object.entries(externalSettings)) {
updateQuery[`externalSettings.${key}`] = value;
}
const result = await this.userSettingsModel
.findOneAndUpdate({ userId }, { $set: updateQuery }, { new: true })
.exec();
return result;
}
async findOneAndDeleteUserSettings(userId: string): Promise<unknown> {
return this.userSettingsModel.findOneAndDelete({ userId }).exec();
}
Expand Down

0 comments on commit 0e73c70

Please sign in to comment.