Skip to content

Commit

Permalink
Preliminary change to the social media responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanDing26 committed Apr 1, 2024
1 parent b524e8c commit 7ebfd80
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
14 changes: 7 additions & 7 deletions api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,26 @@ export class UserController {
}

@Post('/socialMedia')
async insertSocialMediaForUser(@Body() insertSocialMediaRequest: InsertSocialMediaRequest,
async insertSocialMediasForUser(@Body() insertSocialMediaRequests: InsertSocialMediaRequest[],
@AuthenticatedUser() user: UserModel): Promise<InsertSocialMediaResponse> {
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<UpdateSocialMediaResponse> {
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<DeleteSocialMediaResponse> {
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 };
}
}
6 changes: 3 additions & 3 deletions repositories/UserSocialMediaRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export class UserSocialMediaRepository extends BaseRepository<UserSocialMediaMod
}

public async upsertSocialMedia(userSocialMedia: UserSocialMediaModel,
changes?: Partial<UserSocialMediaModel>): Promise<UserSocialMediaModel> {
if (changes) userSocialMedia = UserSocialMediaModel.merge(userSocialMedia, changes);
changes?: Partial<UserSocialMediaModel>[]): Promise<UserSocialMediaModel> {
if (changes) for (const change of changes) userSocialMedia = UserSocialMediaModel.merge(userSocialMedia, change);
return this.repository.save(userSocialMedia);
}

public async deleteSocialMedia(userSocialMedia: UserSocialMediaModel): Promise<UserSocialMediaModel> {
public async deleteSocialMedia(userSocialMedia: UserSocialMediaModel[]): Promise<UserSocialMediaModel[]> {
return this.repository.remove(userSocialMedia);
}

Expand Down
53 changes: 32 additions & 21 deletions services/UserSocialMediaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserSocialMediaModel>): Promise<UserSocialMediaModel> {
public async updateSocialMediasByUuid(user: UserModel,
uuids: Uuid[],
changes: Partial<UserSocialMediaModel>[]): Promise<UserSocialMediaModel> {
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<UserSocialMediaModel> {
public async deleteSocialMediasByUuid(user: UserModel, uuids: Uuid[]): Promise<UserSocialMediaModel[]> {
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;
}
Expand Down

0 comments on commit 7ebfd80

Please sign in to comment.