Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dowhep committed Apr 1, 2024
1 parent c7c64df commit 636192c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
16 changes: 9 additions & 7 deletions services/UserSocialMediaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ export default class UserSocialMediaService {
const addedSocialMedia = await this.transactions.readWrite(async (txn) => {
// checking duplicate
const setDuplicateType = new Set();
for (const socialMedia of socialMedias) {
const type = socialMedia.type;
socialMedias.forEach((socialMedia) => {
const { type } = socialMedia;
if (setDuplicateType.has(type)) {
throw new UserError(`Dupllicate type "${type}" found in the request`);
}
setDuplicateType.add(type);
}
});

// inserting social media
const userSocialMediaRepository = Repositories.userSocialMedia(txn);
const upsertedSocialMedias = await Promise.all(socialMedias.map(async (socialMedia) => {
const isNewSocialMediaType = await userSocialMediaRepository.isNewSocialMediaTypeForUser(user, socialMedia.type);
const isNewSocialMediaType = await userSocialMediaRepository
.isNewSocialMediaTypeForUser(user, socialMedia.type);
if (!isNewSocialMediaType) {
throw new UserError(`Social media URL of type "${socialMedia.type}" has already been created for this user`);
}
Expand All @@ -53,13 +54,14 @@ export default class UserSocialMediaService {
const updatedSocialMedia = await this.transactions.readWrite(async (txn) => {
// checking duplicate
const setDuplicateUuid = new Set();
for (const socialMediaPatch of changes) {
const uuid = socialMediaPatch.uuid;

changes.forEach((socialMediaPatch) => {
const { uuid } = socialMediaPatch;
if (setDuplicateUuid.has(uuid)) {
throw new UserError(`Dupllicate UUID "${uuid}" found in the request`);
}
setDuplicateUuid.add(uuid);
}
});

// patching social media
const userSocialMediaRepository = Repositories.userSocialMedia(txn);
Expand Down
39 changes: 18 additions & 21 deletions tests/userSocialMedia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ describe('social media URL submission', () => {
const conn = await DatabaseConnection.get();
let member1 = UserFactory.fake();
const socialMediaTypes = [SocialMediaType.FACEBOOK, SocialMediaType.TWITTER, SocialMediaType.LINKEDIN];
const userSocialMediaList = socialMediaTypes.map(type =>
UserSocialMediaFactory.fake({ user: member1, type })
);
const userSocialMediaList = socialMediaTypes.map((type) => UserSocialMediaFactory.fake({ user: member1, type }));
// sort by type to ensure order is consistent
userSocialMediaList.sort((a, b) => a.type.localeCompare(b.type));

Expand Down Expand Up @@ -66,7 +64,8 @@ describe('social media URL submission', () => {
const userController = ControllerFactory.user(conn);

const userSocialMediaWithSameType = UserSocialMediaFactory.fake({ type: SocialMediaType.FACEBOOK });
const errorMessage = `Social media URL of type "${SocialMediaType.FACEBOOK}" has already been created for this user`;
const errorMessage = `Social media URL of type "${SocialMediaType.FACEBOOK}"`
+ ' has already been created for this user';
await expect(userController.insertSocialMediaForUser({ socialMedia: [userSocialMediaWithSameType] }, member))
.rejects.toThrow(errorMessage);
});
Expand All @@ -80,11 +79,9 @@ describe('social media URL update', () => {
test('is invalidated when updating social media URL of another user', async () => {
const conn = await DatabaseConnection.get();
let member1 = UserFactory.fake();
let unauthorizedMember = UserFactory.fake();
const unauthorizedMember = UserFactory.fake();
const socialMediaTypes = [SocialMediaType.FACEBOOK, SocialMediaType.TWITTER, SocialMediaType.LINKEDIN];
const userSocialMediaList = socialMediaTypes.map(type =>
UserSocialMediaFactory.fake({ user: member1, type })
);
const userSocialMediaList = socialMediaTypes.map((type) => UserSocialMediaFactory.fake({ user: member1, type }));
// sort by type to ensure order is consistent
userSocialMediaList.sort((a, b) => a.type.localeCompare(b.type));

Expand All @@ -101,7 +98,7 @@ describe('social media URL update', () => {
const errorMessage = 'User cannot update a social media URL of another user';
const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: member1.userSocialMedia[0].uuid
uuid: member1.userSocialMedia[0].uuid,
}] };
await expect(userController.updateSocialMediaForUser(socialMediaParams, unauthorizedMember))
.rejects.toThrow(errorMessage);
Expand All @@ -119,8 +116,8 @@ describe('social media URL update', () => {

const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: faker.datatype.uuid()
}]};
uuid: faker.datatype.uuid(),
}] };
const errorMessage = `Social media of UUID "${socialMediaParams.socialMedia[0].uuid}" not found`;
await expect(userController.updateSocialMediaForUser(socialMediaParams, member))
.rejects.toThrow(errorMessage);
Expand Down Expand Up @@ -207,8 +204,8 @@ describe('social media URL update concurrency', () => {
const index = 0;
const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: member.userSocialMedia[index].uuid
}]};
uuid: member.userSocialMedia[index].uuid,
}] };
await userController.updateSocialMediaForUser(socialMediaParams, member);

const expectedUserSocialMedia0 = {
Expand All @@ -233,8 +230,8 @@ describe('social media URL update concurrency', () => {
const index = 1;
const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: member.userSocialMedia[index].uuid
}]};
uuid: member.userSocialMedia[index].uuid,
}] };
await userController.updateSocialMediaForUser(socialMediaParams, member);

const expectedUserSocialMedia1 = {
Expand All @@ -259,8 +256,8 @@ describe('social media URL update concurrency', () => {
const index = 2;
const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: member.userSocialMedia[index].uuid
}]};
uuid: member.userSocialMedia[index].uuid,
}] };
await userController.updateSocialMediaForUser(socialMediaParams, member);

const expectedUserSocialMedia2 = {
Expand All @@ -285,8 +282,8 @@ describe('social media URL update concurrency', () => {
const index = 3;
const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: member.userSocialMedia[index].uuid
}]};
uuid: member.userSocialMedia[index].uuid,
}] };
await userController.updateSocialMediaForUser(socialMediaParams, member);

const expectedUserSocialMedia3 = {
Expand All @@ -311,8 +308,8 @@ describe('social media URL update concurrency', () => {
const index = 4;
const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: member.userSocialMedia[index].uuid
}]};
uuid: member.userSocialMedia[index].uuid,
}] };
await userController.updateSocialMediaForUser(socialMediaParams, member);

const expectedUserSocialMedia4 = {
Expand Down

0 comments on commit 636192c

Please sign in to comment.