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 = export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER =
'team_environment/not_team_member' as const; '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 = export const BUG_TEAM_ENV_GUARD_NO_ENV_ID =
'bug/team_env/guard_no_env_id' as const; '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() @ObjectType()
export class UserSettings { export class UserSettings {
@Field(() => ID, { @Field(() => ID, {
description: 'ID of the User Settings', description: 'ID of the User Setting',
}) })
id: string; id: string;
@Field(() => ID, { @Field(() => ID, {
description: 'ID of the user this settings belongs to', description: 'ID of the user this setting belongs to',
}) })
userUid: string; userUid: string;
@Field({ @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 properties: string; // JSON string of the properties object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client
@Field({ @Field({
description: 'Last updated date-time of the settings', description: 'Last updated on',
}) })
updatedOn: Date; updatedOn: Date;
} }

View File

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

View File

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

View File

@@ -9,7 +9,6 @@ import {
USER_NOT_FOUND, USER_NOT_FOUND,
USER_SETTINGS_INVALID_PROPERTIES, USER_SETTINGS_INVALID_PROPERTIES,
USER_SETTINGS_NOT_FOUND, USER_SETTINGS_NOT_FOUND,
USER_SETTINGS_UPDATE_FAILED,
} from 'src/errors'; } from 'src/errors';
@Injectable() @Injectable()
@@ -19,83 +18,90 @@ export class UserSettingsService {
private readonly pubsub: PubSubService, 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) { async fetchUserSettings(user: User) {
try { try {
const dbUserSettings = await this.prisma.userSettings.findUnique({ const userSettings = await this.prisma.userSettings.findUniqueOrThrow({
where: { userUid: user.uid }, where: { userUid: user.uid },
rejectOnNotFound: true,
}); });
const userSettings: UserSettings = { const settings: UserSettings = {
id: dbUserSettings.id, ...userSettings,
userUid: dbUserSettings.userUid, properties: JSON.stringify(userSettings.properties),
properties: JSON.stringify(dbUserSettings.properties),
updatedOn: dbUserSettings.updatedOn,
}; };
return E.right(userSettings); return E.right(settings);
} catch (e) { } catch (e) {
return E.left(USER_SETTINGS_NOT_FOUND); 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) { async createUserSettings(user: User, properties: string) {
if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES); if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES);
const jsonProperties = stringToJson(properties); const settingsObject = stringToJson(properties);
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
try { try {
const dbUserSettings = await this.prisma.userSettings.create({ const userSettings = await this.prisma.userSettings.create({
data: { data: {
properties: jsonProperties.right, properties: settingsObject.right,
userUid: user.uid, userUid: user.uid,
}, },
}); });
const userSettings: UserSettings = { const settings: UserSettings = {
id: dbUserSettings.id, ...userSettings,
userUid: dbUserSettings.userUid, properties: JSON.stringify(userSettings.properties),
properties,
updatedOn: dbUserSettings.updatedOn,
}; };
return E.right(userSettings); return E.right(settings);
} catch (e) { } catch (e) {
return E.left(USER_NOT_FOUND); 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) { async updateUserSettings(user: User, properties: string) {
if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES); if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES);
const jsonProperties = stringToJson(properties); const settingsObject = stringToJson(properties);
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
try { try {
const dbUpdatedUserSettings = await this.prisma.userSettings.update({ const updatedUserSettings = await this.prisma.userSettings.update({
where: { userUid: user.uid }, where: { userUid: user.uid },
data: { data: {
properties: jsonProperties.right, properties: settingsObject.right,
}, },
}); });
const updatedUserSettings: UserSettings = { const settings: UserSettings = {
id: dbUpdatedUserSettings.id, ...updatedUserSettings,
userUid: dbUpdatedUserSettings.userUid, properties: JSON.stringify(updatedUserSettings.properties),
properties,
updatedOn: dbUpdatedUserSettings.updatedOn,
}; };
// Publish subscription for environment creation // Publish subscription for environment creation
await this.pubsub.publish( await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);
`user_settings/${user.uid}/updated`,
updatedUserSettings,
);
return E.right(updatedUserSettings); return E.right(settings);
} catch (e) { } 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, { @ResolveField(() => UserSettings, {
description: 'Returns user settings', description: 'Returns user settings',
}) })
async settings(@Parent() user: User): Promise<UserSettings | string> { async settings(@Parent() user: User) {
const userSettings = await this.userSettingsService.fetchUserSettings(user); const userSettings = await this.userSettingsService.fetchUserSettings(user);
if (E.isLeft(userSettings)) throwErr(userSettings.left); if (E.isLeft(userSettings)) throwErr(userSettings.left);