From 3bc203c4986efedfd609f33cf927a379aebbff6c Mon Sep 17 00:00:00 2001 From: Carina Antunes <carina.oliveira.antunes@cern.ch> Date: Mon, 19 Sep 2022 22:07:51 +0000 Subject: [PATCH] bump typeorm --- src/models/api-key-object.ts | 5 +- src/models/user-channel-collection.ts | 33 ++++-------- src/services/impl/api-key/verify-api-key.ts | 6 +-- .../GetAuthenticatedUser.ts | 22 ++++---- .../impl/categories/create-category.ts | 4 +- src/services/impl/channels/create-channel.ts | 8 +-- .../impl/channels/remove-user-from-channel.ts | 13 ++--- .../impl/channels/subscribe-to-channel.ts | 13 ++--- .../impl/channels/unsubscribe-from-channel.ts | 13 ++--- .../channels/update-channel-admin-group.ts | 2 +- src/services/impl/channels/update-channel.ts | 11 ++-- .../impl/devices/update-user-device.ts | 2 +- .../impl/groups/get-or-create-group.ts | 2 +- .../impl/mutes/create-unsubscribe-mute.ts | 2 +- src/services/impl/mutes/create-user-mute.ts | 2 +- .../notifications/update-user-notification.ts | 13 +++-- .../impl/preferences/get-user-preferences.ts | 35 ++++++------- src/services/impl/tags/create-tag.ts | 30 ++++++----- .../impl/usersettings/delete-draft.ts | 37 +++++++------- src/services/impl/usersettings/get-draft.ts | 29 +++++------ .../impl/usersettings/get-user-settings.ts | 51 +++++++++---------- src/services/impl/usersettings/save-draft.ts | 39 +++++++------- 22 files changed, 169 insertions(+), 203 deletions(-) diff --git a/src/models/api-key-object.ts b/src/models/api-key-object.ts index c2b3ea0a..fb19b928 100644 --- a/src/models/api-key-object.ts +++ b/src/models/api-key-object.ts @@ -1,9 +1,12 @@ -import { Column } from 'typeorm'; +import { Column, PrimaryGeneratedColumn } from 'typeorm'; import { HexBase64BinaryEncoding } from 'crypto'; const crypto = require('crypto'); export abstract class ApiKeyObject { + @PrimaryGeneratedColumn('uuid') + id: string; + //this is a hash of the API key and the salt @Column({ nullable: true }) APIKey: string; diff --git a/src/models/user-channel-collection.ts b/src/models/user-channel-collection.ts index 6fa0208d..40c00279 100644 --- a/src/models/user-channel-collection.ts +++ b/src/models/user-channel-collection.ts @@ -1,11 +1,4 @@ -import { - Entity, - Index, - JoinColumn, - ManyToOne, - PrimaryColumn, - RelationId, -} from 'typeorm'; +import { Entity, Index, JoinColumn, ManyToOne, PrimaryColumn, RelationId } from 'typeorm'; import { Channel } from './channel'; import { User } from './user'; @@ -19,28 +12,24 @@ export class UserChannelCollection { @PrimaryColumn('varchar') type: UserChannelCollectionType; - @ManyToOne(type => User, { primary: true }) + @PrimaryColumn() + @RelationId((userChannelCollection: UserChannelCollection) => userChannelCollection.user) + userId: string; + + @PrimaryColumn() + @RelationId((userChannelCollection: UserChannelCollection) => userChannelCollection.channel) + channelId: string; + + @ManyToOne(type => User) @JoinColumn() @Index() user: User; - @RelationId( - (userChannelCollection: UserChannelCollection) => - userChannelCollection.user, - ) - userId: string; - @Index() - @ManyToOne(type => Channel, { primary: true }) + @ManyToOne(type => Channel) @JoinColumn() channel: Channel; - @RelationId( - (userChannelCollection: UserChannelCollection) => - userChannelCollection.channel, - ) - channelId: string; - constructor(collection: UserChannelCollection) { if (collection) { this.channel = collection.channel; diff --git a/src/services/impl/api-key/verify-api-key.ts b/src/services/impl/api-key/verify-api-key.ts index 80c456a8..6a1e5a52 100644 --- a/src/services/impl/api-key/verify-api-key.ts +++ b/src/services/impl/api-key/verify-api-key.ts @@ -7,11 +7,7 @@ export class VerifyApiKey implements Command { constructor(private id: string, private key: string, private entity: EntityTarget<ApiKeyObject>) {} async execute(transactionManager: EntityManager): Promise<boolean> { - const obj = await transactionManager.findOne(this.entity, { - where: { - id: this.id, - }, - }); + const obj = await transactionManager.findOneBy(this.entity, { id: this.id }); if (!obj) return false; diff --git a/src/services/impl/auth/get-authenticated-user/GetAuthenticatedUser.ts b/src/services/impl/auth/get-authenticated-user/GetAuthenticatedUser.ts index 83e2642a..c5630aff 100644 --- a/src/services/impl/auth/get-authenticated-user/GetAuthenticatedUser.ts +++ b/src/services/impl/auth/get-authenticated-user/GetAuthenticatedUser.ts @@ -1,15 +1,11 @@ -import { Command } from "../../command"; -import { EntityManager } from "typeorm"; -import { User } from "../../../../models/user"; +import { Command } from '../../command'; +import { EntityManager } from 'typeorm'; +import { User } from '../../../../models/user'; -export class GetAuthenticatedUser implements Command{ +export class GetAuthenticatedUser implements Command { + constructor(private username: string) {} - constructor(private username: string){ - - } - - async execute(transactionManager: EntityManager): Promise<User> { - return await transactionManager.findOne(User, {username: this.username} ) - } - -} \ No newline at end of file + async execute(transactionManager: EntityManager): Promise<User> { + return await transactionManager.findOneBy(User, { username: this.username }); + } +} diff --git a/src/services/impl/categories/create-category.ts b/src/services/impl/categories/create-category.ts index c8f9be47..49c43745 100644 --- a/src/services/impl/categories/create-category.ts +++ b/src/services/impl/categories/create-category.ts @@ -14,7 +14,7 @@ export class CreateCategory implements Command { } // Check if category already exists if ( - await transactionManager.findOne(Category, { + await transactionManager.findOneBy(Category, { name: this.category.name, }) ) { @@ -24,7 +24,7 @@ export class CreateCategory implements Command { const newCategory = new Category({ ...this.category }); if (this.category.parent) { - const parentCategory = await transactionManager.findOne(Category, { + const parentCategory = await transactionManager.findOneBy(Category, { name: this.category.parent, }); if (!parentCategory) { diff --git a/src/services/impl/channels/create-channel.ts b/src/services/impl/channels/create-channel.ts index 16d492b5..5d6d2a03 100644 --- a/src/services/impl/channels/create-channel.ts +++ b/src/services/impl/channels/create-channel.ts @@ -16,7 +16,7 @@ export class CreateChannel implements Command { async execute(transactionManager: EntityManager): Promise<ChannelResponse> { // Get owner (user who have created the channel) to set as user by default - const user = await transactionManager.findOne(User, { + const user = await transactionManager.findOneBy(User, { id: this.authorizationBag.userId, }); @@ -36,14 +36,14 @@ export class CreateChannel implements Command { } if ( - await transactionManager.findOne(Channel, { + await transactionManager.findOneBy(Channel, { name: channel.name, }) ) { throw new BadRequestError('Channel name already exists'); } if ( - await transactionManager.findOne(Channel, { + await transactionManager.findOneBy(Channel, { slug: channel.slug, }) ) { @@ -51,7 +51,7 @@ export class CreateChannel implements Command { } if (this.channel.adminGroup) { - const adminGroup = await transactionManager.findOne(Group, { groupIdentifier: this.channel.adminGroup }); + const adminGroup = await transactionManager.findOneBy(Group, { groupIdentifier: this.channel.adminGroup }); if (!adminGroup) throw new NotFoundError('Admin group does not exist'); if (adminGroup.groupIdentifier != this.channel.adminGroup) throw new Error('Admin group name does not match provided id.'); diff --git a/src/services/impl/channels/remove-user-from-channel.ts b/src/services/impl/channels/remove-user-from-channel.ts index c81f4688..8bff66d5 100644 --- a/src/services/impl/channels/remove-user-from-channel.ts +++ b/src/services/impl/channels/remove-user-from-channel.ts @@ -10,13 +10,10 @@ export class RemoveUserFromChannel implements Command { constructor(private memberId: string, private channelId: string, private authorizationBag: AuthorizationBag) {} async execute(transactionManager: EntityManager): Promise<void> { - const channel = await transactionManager.findOne( - Channel, - { id: this.channelId }, - { - relations: ['unsubscribed', 'members', 'groups', 'owner', 'adminGroup'], - }, - ); + const channel = await transactionManager.findOne(Channel, { + where: { id: this.channelId }, + relations: ['unsubscribed', 'members', 'groups', 'owner', 'adminGroup'], + }); if (!channel) throw new NotFoundError('Channel does not exist'); @@ -25,7 +22,7 @@ export class RemoveUserFromChannel implements Command { if (this.memberId !== this.authorizationBag.userId && !(await channel.isAdmin(this.authorizationBag))) throw new ForbiddenError('Access to Channel not Authorized !'); - const user = await transactionManager.findOne(User, { id: this.memberId }); + const user = await transactionManager.findOneBy(User, { id: this.memberId }); if (!user) throw new NotFoundError('User not found'); await transactionManager.createQueryBuilder().relation(Channel, 'members').of(channel).remove(user); diff --git a/src/services/impl/channels/subscribe-to-channel.ts b/src/services/impl/channels/subscribe-to-channel.ts index 84da7160..346ef890 100644 --- a/src/services/impl/channels/subscribe-to-channel.ts +++ b/src/services/impl/channels/subscribe-to-channel.ts @@ -10,13 +10,10 @@ export class SubscribeToChannel implements Command { constructor(private memberId: string, private channelId: string, private authorizationBag: AuthorizationBag) {} async execute(transactionManager: EntityManager): Promise<void> { - const channel = await transactionManager.findOne( - Channel, - { id: this.channelId }, - { - relations: ['unsubscribed', 'members', 'groups', 'owner', 'adminGroup'], - }, - ); + const channel = await transactionManager.findOne(Channel, { + where: { id: this.channelId }, + relations: ['unsubscribed', 'members', 'groups', 'owner', 'adminGroup'], + }); if (!channel) throw new NotFoundError('Channel does not exist'); @@ -27,7 +24,7 @@ export class SubscribeToChannel implements Command { else if (!(await channel.hasAccess(this.authorizationBag))) throw new ForbiddenError('Access to Channel not Authorized !'); - const user = await transactionManager.findOne(User, { id: this.memberId }); + const user = await transactionManager.findOneBy(User, { id: this.memberId }); channel.addMember(user); const updatedChannel = await transactionManager.save(channel); diff --git a/src/services/impl/channels/unsubscribe-from-channel.ts b/src/services/impl/channels/unsubscribe-from-channel.ts index 8e1f2346..331172b2 100644 --- a/src/services/impl/channels/unsubscribe-from-channel.ts +++ b/src/services/impl/channels/unsubscribe-from-channel.ts @@ -11,13 +11,10 @@ export class UnsubscribeFromChannel implements Command { constructor(private memberId: string, private channelId: string, private authorizationBag: AuthorizationBag) {} async execute(transactionManager: EntityManager): Promise<void> { - const channel = await transactionManager.findOne( - Channel, - { id: this.channelId }, - { - relations: ['unsubscribed', 'members', 'groups', 'owner', 'adminGroup'], - }, - ); + const channel = await transactionManager.findOne(Channel, { + where: { id: this.channelId }, + relations: ['unsubscribed', 'members', 'groups', 'owner', 'adminGroup'], + }); if (!channel) throw new NotFoundError('Channel does not exist'); @@ -31,7 +28,7 @@ export class UnsubscribeFromChannel implements Command { 'This channel is mandatory, unsubscribe is not possible. You can however mute it if needed.', ); - const user = await transactionManager.findOne(User, { id: this.memberId }); + const user = await transactionManager.findOneBy(User, { id: this.memberId }); if (!user) throw new NotFoundError('User not found'); channel.unsubscribeMember(user); diff --git a/src/services/impl/channels/update-channel-admin-group.ts b/src/services/impl/channels/update-channel-admin-group.ts index fe2ed4f0..98659eca 100644 --- a/src/services/impl/channels/update-channel-admin-group.ts +++ b/src/services/impl/channels/update-channel-admin-group.ts @@ -30,7 +30,7 @@ export class UpdateChannelAdminGroup implements Command { const newGroup = new Group({ groupIdentifier: this.newAdminGroup.newAdminGroup }); - let groupToAdd = await transactionManager.findOne(Group, { + let groupToAdd = await transactionManager.findOneBy(Group, { groupIdentifier: this.newAdminGroup.newAdminGroup, }); if (!groupToAdd && (await newGroup.exists())) { diff --git a/src/services/impl/channels/update-channel.ts b/src/services/impl/channels/update-channel.ts index a2b65c63..fc4dc3d1 100644 --- a/src/services/impl/channels/update-channel.ts +++ b/src/services/impl/channels/update-channel.ts @@ -35,16 +35,13 @@ export class UpdateChannel implements Command { } if ( - await transactionManager.findOne( - Channel, - { + await transactionManager.findOne(Channel, { + where: { id: Not(channel.id), name: this.channel.name, }, - { - withDeleted: true, - }, - ) + withDeleted: true, + }) ) { throw new BadRequestError('Channel name already exists'); } diff --git a/src/services/impl/devices/update-user-device.ts b/src/services/impl/devices/update-user-device.ts index 631716b2..9e4c494c 100644 --- a/src/services/impl/devices/update-user-device.ts +++ b/src/services/impl/devices/update-user-device.ts @@ -27,7 +27,7 @@ export class UpdateUserDevice implements Command { if (result.affected !== 1) throw new ForbiddenError('The device does not exist or you are not the owner'); - const response = await transactionManager.findOne(Device, { id: this.deviceId }); + const response = await transactionManager.findOneBy(Device, { id: this.deviceId }); return new DeviceResponse(response); } diff --git a/src/services/impl/groups/get-or-create-group.ts b/src/services/impl/groups/get-or-create-group.ts index c2edd6f1..68848ab2 100644 --- a/src/services/impl/groups/get-or-create-group.ts +++ b/src/services/impl/groups/get-or-create-group.ts @@ -7,7 +7,7 @@ export class GetOrCreateGroup implements Command { constructor(private groupIdentifier: string) {} async execute(transactionManager: EntityManager): Promise<Group> { - let group = await transactionManager.findOne(Group, { + let group = await transactionManager.findOneBy(Group, { groupIdentifier: this.groupIdentifier, }); if (group) return group; diff --git a/src/services/impl/mutes/create-unsubscribe-mute.ts b/src/services/impl/mutes/create-unsubscribe-mute.ts index 1fc217c2..8bbe71ef 100644 --- a/src/services/impl/mutes/create-unsubscribe-mute.ts +++ b/src/services/impl/mutes/create-unsubscribe-mute.ts @@ -28,7 +28,7 @@ export class CreateUnSubscribeMute implements Command { } // Retrieve existing user if any for this email - let user = await transactionManager.findOne(User, { email: this.email }); + let user = await transactionManager.findOneBy(User, { email: this.email }); // Check if a global permanent unsubscribe record already exists for this user if ( diff --git a/src/services/impl/mutes/create-user-mute.ts b/src/services/impl/mutes/create-user-mute.ts index 2f91a90e..318e062a 100644 --- a/src/services/impl/mutes/create-user-mute.ts +++ b/src/services/impl/mutes/create-user-mute.ts @@ -32,7 +32,7 @@ export class CreateUserMute implements Command { ); if (savedMute) { return await transactionManager.findOne(Mute, { - relations: ["target"], + relations: ['target'], where: { id: savedMute.id, }, diff --git a/src/services/impl/notifications/update-user-notification.ts b/src/services/impl/notifications/update-user-notification.ts index 6e037de7..fd2352d6 100644 --- a/src/services/impl/notifications/update-user-notification.ts +++ b/src/services/impl/notifications/update-user-notification.ts @@ -12,7 +12,7 @@ export class UpdateUserNotification implements Command { async execute(transactionManager: EntityManager): Promise<any> { if (await this.isNotificationOfUser(transactionManager)) { - let userNotification = await transactionManager.findOne(UserNotification, { + let userNotification = await transactionManager.findOneBy(UserNotification, { user: { id: this.authorizationBag.userId }, notification: this.notification.notification, }); @@ -23,18 +23,17 @@ export class UpdateUserNotification implements Command { delete userNotification.user; return { - ...(await transactionManager.findOne(Notification, { id: this.notification.notification })), + ...(await transactionManager.findOneBy(Notification, { id: this.notification.notification })), ...userNotification, }; } } async isNotificationOfUser(transactionManager: EntityManager): Promise<boolean> { - const notification = await transactionManager.findOne( - Notification, - { id: this.notification.notification }, - { relations: ['users', 'users.user'] }, - ); + const notification = await transactionManager.findOne(Notification, { + where: { id: this.notification.notification }, + relations: ['users', 'users.user'], + }); return notification.users.filter(u => u.user.id === this.authorizationBag.userId).length === 1; } } diff --git a/src/services/impl/preferences/get-user-preferences.ts b/src/services/impl/preferences/get-user-preferences.ts index 780be221..25a72ddc 100644 --- a/src/services/impl/preferences/get-user-preferences.ts +++ b/src/services/impl/preferences/get-user-preferences.ts @@ -1,20 +1,21 @@ -import { Command } from "../command"; -import { EntityManager } from "typeorm"; -import { Preference } from "../../../models/preference"; -import { AuthorizationBag } from "../../../models/authorization-bag"; +import { Command } from '../command'; +import { EntityManager } from 'typeorm'; +import { Preference } from '../../../models/preference'; +import { AuthorizationBag } from '../../../models/authorization-bag'; export class GetUserPreferences implements Command { - constructor( - private channelId: string, - private authorizationBag: AuthorizationBag - ) {} + constructor(private channelId: string, private authorizationBag: AuthorizationBag) {} private async fetchGlobalPreferences(transactionManager: EntityManager) { return await transactionManager.find(Preference, { - relations: ["target", "disabledChannels", "devices"], + relations: { + target: true, + disabledChannels: true, + devices: true, + }, where: { - user: this.authorizationBag.userId, - target: null + user: { id: this.authorizationBag.userId }, + target: null, }, }); } @@ -22,11 +23,11 @@ export class GetUserPreferences implements Command { private async fetchChannelPreferences(transactionManager: EntityManager) { return await transactionManager .getRepository(Preference) - .createQueryBuilder("preference") - .leftJoin("preference.user", "user") - .leftJoinAndSelect("preference.target", "target") - .leftJoinAndSelect("preference.devices", "device") - .where("user.id = :userId AND target.id = :channelId", { + .createQueryBuilder('preference') + .leftJoin('preference.user', 'user') + .leftJoinAndSelect('preference.target', 'target') + .leftJoinAndSelect('preference.devices', 'device') + .where('user.id = :userId AND target.id = :channelId', { userId: this.authorizationBag.userId, channelId: this.channelId, }) @@ -35,7 +36,7 @@ export class GetUserPreferences implements Command { async execute(transactionManager: EntityManager) { const result = { - globalPreferences: await this.fetchGlobalPreferences(transactionManager) + globalPreferences: await this.fetchGlobalPreferences(transactionManager), }; if (this.channelId) { diff --git a/src/services/impl/tags/create-tag.ts b/src/services/impl/tags/create-tag.ts index e52cb74e..9cb21b9d 100644 --- a/src/services/impl/tags/create-tag.ts +++ b/src/services/impl/tags/create-tag.ts @@ -1,30 +1,32 @@ -import { Command } from "../command"; -import { EntityManager } from "typeorm"; -import { Tag } from "../../../models/tag"; -import { AuthorizationBag } from "../../../models/authorization-bag"; -import { BadRequestError } from "routing-controllers"; +import { Command } from '../command'; +import { EntityManager } from 'typeorm'; +import { Tag } from '../../../models/tag'; +import { AuthorizationBag } from '../../../models/authorization-bag'; +import { BadRequestError } from 'routing-controllers'; export class CreateTag implements Command { - constructor(private tag: Tag, private authorizationBag: AuthorizationBag) { } + constructor(private tag: Tag, private authorizationBag: AuthorizationBag) {} async execute(transactionManager: EntityManager) { // Check tag length if (this.tag.name.length < 2 || this.tag.name.length > 32) { - throw new BadRequestError("Tag should be between 2 and 32 characters.") + throw new BadRequestError('Tag should be between 2 and 32 characters.'); } // Check if tag already exists - if (await transactionManager.findOne(Tag, { - name: this.tag.name, - }) ) { - throw new BadRequestError("Tag already exists"); + if ( + await transactionManager.findOneBy(Tag, { + name: this.tag.name, + }) + ) { + throw new BadRequestError('Tag already exists'); } - const tag = new Tag({ ...this.tag }) + const tag = new Tag({ ...this.tag }); const savedTag = await transactionManager.save(tag); if (savedTag) { - return await transactionManager.findOne(Tag, { - id: savedTag.id, + return await transactionManager.findOneBy(Tag, { + id: savedTag.id, }); } } diff --git a/src/services/impl/usersettings/delete-draft.ts b/src/services/impl/usersettings/delete-draft.ts index 2bbb0de5..37f0c858 100644 --- a/src/services/impl/usersettings/delete-draft.ts +++ b/src/services/impl/usersettings/delete-draft.ts @@ -1,25 +1,24 @@ -import {Command} from "../command"; -import {EntityManager} from "typeorm"; -import {UserSettings} from "../../../models/user-settings"; -import {AuthorizationBag} from "../../../models/authorization-bag"; -import {NotFoundError} from "routing-controllers"; +import { Command } from '../command'; +import { EntityManager } from 'typeorm'; +import { UserSettings } from '../../../models/user-settings'; +import { AuthorizationBag } from '../../../models/authorization-bag'; +import { NotFoundError } from 'routing-controllers'; export class DeleteDraft implements Command { - constructor(private authorizationBag: AuthorizationBag) { - } - - async execute(transactionManager: EntityManager) { - let userSetting = await transactionManager.findOne(UserSettings, { - where: { - user: this.authorizationBag.userId, - }, - }); + constructor(private authorizationBag: AuthorizationBag) {} - if (!userSetting) { - throw new NotFoundError("Draft not found") - } + async execute(transactionManager: EntityManager) { + const userSetting = await transactionManager.findOne(UserSettings, { + where: { + user: { id: this.authorizationBag.userId }, + }, + }); - userSetting.draft = null; - await transactionManager.save(userSetting); + if (!userSetting) { + throw new NotFoundError('Draft not found'); } + + userSetting.draft = null; + await transactionManager.save(userSetting); + } } diff --git a/src/services/impl/usersettings/get-draft.ts b/src/services/impl/usersettings/get-draft.ts index 6e2d191e..98dd1299 100644 --- a/src/services/impl/usersettings/get-draft.ts +++ b/src/services/impl/usersettings/get-draft.ts @@ -1,21 +1,20 @@ -import {Command} from "../command"; -import {EntityManager} from "typeorm"; -import {UserSettings} from "../../../models/user-settings"; -import {AuthorizationBag} from "../../../models/authorization-bag"; +import { Command } from '../command'; +import { EntityManager } from 'typeorm'; +import { UserSettings } from '../../../models/user-settings'; +import { AuthorizationBag } from '../../../models/authorization-bag'; export class GetDraft implements Command { - constructor(private authorizationBag: AuthorizationBag) { - } + constructor(private authorizationBag: AuthorizationBag) {} - async execute(transactionManager: EntityManager) { - const userSetting = await transactionManager.findOne(UserSettings, { - where: { - user: this.authorizationBag.userId, - }, - }); + async execute(transactionManager: EntityManager) { + const userSetting = await transactionManager.findOne(UserSettings, { + where: { + user: { id: this.authorizationBag.userId }, + }, + }); - if (userSetting) { - return userSetting.draft; - } + if (userSetting) { + return userSetting.draft; } + } } diff --git a/src/services/impl/usersettings/get-user-settings.ts b/src/services/impl/usersettings/get-user-settings.ts index b8427d1f..bd58393e 100644 --- a/src/services/impl/usersettings/get-user-settings.ts +++ b/src/services/impl/usersettings/get-user-settings.ts @@ -1,35 +1,30 @@ -import { Command } from "../command"; -import { EntityManager } from "typeorm"; -import { UserSettings } from "../../../models/user-settings"; -import { User } from "../../../models/user"; -import { AuthorizationBag } from "../../../models/authorization-bag"; -import { NotFoundError } from "routing-controllers"; +import { Command } from '../command'; +import { EntityManager } from 'typeorm'; +import { UserSettings } from '../../../models/user-settings'; +import { User } from '../../../models/user'; +import { AuthorizationBag } from '../../../models/authorization-bag'; +import { NotFoundError } from 'routing-controllers'; export class GetUserSettings implements Command { - constructor(private authorizationBag: AuthorizationBag) { - } - - async execute(transactionManager: EntityManager) { + constructor(private authorizationBag: AuthorizationBag) {} - let userSettings = await transactionManager.findOne(UserSettings, { - where: { - user: this.authorizationBag.userId, - }, - }); + async execute(transactionManager: EntityManager) { + let userSettings = await transactionManager.findOne(UserSettings, { + where: { + user: this.authorizationBag.userId, + }, + }); - if (!userSettings) { - const user = await transactionManager.findOne( - User, - { id: this.authorizationBag.userId} - ); + if (!userSettings) { + const user = await transactionManager.findOneBy(User, { id: this.authorizationBag.userId }); - userSettings = new UserSettings({ - user: user, - draft: "", - favoriteList: [], - }); - return await transactionManager.save(userSettings); - } - return userSettings; + userSettings = new UserSettings({ + user: user, + draft: '', + favoriteList: [], + }); + return await transactionManager.save(userSettings); } + return userSettings; + } } diff --git a/src/services/impl/usersettings/save-draft.ts b/src/services/impl/usersettings/save-draft.ts index 0446f944..b34d5f6b 100644 --- a/src/services/impl/usersettings/save-draft.ts +++ b/src/services/impl/usersettings/save-draft.ts @@ -1,26 +1,25 @@ -import {Command} from "../command"; -import {EntityManager} from "typeorm"; -import {UserSettings} from "../../../models/user-settings"; -import {AuthorizationBag} from "../../../models/authorization-bag"; +import { Command } from '../command'; +import { EntityManager } from 'typeorm'; +import { UserSettings } from '../../../models/user-settings'; +import { AuthorizationBag } from '../../../models/authorization-bag'; export class SaveDraft implements Command { - constructor(private draft: string, private authorizationBag: AuthorizationBag) { - } - - async execute(transactionManager: EntityManager) { - let userSetting = await transactionManager.findOne(UserSettings, { - where: { - user: this.authorizationBag.userId, - }, - }); + constructor(private draft: string, private authorizationBag: AuthorizationBag) {} - if (!userSetting) { - userSetting = new UserSettings({ - user: this.authorizationBag.userId, - }) - } + async execute(transactionManager: EntityManager) { + let userSetting = await transactionManager.findOne(UserSettings, { + where: { + user: { id: this.authorizationBag.userId }, + }, + }); - userSetting.draft = this.draft; - await transactionManager.save(userSetting); + if (!userSetting) { + userSetting = new UserSettings({ + user: this.authorizationBag.userId, + }); } + + userSetting.draft = this.draft; + await transactionManager.save(userSetting); + } } -- GitLab