refactor: user-settings module

This commit is contained in:
Mir Arif Hasan
2022-12-23 12:28:22 +06:00
parent 877532559e
commit 9b5734f2ff
6 changed files with 74 additions and 71 deletions

View File

@@ -161,6 +161,19 @@ export const TEAM_ENVIRONMMENT_NOT_FOUND =
export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER =
'team_environment/not_team_member' as const;
/**
* User setting not found for a user
* (UserSettingsService)
*/
export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const;
/**
* User setting invalid (null) properties
* (UserSettingsService)
*/
export const USER_SETTINGS_INVALID_PROPERTIES = 'user_settings/invalid_properties' as const;
/*
|------------------------------------|
@@ -215,21 +228,3 @@ export const BUG_TEAM_ENV_GUARD_NO_REQUIRE_ROLES =
*/
export const BUG_TEAM_ENV_GUARD_NO_ENV_ID =
'bug/team_env/guard_no_env_id' as const;
/**
* User settings update failed
* (UserSettingsService)
*/
export const USER_SETTINGS_UPDATE_FAILED = 'user_settings/update_failed' as const;
/**
* User settings not found
* (UserSettingsService)
*/
export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const;
/**
* User settings invalid properties
* (UserSettingsService)
*/
export const USER_SETTINGS_INVALID_PROPERTIES = 'user_settings/invalid_properties' as const;

View File

@@ -3,22 +3,22 @@ import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class UserSettings {
@Field(() => ID, {
description: 'ID of the User Settings',
description: 'ID of the User Setting',
})
id: string;
@Field(() => ID, {
description: 'ID of the user this settings belongs to',
description: 'ID of the user this setting belongs to',
})
userUid: string;
@Field({
description: 'All properties present in the settings',
description: 'Stringified JSON settings object',
})
properties: string; // JSON string of the properties object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client
@Field({
description: 'Last updated date-time of the settings',
description: 'Last updated on',
})
updatedOn: Date;
}

View File

@@ -19,14 +19,14 @@ export class UserSettingsResolver {
/* Mutations */
@Mutation(() => UserSettings, {
description: 'Creates a new user settings for given user',
description: 'Creates a new user setting for a given user',
})
@UseGuards(GqlAuthGuard)
async createUserSettings(
@GqlUser() user: User,
@Args({
name: 'properties',
description: 'JSON string of properties object',
description: 'Stringified JSON settings object',
})
properties: string,
) {
@@ -40,14 +40,14 @@ export class UserSettingsResolver {
}
@Mutation(() => UserSettings, {
description: 'Update user settings for given user',
description: 'Update user setting for a given user',
})
@UseGuards(GqlAuthGuard)
async updateUserSettings(
@GqlUser() user: User,
@Args({
name: 'properties',
description: 'JSON string of properties object',
description: 'Stringified JSON settings object',
})
properties: string,
) {
@@ -61,7 +61,7 @@ export class UserSettingsResolver {
/* Subscriptions */
@Subscription(() => UserSettings, {
description: 'Listen for user setting updating',
description: 'Listen for user setting updates',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)

View File

@@ -6,8 +6,10 @@ import {
JSON_INVALID,
USER_NOT_FOUND,
USER_SETTINGS_INVALID_PROPERTIES,
USER_SETTINGS_UPDATE_FAILED,
USER_SETTINGS_NOT_FOUND,
} from 'src/errors';
import { UserSettings } from './user-settings.model';
import { User } from 'src/user/user.model';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
@@ -19,16 +21,16 @@ const userSettingsService = new UserSettingsService(
mockPubSub as any,
);
const user = {
const user: User = {
uid: 'user-uid',
displayName: 'user-display-name',
email: 'user-email',
photoURL: 'user-photo-url',
};
const userSettings = {
const userSettings: UserSettings = {
id: '1',
userUid: user.uid,
properties: { key: 'k', value: 'v' },
properties: JSON.stringify({ key: 'k', value: 'v' }),
updatedOn: new Date('2022-12-19T12:43:18.635Z'),
};
@@ -39,7 +41,7 @@ beforeEach(() => {
describe('UserSettingsService', () => {
describe('createUserSettings', () => {
test('should create a user settings with valid user and properties', async () => {
test('should create a user setting with valid user and properties', async () => {
mockPrisma.userSettings.create.mockResolvedValue(userSettings);
const result = await userSettingsService.createUserSettings(
@@ -76,7 +78,7 @@ describe('UserSettingsService', () => {
});
});
describe('updateUserSettings', () => {
test('should update a user settings for valid user and properties', async () => {
test('should update a user setting for valid user and properties', async () => {
mockPrisma.userSettings.update.mockResolvedValue(userSettings);
const result = await userSettingsService.updateUserSettings(
@@ -94,9 +96,9 @@ describe('UserSettingsService', () => {
null as any,
JSON.stringify(userSettings.properties),
);
expect(result).toEqualLeft(USER_SETTINGS_UPDATE_FAILED);
expect(result).toEqualLeft(USER_SETTINGS_NOT_FOUND);
});
test('should reject for invalid properties', async () => {
test('should reject for invalid stringified JSON properties', async () => {
const result = await userSettingsService.updateUserSettings(
user,
'invalid-properties',
@@ -110,7 +112,7 @@ describe('UserSettingsService', () => {
);
expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES);
});
test('should publish message on pubnub after update successfully', async () => {
test('should publish message over pubsub on successful update', async () => {
mockPrisma.userSettings.update.mockResolvedValue(userSettings);
await userSettingsService.updateUserSettings(

View File

@@ -9,7 +9,6 @@ import {
USER_NOT_FOUND,
USER_SETTINGS_INVALID_PROPERTIES,
USER_SETTINGS_NOT_FOUND,
USER_SETTINGS_UPDATE_FAILED,
} from 'src/errors';
@Injectable()
@@ -19,83 +18,90 @@ export class UserSettingsService {
private readonly pubsub: PubSubService,
) {}
/**
* Fetch user setting for a given user
* @param user User object
* @returns an Either of `UserSettings` or error
*/
async fetchUserSettings(user: User) {
try {
const dbUserSettings = await this.prisma.userSettings.findUnique({
const userSettings = await this.prisma.userSettings.findUniqueOrThrow({
where: { userUid: user.uid },
rejectOnNotFound: true,
});
const userSettings: UserSettings = {
id: dbUserSettings.id,
userUid: dbUserSettings.userUid,
properties: JSON.stringify(dbUserSettings.properties),
updatedOn: dbUserSettings.updatedOn,
const settings: UserSettings = {
...userSettings,
properties: JSON.stringify(userSettings.properties),
};
return E.right(userSettings);
return E.right(settings);
} catch (e) {
return E.left(USER_SETTINGS_NOT_FOUND);
}
}
/**
* Create user setting for a given user
* @param user User object
* @param properties User setting properties
* @returns an Either of `UserSettings` or error
*/
async createUserSettings(user: User, properties: string) {
if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES);
const jsonProperties = stringToJson(properties);
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left);
const settingsObject = stringToJson(properties);
if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
try {
const dbUserSettings = await this.prisma.userSettings.create({
const userSettings = await this.prisma.userSettings.create({
data: {
properties: jsonProperties.right,
properties: settingsObject.right,
userUid: user.uid,
},
});
const userSettings: UserSettings = {
id: dbUserSettings.id,
userUid: dbUserSettings.userUid,
properties,
updatedOn: dbUserSettings.updatedOn,
const settings: UserSettings = {
...userSettings,
properties: JSON.stringify(userSettings.properties),
};
return E.right(userSettings);
return E.right(settings);
} catch (e) {
return E.left(USER_NOT_FOUND);
}
}
/**
* Update user setting for a given user
* @param user User object
* @param properties
* @returns
*/
async updateUserSettings(user: User, properties: string) {
if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES);
const jsonProperties = stringToJson(properties);
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left);
const settingsObject = stringToJson(properties);
if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
try {
const dbUpdatedUserSettings = await this.prisma.userSettings.update({
const updatedUserSettings = await this.prisma.userSettings.update({
where: { userUid: user.uid },
data: {
properties: jsonProperties.right,
properties: settingsObject.right,
},
});
const updatedUserSettings: UserSettings = {
id: dbUpdatedUserSettings.id,
userUid: dbUpdatedUserSettings.userUid,
properties,
updatedOn: dbUpdatedUserSettings.updatedOn,
const settings: UserSettings = {
...updatedUserSettings,
properties: JSON.stringify(updatedUserSettings.properties),
};
// Publish subscription for environment creation
await this.pubsub.publish(
`user_settings/${user.uid}/updated`,
updatedUserSettings,
);
await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);
return E.right(updatedUserSettings);
return E.right(settings);
} catch (e) {
return E.left(USER_SETTINGS_UPDATE_FAILED);
return E.left(USER_SETTINGS_NOT_FOUND);
}
}
}

View File

@@ -12,7 +12,7 @@ export class UserSettingsUserResolver {
@ResolveField(() => UserSettings, {
description: 'Returns user settings',
})
async settings(@Parent() user: User): Promise<UserSettings | string> {
async settings(@Parent() user: User) {
const userSettings = await this.userSettingsService.fetchUserSettings(user);
if (E.isLeft(userSettings)) throwErr(userSettings.left);