Skip to content

Commit

Permalink
Add interestCategories Mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewquang512 committed Nov 26, 2023
1 parent ab40987 commit 94a6c18
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/Type_Definitions/User_User.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gql from 'graphql-tag';
import commonDefs from './Common_Common.js';
import skillDefs from './Skill_Skill.js';
import categoryDefs from './Category_Category.js';

const userDefs = gql`
extend type Query {
Expand Down Expand Up @@ -58,6 +59,8 @@ const userDefs = gql`
addSkill(data: AddSkillInput!): User!
endorseSkill(data: EndorseSkillInput!): User!
unEndorseSkill(data: EndorseSkillInput!): User!
addInterestCategories(data: InterestCategoriesInput!): User!
removeInterestCategories(data: InterestCategoriesInput!): User!
}
input EndorseSkillInput {
Expand All @@ -74,6 +77,11 @@ const userDefs = gql`
content: String!
}
input InterestCategoriesInput {
userId: ID!
categoryIds: [String]!
}
input AddSkillInput {
userId: ID!
skillIds: [String]!
Expand Down Expand Up @@ -130,7 +138,9 @@ const userDefs = gql`
biography: String
userEndorsements: [Endorsement]
interestCategories: [Category]
}
${categoryDefs}
type Endorsement {
id: ID!
Expand Down
2 changes: 1 addition & 1 deletion src/prisma/ERD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ model User {
endorsementIds String[] @db.ObjectId
endorsements Endorsement[] @relation(name: "endorser", fields: [endorsementIds], references: [id])
interestCategoryIds String[] @db.ObjectId
interestCategories Category[] @relation(name: "interest", fields: [interestCategoryIds], references: [id])
}

// model Notification {
Expand Down Expand Up @@ -167,6 +171,9 @@ model Category {
posts String[] @db.ObjectId
category_to_post Post[] @relation(fields: [posts], references: [id])
interestUserIds String[] @db.ObjectId
category_to_user User[] @relation(name: "interest", fields: [interestUserIds], references: [id])
}

model Album {
Expand Down
20 changes: 13 additions & 7 deletions src/resolvers/Mutation/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ const profileMutation = {
if (!user) {
throw Error('User is not existed');
}

const existedSkills = await prisma.skill.findMany({
where: {
id: {
in: skillIds,
},
},
});

if (existedSkills.length !== skillIds.length) {
throw Error('Some of skill not existed');
}

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

skillIds.forEach((id) => {
Expand All @@ -54,13 +67,6 @@ const profileMutation = {
},
data: {
user_to_endorsement: {
// create: {
// skill: {
// connect: {
// id: skillId,
// },
// },
// },
create: skillIds.map((id) => {
return {
skill: {
Expand Down
68 changes: 68 additions & 0 deletions src/resolvers/Mutation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const userMutation = {
}
return user;
},

// TODO: Update delele User that lead to all records relate to user also be deleted
deleteUser: async (parent, args, info) => {
let user;
try {
Expand Down Expand Up @@ -85,6 +87,72 @@ const userMutation = {

return updatedUser;
},

/**
* @param {*} parent
* @param {{data: {categoryIds: string[], userId: string}}} args
* @param {*} info
* @returns
*/
addInterestCategories: async (parent, args, info) => {
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 already 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: {
connect: categoryIds.map((id) => {
return {
id: id,
};
}),
},
},
});
},

/**
* @param {*} parent
* @param {{data: {categoryIds: string, userId: string}}} args
* @param {*} info
* @returns
*/
// Todo done this
removeInterestCategories: async (parent, args, info) => {
throw Error('Not implement yet');
},
};

export default userMutation;
10 changes: 10 additions & 0 deletions src/resolvers/Type/User___.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ const User = {
},
});
},

interestCategories: async (parent, args, info) => {
return await prisma.category.findMany({
where: {
interestUserIds: {
has: parent.id,
},
},
});
},
};

export default User;

0 comments on commit 94a6c18

Please sign in to comment.