Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-Server into develop
  • Loading branch information
HUNG-rushb committed Nov 26, 2023
2 parents cbf8ffd + 94a6c18 commit ce1a9cc
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ IS_LOGGING=1
# For tool generatePosts
AWS_ACCESS_KEY=
AWS_SECRET_ACCESS=
AWS_BUCKET_NAME=
AWS_BUCKET_NAME=
APP_ID=
12 changes: 11 additions & 1 deletion 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,9 +77,14 @@ const userDefs = gql`
content: String!
}
input InterestCategoriesInput {
userId: ID!
categoryIds: [String]!
}
input AddSkillInput {
userId: ID!
skillId: String!
skillIds: [String]!
}
input CreateUserInput {
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: 6 additions & 1 deletion src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ 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 @@ -172,6 +174,9 @@ model Category {
stories String[] @db.ObjectId
category_to_story Story[] @relation(fields: [stories], references: [id])
interestUserIds String[] @db.ObjectId
category_to_user User[] @relation(name: "interest", fields: [interestUserIds], references: [id])
}

model Album {
Expand Down
44 changes: 31 additions & 13 deletions src/resolvers/Mutation/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const profileMutation = {

/**
* @param {*} parent
* @param {{data: {skillId: string, userId: string}}} args
* @param {{data: {skillIds: string[], userId: string}}} args
* @param {*} info
* @returns
*/
addSkill: async (parent, args, info) => {
const { skillId, userId } = args.data;
const { skillIds, userId } = args.data;

const user = await prisma.user.findUnique({
where: {
Expand All @@ -40,31 +40,49 @@ const profileMutation = {
if (!user) {
throw Error('User is not existed');
}
const userSkillIds = user.user_to_endorsement.map((each) => each.skillId);

if (userSkillIds.includes(skillId)) {
throw Error('User has already added this skill');
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) => {
if (userSkillIds.includes(id)) {
throw Error('User has already added this skill');
}
});

return await prisma.user.update({
where: {
id: userId,
},
data: {
user_to_endorsement: {
create: {
skill: {
connect: {
id: skillId,
create: skillIds.map((id) => {
return {
skill: {
connect: {
id: id,
},
},
},
},
};
}),
},
},
});
},

/**
/**.map
*
* @param {*} parent
* @param {{data: {endorsementId: string, endorserUserId: string}}} args
* @param {*} info
Expand Down Expand Up @@ -123,7 +141,7 @@ const profileMutation = {
id: endorsementId,
},
data: {
endorsers: {
endorser: {
connect: {
id: endorserUserId,
},
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;
24 changes: 21 additions & 3 deletions tools/generatePosts/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
bucket_name = os.getenv("AWS_BUCKET_NAME")
graphql_api_url = "http://localhost:4000/graphql"

image_urls = ["https://picsum.photos"] * 100

def getMockImageURLsMethodOne(count):
return ["https://picsum.photos"] * count


def getMockImageURLsMethodTwo(count):
TOTAL = 873
MAX_LIMIT = 50
MIN_LIMIT = 5

url = "https://dummyapi.io/data/v1/post"
raise Exception("Not Complete yet")


# Get user list from database
current_user_list = [
Expand Down Expand Up @@ -112,8 +124,14 @@ def upload_images_to_s3(urls, bucket):
print(f"Error uploading {url} to {bucket}: {str(e)}")


t1 = threading.Thread(target=upload_images_to_s3, args=(image_urls, bucket_name))
t2 = threading.Thread(target=upload_images_to_s3, args=(image_urls, bucket_name))
t1 = threading.Thread(
target=upload_images_to_s3,
args=(getMockImageURLsMethodOne(50), bucket_name),
)
t2 = threading.Thread(
target=upload_images_to_s3,
args=(getMockImageURLsMethodOne(50), bucket_name),
)

t1.start()
t2.start()
Expand Down

0 comments on commit ce1a9cc

Please sign in to comment.