feat: error message updated
This commit is contained in:
@@ -172,13 +172,13 @@ export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const;
|
|||||||
* User setting not found for a user
|
* User setting not found for a user
|
||||||
* (UserSettingsService)
|
* (UserSettingsService)
|
||||||
*/
|
*/
|
||||||
export const USER_SETTINGS_EXIST = 'user_settings/exist' as const;
|
export const USER_SETTINGS_ALREADY_EXISTS = 'user_settings/settings_already_present' as const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User setting invalid (null) properties
|
* User setting invalid (null) settings
|
||||||
* (UserSettingsService)
|
* (UserSettingsService)
|
||||||
*/
|
*/
|
||||||
export const USER_SETTINGS_INVALID_PROPERTIES = 'user_settings/invalid_properties' as const;
|
export const USER_SETTINGS_NULL_SETTINGS = 'user_settings/null_settings' as const;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { UserSettingsService } from './user-settings.service';
|
|||||||
import {
|
import {
|
||||||
JSON_INVALID,
|
JSON_INVALID,
|
||||||
USER_NOT_FOUND,
|
USER_NOT_FOUND,
|
||||||
USER_SETTINGS_INVALID_PROPERTIES,
|
USER_SETTINGS_NULL_SETTINGS,
|
||||||
USER_SETTINGS_NOT_FOUND,
|
USER_SETTINGS_NOT_FOUND,
|
||||||
} from 'src/errors';
|
} from 'src/errors';
|
||||||
import { UserSettings } from './user-settings.model';
|
import { UserSettings } from './user-settings.model';
|
||||||
@@ -22,15 +22,15 @@ const userSettingsService = new UserSettingsService(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const user: User = {
|
const user: User = {
|
||||||
uid: 'user-uid',
|
uid: 'aabb22ccdd',
|
||||||
displayName: 'user-display-name',
|
displayName: 'user-display-name',
|
||||||
email: 'user-email',
|
email: 'user-email',
|
||||||
photoURL: 'user-photo-url',
|
photoURL: 'user-photo-url',
|
||||||
};
|
};
|
||||||
const userSettings: UserSettings = {
|
const settings: UserSettings = {
|
||||||
id: '1',
|
id: '1',
|
||||||
userUid: user.uid,
|
userUid: user.uid,
|
||||||
properties: JSON.stringify({ key: 'k', value: 'v' }),
|
userSettings: JSON.stringify({ key: 'k', value: 'v' }),
|
||||||
updatedOn: new Date('2022-12-19T12:43:18.635Z'),
|
updatedOn: new Date('2022-12-19T12:43:18.635Z'),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -42,90 +42,75 @@ beforeEach(() => {
|
|||||||
describe('UserSettingsService', () => {
|
describe('UserSettingsService', () => {
|
||||||
describe('createUserSettings', () => {
|
describe('createUserSettings', () => {
|
||||||
test('should create a user setting 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({
|
||||||
|
...settings,
|
||||||
|
settings: JSON.parse(settings.userSettings),
|
||||||
|
});
|
||||||
|
|
||||||
const result = await userSettingsService.createUserSettings(
|
const result = await userSettingsService.createUserSettings(
|
||||||
user,
|
user,
|
||||||
JSON.stringify(userSettings.properties),
|
settings.userSettings,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toEqualRight({
|
expect(result).toEqualRight(settings);
|
||||||
...userSettings,
|
|
||||||
properties: JSON.stringify(userSettings.properties),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('should reject for invalid user', async () => {
|
|
||||||
const result = await userSettingsService.createUserSettings(
|
|
||||||
null as any,
|
|
||||||
JSON.stringify(userSettings.properties),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result).toEqualLeft(USER_NOT_FOUND);
|
|
||||||
});
|
});
|
||||||
test('should reject for invalid properties', async () => {
|
test('should reject for invalid properties', async () => {
|
||||||
const result = await userSettingsService.createUserSettings(
|
const result = await userSettingsService.createUserSettings(
|
||||||
user,
|
user,
|
||||||
'invalid-properties',
|
'invalid-settings',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toEqualLeft(JSON_INVALID);
|
expect(result).toEqualLeft(JSON_INVALID);
|
||||||
});
|
});
|
||||||
test('should reject for null properties', async () => {
|
test('should reject for null settings', async () => {
|
||||||
const result = await userSettingsService.createUserSettings(
|
const result = await userSettingsService.createUserSettings(
|
||||||
user,
|
user,
|
||||||
null as any,
|
null as any,
|
||||||
);
|
);
|
||||||
expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES);
|
|
||||||
|
expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('updateUserSettings', () => {
|
describe('updateUserSettings', () => {
|
||||||
test('should update a user setting for valid user and properties', async () => {
|
test('should update a user setting for valid user and settings', async () => {
|
||||||
mockPrisma.userSettings.update.mockResolvedValue(userSettings);
|
mockPrisma.userSettings.update.mockResolvedValue({
|
||||||
|
...settings,
|
||||||
const result = await userSettingsService.updateUserSettings(
|
settings: JSON.parse(settings.userSettings),
|
||||||
user,
|
|
||||||
JSON.stringify(userSettings.properties),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result).toEqualRight({
|
|
||||||
...userSettings,
|
|
||||||
properties: JSON.stringify(userSettings.properties),
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
test('should reject for invalid user', async () => {
|
|
||||||
const result = await userSettingsService.updateUserSettings(
|
|
||||||
null as any,
|
|
||||||
JSON.stringify(userSettings.properties),
|
|
||||||
);
|
|
||||||
expect(result).toEqualLeft(USER_SETTINGS_NOT_FOUND);
|
|
||||||
});
|
|
||||||
test('should reject for invalid stringified JSON properties', async () => {
|
|
||||||
const result = await userSettingsService.updateUserSettings(
|
const result = await userSettingsService.updateUserSettings(
|
||||||
user,
|
user,
|
||||||
'invalid-properties',
|
settings.userSettings,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expect(result).toEqualRight(settings);
|
||||||
|
});
|
||||||
|
test('should reject for invalid stringified JSON settings', async () => {
|
||||||
|
const result = await userSettingsService.updateUserSettings(
|
||||||
|
user,
|
||||||
|
'invalid-settings',
|
||||||
|
);
|
||||||
|
|
||||||
expect(result).toEqualLeft(JSON_INVALID);
|
expect(result).toEqualLeft(JSON_INVALID);
|
||||||
});
|
});
|
||||||
test('should reject for null properties', async () => {
|
test('should reject for null settings', async () => {
|
||||||
const result = await userSettingsService.updateUserSettings(
|
const result = await userSettingsService.updateUserSettings(
|
||||||
user,
|
user,
|
||||||
null as any,
|
null as any,
|
||||||
);
|
);
|
||||||
expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES);
|
expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS);
|
||||||
});
|
});
|
||||||
test('should publish message over pubsub on successful update', async () => {
|
test('should publish message over pubsub on successful update', async () => {
|
||||||
mockPrisma.userSettings.update.mockResolvedValue(userSettings);
|
mockPrisma.userSettings.update.mockResolvedValue({
|
||||||
|
...settings,
|
||||||
|
settings: JSON.parse(settings.userSettings),
|
||||||
|
});
|
||||||
|
|
||||||
await userSettingsService.updateUserSettings(
|
await userSettingsService.updateUserSettings(user, settings.userSettings);
|
||||||
user,
|
|
||||||
JSON.stringify(userSettings.properties),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(mockPubSub.publish).toBeCalledWith(
|
expect(mockPubSub.publish).toBeCalledWith(
|
||||||
`user_settings/${user.uid}/updated`,
|
`user_settings/${user.uid}/updated`,
|
||||||
{
|
settings,
|
||||||
...userSettings,
|
|
||||||
properties: JSON.stringify(userSettings.properties),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import * as E from 'fp-ts/Either';
|
|||||||
import { stringToJson } from 'src/utils';
|
import { stringToJson } from 'src/utils';
|
||||||
import { UserSettings } from './user-settings.model';
|
import { UserSettings } from './user-settings.model';
|
||||||
import {
|
import {
|
||||||
USER_SETTINGS_EXIST,
|
USER_SETTINGS_ALREADY_EXISTS,
|
||||||
USER_SETTINGS_INVALID_PROPERTIES,
|
USER_SETTINGS_NULL_SETTINGS,
|
||||||
USER_SETTINGS_NOT_FOUND,
|
USER_SETTINGS_NOT_FOUND,
|
||||||
} from 'src/errors';
|
} from 'src/errors';
|
||||||
|
|
||||||
@@ -33,6 +33,7 @@ export class UserSettingsService {
|
|||||||
...userSettings,
|
...userSettings,
|
||||||
userSettings: JSON.stringify(userSettings.settings),
|
userSettings: JSON.stringify(userSettings.settings),
|
||||||
};
|
};
|
||||||
|
delete (settings as any).settings;
|
||||||
|
|
||||||
return E.right(settings);
|
return E.right(settings);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -47,7 +48,7 @@ export class UserSettingsService {
|
|||||||
* @returns an Either of `UserSettings` or error
|
* @returns an Either of `UserSettings` or error
|
||||||
*/
|
*/
|
||||||
async createUserSettings(user: User, settingsString: string) {
|
async createUserSettings(user: User, settingsString: string) {
|
||||||
if (!settingsString) return E.left(USER_SETTINGS_INVALID_PROPERTIES);
|
if (!settingsString) return E.left(USER_SETTINGS_NULL_SETTINGS);
|
||||||
|
|
||||||
const settingsObject = stringToJson(settingsString);
|
const settingsObject = stringToJson(settingsString);
|
||||||
if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
|
if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
|
||||||
@@ -64,10 +65,11 @@ export class UserSettingsService {
|
|||||||
...userSettings,
|
...userSettings,
|
||||||
userSettings: JSON.stringify(userSettings.settings),
|
userSettings: JSON.stringify(userSettings.settings),
|
||||||
};
|
};
|
||||||
|
delete (settings as any).settings;
|
||||||
|
|
||||||
return E.right(settings);
|
return E.right(settings);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return E.left(USER_SETTINGS_EXIST);
|
return E.left(USER_SETTINGS_ALREADY_EXISTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +80,7 @@ export class UserSettingsService {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async updateUserSettings(user: User, settingsString: string) {
|
async updateUserSettings(user: User, settingsString: string) {
|
||||||
if (!settingsString) return E.left(USER_SETTINGS_INVALID_PROPERTIES);
|
if (!settingsString) return E.left(USER_SETTINGS_NULL_SETTINGS);
|
||||||
|
|
||||||
const settingsObject = stringToJson(settingsString);
|
const settingsObject = stringToJson(settingsString);
|
||||||
if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
|
if (E.isLeft(settingsObject)) return E.left(settingsObject.left);
|
||||||
@@ -95,6 +97,7 @@ export class UserSettingsService {
|
|||||||
...updatedUserSettings,
|
...updatedUserSettings,
|
||||||
userSettings: JSON.stringify(updatedUserSettings.settings),
|
userSettings: JSON.stringify(updatedUserSettings.settings),
|
||||||
};
|
};
|
||||||
|
delete (settings as any).settings;
|
||||||
|
|
||||||
// Publish subscription for environment creation
|
// Publish subscription for environment creation
|
||||||
await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);
|
await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);
|
||||||
|
|||||||
Reference in New Issue
Block a user