diff --git a/api/controllers/UserController.ts b/api/controllers/UserController.ts index e80f4347..f2ffd8ad 100644 --- a/api/controllers/UserController.ts +++ b/api/controllers/UserController.ts @@ -104,26 +104,26 @@ export class UserController { } @Post('/socialMedia') - async insertSocialMediaForUser(@Body() insertSocialMediaRequest: InsertSocialMediaRequest, + async insertSocialMediasForUser(@Body() insertSocialMediaRequests: InsertSocialMediaRequest[], @AuthenticatedUser() user: UserModel): Promise { const userSocialMedia = await this.userSocialMediaService - .insertSocialMediaForUser(user, insertSocialMediaRequest.socialMedia); + .insertSocialMediasForUser(user, insertSocialMediaRequests.map(request => request.socialMedia)); return { error: null, userSocialMedia: userSocialMedia.getPublicSocialMedia() }; } @Patch('/socialMedia/:uuid') - async updateSocialMediaForUser(@Params() params: UuidParam, - @Body() updateSocialMediaRequest: UpdateSocialMediaRequest, + async updateSocialMediasForUser(@Params() params: UuidParam[], + @Body() updateSocialMediaRequests: UpdateSocialMediaRequest[], @AuthenticatedUser() user: UserModel): Promise { const userSocialMedia = await this.userSocialMediaService - .updateSocialMediaByUuid(user, params.uuid, updateSocialMediaRequest.socialMedia); + .updateSocialMediasByUuid(user, params.map(param => param.uuid), updateSocialMediaRequests.map(request => request.socialMedia)); return { error: null, userSocialMedia: userSocialMedia.getPublicSocialMedia() }; } @Delete('/socialMedia/:uuid') - async deleteSocialMediaForUser(@Params() params: UuidParam, + async deleteSocialMediaForUser(@Params() params: UuidParam[], @AuthenticatedUser() user: UserModel): Promise { - await this.userSocialMediaService.deleteSocialMediaByUuid(user, params.uuid); + for (const uuid of params) await this.userSocialMediaService.deleteSocialMediasByUuid(user, params.map(param => param.uuid)); return { error: null }; } } diff --git a/repositories/UserSocialMediaRepository.ts b/repositories/UserSocialMediaRepository.ts index 4554ed10..ad12f971 100644 --- a/repositories/UserSocialMediaRepository.ts +++ b/repositories/UserSocialMediaRepository.ts @@ -17,12 +17,12 @@ export class UserSocialMediaRepository extends BaseRepository): Promise { - if (changes) userSocialMedia = UserSocialMediaModel.merge(userSocialMedia, changes); + changes?: Partial[]): Promise { + if (changes) for (const change of changes) userSocialMedia = UserSocialMediaModel.merge(userSocialMedia, change); return this.repository.save(userSocialMedia); } - public async deleteSocialMedia(userSocialMedia: UserSocialMediaModel): Promise { + public async deleteSocialMedia(userSocialMedia: UserSocialMediaModel[]): Promise { return this.repository.remove(userSocialMedia); } diff --git a/services/UserSocialMediaService.ts b/services/UserSocialMediaService.ts index 6a13cf20..62970a00 100644 --- a/services/UserSocialMediaService.ts +++ b/services/UserSocialMediaService.ts @@ -22,42 +22,53 @@ export default class UserSocialMediaService { return userSocialMedia; } - public async insertSocialMediaForUser(user: UserModel, socialMedia: SocialMedia) { - const addedSocialMedia = await this.transactions.readWrite(async (txn) => { + public async insertSocialMediasForUser(user: UserModel, socialMedias: SocialMedia[]) { + const addedSocialMedias = await this.transactions.readWrite(async (txn) => { const userSocialMediaRepository = Repositories.userSocialMedia(txn); - const isNewSocialMediaType = await userSocialMediaRepository.isNewSocialMediaTypeForUser(user, socialMedia.type); - if (!isNewSocialMediaType) { - throw new UserError('Social media URL of this type has already been created for this user'); + for (const socialMedia of socialMedias) { + const isNewSocialMediaType = await userSocialMediaRepository.isNewSocialMediaTypeForUser(user, socialMedia.type); + if (!isNewSocialMediaType) { + throw new UserError('Social media URL of this type has already been created for this user'); + } } - return userSocialMediaRepository.upsertSocialMedia(UserSocialMediaModel.create({ ...socialMedia, user })); + return userSocialMediaRepository.upsertSocialMedia(UserSocialMediaModel.create({ ...socialMedias, user })); }); - return addedSocialMedia; + return addedSocialMedias; } - public async updateSocialMediaByUuid(user: UserModel, - uuid: Uuid, - changes: Partial): Promise { + public async updateSocialMediasByUuid(user: UserModel, + uuids: Uuid[], + changes: Partial[]): Promise { const updatedSocialMedia = await this.transactions.readWrite(async (txn) => { const userSocialMediaRepository = Repositories.userSocialMedia(txn); - const socialMedia = await userSocialMediaRepository.findByUuid(uuid); - if (!socialMedia) throw new NotFoundError('Social media URL not found'); - if (user.uuid !== socialMedia.user.uuid) { - throw new ForbiddenError('User cannot update a social media URL of another user'); + const validSocials = []; + for (const uuid of uuids) { + const socialMedia = await userSocialMediaRepository.findByUuid(uuid); + if (!socialMedia) throw new NotFoundError('Social media URL not found'); + if (user.uuid !== socialMedia.user.uuid) { + throw new ForbiddenError('User cannot update a social media URL of another user'); + } + validSocials.push(socialMedia); } - return userSocialMediaRepository.upsertSocialMedia(socialMedia, changes); + return validSocials.map((socialMedia, index) => updatedSocialMedia.upsertSocialMedia(socialMedia, changes[index])); }); return updatedSocialMedia; } - public async deleteSocialMediaByUuid(user: UserModel, uuid: Uuid): Promise { + public async deleteSocialMediasByUuid(user: UserModel, uuids: Uuid[]): Promise { const updatedSocialMedia = await this.transactions.readWrite(async (txn) => { const userSocialMediaRepository = Repositories.userSocialMedia(txn); - const socialMedia = await userSocialMediaRepository.findByUuid(uuid); - if (!socialMedia) throw new NotFoundError('Social media URL not found'); - if (user.uuid !== socialMedia.user.uuid) { - throw new ForbiddenError('User cannot delete a social media URL of another user'); + const validSocials = []; + for (const uuid of uuids) { + const socialMedia = await userSocialMediaRepository.findByUuid(uuid); + if (!socialMedia) throw new NotFoundError('Social media URL not found'); + if (user.uuid !== socialMedia.user.uuid) { + throw new ForbiddenError('User cannot delete a social media URL of another user'); + } + validSocials.push(socialMedia); } - return userSocialMediaRepository.deleteSocialMedia(socialMedia); + for (const social of validSocials) userSocialMediaRepository.deleteSocialMedia(social); + return userSocialMediaRepository.getSocialMediaForUser(user); }); return updatedSocialMedia; }