feat: added mutation for update user settings
This commit is contained in:
@@ -215,3 +215,9 @@ 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;
|
||||
@@ -35,5 +35,24 @@ export class UserSettingsResolver {
|
||||
return userSettings.right;
|
||||
}
|
||||
|
||||
@Mutation(() => UserSettings, {
|
||||
description: 'Update user settings for given user',
|
||||
})
|
||||
@UseGuards(GqlAuthGuard)
|
||||
async updateUserSettings(
|
||||
@GqlUser() user: User,
|
||||
@Args({
|
||||
name: 'properties',
|
||||
description: 'JSON string of properties object',
|
||||
})
|
||||
properties: string,
|
||||
) {
|
||||
const updatedUserSettings =
|
||||
await this.userSettingsService.updateUserSettings(user, properties);
|
||||
|
||||
if (E.isLeft(updatedUserSettings)) throwErr(updatedUserSettings.left);
|
||||
return updatedUserSettings.right;
|
||||
}
|
||||
|
||||
/* Subscriptions */
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { User } from 'src/user/user.model';
|
||||
import * as E from 'fp-ts/Either';
|
||||
import { stringToJson } from 'src/utils';
|
||||
import { UserSettings } from './user-settings.model';
|
||||
import { USER_SETTINGS_UPDATE_FAILED } from 'src/errors';
|
||||
|
||||
@Injectable()
|
||||
export class UserSettingsService {
|
||||
@@ -33,4 +34,35 @@ export class UserSettingsService {
|
||||
|
||||
return E.right(userSettings);
|
||||
}
|
||||
|
||||
async updateUserSettings(user: User, properties: string) {
|
||||
const jsonProperties = stringToJson(properties);
|
||||
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left);
|
||||
|
||||
try {
|
||||
const dbUpdatedUserSettings = await this.prisma.userSettings.update({
|
||||
where: { userUid: user.uid },
|
||||
data: {
|
||||
properties: jsonProperties.right,
|
||||
},
|
||||
});
|
||||
|
||||
const updatedUserSettings: UserSettings = {
|
||||
id: dbUpdatedUserSettings.id,
|
||||
userUid: dbUpdatedUserSettings.userUid,
|
||||
properties,
|
||||
updatedOn: dbUpdatedUserSettings.updatedOn,
|
||||
};
|
||||
|
||||
// Publish subscription for environment creation
|
||||
await this.pubsub.publish(
|
||||
`user_settings/${user.uid}/updated`,
|
||||
updatedUserSettings,
|
||||
);
|
||||
|
||||
return E.right(updatedUserSettings);
|
||||
} catch (e) {
|
||||
return E.left(USER_SETTINGS_UPDATE_FAILED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user