Skip to content

Commit

Permalink
Update get comment duplicate, add removeInterestCategories, add unEnd…
Browse files Browse the repository at this point in the history
…orseSkill, update jaacard algo
  • Loading branch information
andrewquang512 committed Dec 2, 2023
1 parent 3614f5f commit 5fbafd5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/resolvers/Common/suggestUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class UserSuggestion {
*/
#getJaacardIndex = (dataset1, dataset2) => {
if (dataset1.length === 0 || dataset2.length === 0) {
return 0;
return 1;
}
const intersection = this.#getIntersection(dataset1, dataset2);
const union = this.#getUnion(dataset1, dataset2);
Expand Down
68 changes: 64 additions & 4 deletions src/resolvers/Mutation/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ const profileMutation = {

const userSkillIds = user.endorsements.map((each) => each.skillId);

skillIds.forEach((id) => {
const newSkillIds = skillIds.filter((id) => {
if (userSkillIds.includes(id)) {
throw Error('User has already added this skill');
console.log(`User has already added this skill ${id}`);
return false;
}
return true;
});

return await prisma.user.update({
Expand All @@ -67,7 +69,7 @@ const profileMutation = {
},
data: {
endorsements: {
create: skillIds.map((id) => {
create: newSkillIds.map((id) => {
return {
skill: {
connect: {
Expand Down Expand Up @@ -158,7 +160,65 @@ const profileMutation = {
*/
// Todo done this
unEndorseSkill: async (parent, args, info) => {
throw Error('Not implement yet');
const { endorsementId, endorserUserId } = args.data;
const [targetUser, endorser] = await Promise.all([
prisma.user.findFirst({
where: {
endorsements: {
some: {
id: endorsementId,
},
},
},
include: {
endorsements: {
include: {
skill: true,
},
},
},
}),
prisma.user.findUnique({
where: {
id: endorserUserId,
},
}),
]);

if (!targetUser) {
throw Error('User not have this skill');
}

if (!endorser) {
throw Error('Endorsing User is not existed');
}

if (endorser.id === targetUser.id) {
throw Error('endorser and targetUser can not be the same');
}

const endorsement = targetUser.endorsements.find(
(each) => each.id === endorsementId,
);

const endorserIds = endorsement.endorserIds;

if (!endorserIds.includes(endorserUserId)) {
throw Error('User have not endorsed this skill of user');
}

return await prisma.endorsement.update({
where: {
id: endorsementId,
},
data: {
endorser: {
disconnect: {
id: endorserUserId,
},
},
},
});
},
};

Expand Down
48 changes: 46 additions & 2 deletions src/resolvers/Mutation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,53 @@ const userMutation = {
* @param {*} info
* @returns
*/
// Todo done this
removeInterestCategories: async (parent, args, info) => {
throw Error('Not implement yet');
const { categoryIds, userId } = args.data;

const user = await prisma.user.findUnique({
where: {
id: userId,
},
include: {
interestCategories: true,
},
});
if (!user) {
throw Error('User is not existed');
}

categoryIds.forEach((id) => {
if (!user.interestCategoryIds.includes(id)) {
throw Error('User has not added this category to interest');
}
});

const existedCategories = await prisma.category.findMany({
where: {
id: {
in: categoryIds,
},
},
});

if (existedCategories.length !== categoryIds.length) {
throw Error('Some of category not existed');
}

return await prisma.user.update({
where: {
id: userId,
},
data: {
interestCategories: {
disconnect: categoryIds.map((id) => {
return {
id: id,
};
}),
},
},
});
},
};

Expand Down
1 change: 1 addition & 0 deletions src/resolvers/Query/COMMENT.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const commentQuery = {
}),
where: {
postId: postId,
parentId: null,
},
include: {
child: {
Expand Down

0 comments on commit 5fbafd5

Please sign in to comment.