Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register with email/phone/username #435

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 161 additions & 79 deletions api/src/controllers/communityController.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ const getCommunities = async (req, res) => {
CASE WHEN "creatorId"=${req.user.id} THEN 'true'
ELSE 'false'
END
`), 'isCreator'
`),
'isCreator'
],
[
sequelize.literal(`(
SELECT COUNT("userId")
SELECT COUNT("userId")
FROM communities_users
WHERE "communityId" = communities.id AND active = true AND "userId" = ${req.user.id}
)`),
Expand All @@ -55,13 +56,15 @@ const getCommunities = async (req, res) => {
}
})
const totalPages = Math.ceil(communities.count / pageSize)
res.json({
communities: communities.rows.map(rec => ({ ...rec.dataValues })),
totalItems: communities.count,
totalPages,
page,
pageSize
}).status(200)
res
.json({
communities: communities.rows.map((rec) => ({ ...rec.dataValues })),
totalItems: communities.count,
totalPages,
page,
pageSize
})
.status(200)
} catch (error) {
res.json(error)
}
Expand All @@ -72,11 +75,12 @@ const getCommunities = async (req, res) => {
// @access Public

const getUserCommunities = async (req, res) => {
console.log(req.body)
const pageSize = 10
const page = Number(req.query.pageNumber) || 1
// const order = req.query.order || 'DESC'
// const ordervalue = order && [['name', order]]

console.log(req.user.id)
db.Community.findAndCountAll({
offset: (page - 1) * pageSize,
limit: pageSize,
Expand All @@ -96,11 +100,12 @@ const getUserCommunities = async (req, res) => {
CASE WHEN "creatorId"=${req.user.id} THEN 'true'
ELSE 'false'
END
`), 'isCreator'
`),
'isCreator'
],
[
sequelize.literal(`(
SELECT COUNT("userId")
SELECT COUNT("userId")
FROM communities_users
WHERE "communityId" = communities.id AND active = true AND "userId" = ${req.user.id}
)`),
Expand All @@ -111,23 +116,30 @@ const getUserCommunities = async (req, res) => {
},
order: [['createdAt', 'DESC']],
where: { deleted: false },
include: [{
model: db.User,
as: 'followers',
attributes: [],
where: { id: req.user.id },
through: { attributes: [] }
}]
include: [
{
model: db.User,
as: 'followers',
attributes: [],
where: { id: req.user.id },
through: { attributes: [] }
}
]
})
.then(communities => {
.then((communities) => {
const totalPages = Math.ceil(communities.count / pageSize)
res.json({
communities: communities.rows.map(rec => ({ ...rec.dataValues, attachment: changeFormat(rec.attachment) })),
totalItems: communities.count,
totalPages,
page,
pageSize
}).status(200)
res
.json({
communities: communities.rows.map((rec) => ({
...rec.dataValues,
attachment: changeFormat(rec.attachment)
})),
totalItems: communities.count,
totalPages,
page,
pageSize
})
.status(200)
})
.catch((err) => res.json({ err }).status(400))
}
Expand All @@ -147,8 +159,19 @@ const createCommunity = async (req, res) => {
// auto follow through transactions
if (req.body.auto_follow === 'true') {
const result = await sequelize.transaction(async (t) => {
const community = await db.Community.create({ ...req.body, creatorId: req.user.id, slug: '', attachment: 'community/' + filename }, { transaction: t })
const idArrays = await db.User.findAll({ attributes: ['id'] }, { transaction: t })
const community = await db.Community.create(
{
...req.body,
creatorId: req.user.id,
slug: '',
attachment: 'community/' + filename
},
{ transaction: t }
)
const idArrays = await db.User.findAll(
{ attributes: ['id'] },
{ transaction: t }
)
const allFollow = []

for (let i = 0; i < idArrays.length; i++) {
Expand All @@ -166,11 +189,22 @@ const createCommunity = async (req, res) => {
return res.json({ message: result })
} else {
const followCommunity = await sequelize.transaction(async (t) => {
const community = await db.Community.create({ ...req.body, creatorId: req.user.id, slug: '', attachment: 'community/' + filename }, { transaction: t })
await db.CommunityUser.create({ userId: req.user.id, communityId: community.id }, { transaction: t })
const community = await db.Community.create(
{
...req.body,
creatorId: req.user.id,
slug: '',
attachment: 'community/' + filename
},
{ transaction: t }
)
await db.CommunityUser.create(
{ userId: req.user.id, communityId: community.id },
{ transaction: t }
)
return true
})
if (followCommunity) return res.json({ message: 'Community is Created !!!' }).status(200)
if (followCommunity) { return res.json({ message: 'Community is Created !!!' }).status(200) }
}
} catch (error) {
return res.json({ error: error.message }).status(400)
Expand All @@ -184,9 +218,12 @@ const getCommunityById = async (req, res) => {
const id = req.params.id

db.Community.findByPk(id)
.then(communities => {
.then((communities) => {
if (communities) {
res.json({ ...communities.dataValues, attachment: changeFormat(communities.dataValues.attachment) })
res.json({
...communities.dataValues,
attachment: changeFormat(communities.dataValues.attachment)
})
} else {
res.status(404)
throw new Error('Community not found')
Expand Down Expand Up @@ -214,14 +251,25 @@ const deleteCommunity = async (req, res) => {
}

const result = await sequelize.transaction(async (t) => {
const communityUserIds = await db.CommunityUser.findAll({ where: { communityId: id } }, { transaction: t })
const communityUserIds = await db.CommunityUser.findAll(
{ where: { communityId: id } },
{ transaction: t }
)

communityUserIds.forEach(async function (communityId) {
const { id } = communityId
await db.CommunityUser.update({ active: false }, { where: { id } }, { transaction: t })
await db.CommunityUser.update(
{ active: false },
{ where: { id } },
{ transaction: t }
)
})

await db.Community.update({ deleted: true }, { where: { id } }, { transaction: t })
await db.Community.update(
{ deleted: true },
{ where: { id } },
{ transaction: t }
)

return 'Community Deleted with links.'
})
Expand All @@ -241,9 +289,7 @@ const deleteCommunity = async (req, res) => {
// @access Private
const updateCommunity = async (req, res) => {
try {
const {
name, description, file, toggleActive
} = req.body
const { name, description, file, toggleActive } = req.body

let filename = ''
if (req.file) {
Expand Down Expand Up @@ -274,11 +320,11 @@ const updateCommunity = async (req, res) => {

// converting them to the array of ids not(object with ids)
// to check console.log(followIdArrays) and newFollowIds
const newFollowIds = followIdArrays.map(item => item.userId)
const newUserIds = userIdArrays.map(item => item.id)
const newFollowIds = followIdArrays.map((item) => item.userId)
const newUserIds = userIdArrays.map((item) => item.id)

// this is removing any duplicate items between newFollowIds and newUserIds
const idArrays = newUserIds.filter(item => {
const idArrays = newUserIds.filter((item) => {
return newFollowIds.indexOf(item) === -1
})

Expand All @@ -295,33 +341,41 @@ const updateCommunity = async (req, res) => {
await db.CommunityUser.bulkCreate(allFollow, { transaction: t })

if (!req.file) {
await db.Community.update({ ...req.body, slug: '' },
{ where: { id } })
await db.Community.update(
{ ...req.body, slug: '' },
{ where: { id } }
)
return 'Community is updated with autoFollow'
}

await db.Community.update({ ...req.body, slug: '', attachment: 'community/' + filename },
{ where: { id } })
await db.Community.update(
{ ...req.body, slug: '', attachment: 'community/' + filename },
{ where: { id } }
)
return 'Community is updated with autoFollow'
})

return res.json({ message: result })
} else {
if (!req.file) {
await db.Community.update({
name,
description
},
{ where: { id }, returning: true, attributes: ['id'] })
await db.Community.update(
{
name,
description
},
{ where: { id }, returning: true, attributes: ['id'] }
)
return res.json({ message: 'Community Updated !!!' }).status(200)
}

await db.Community.update({
name,
description,
attachment: 'community/' + filename
},
{ where: { id }, returning: true, attributes: ['id'] })
await db.Community.update(
{
name,
description,
attachment: 'community/' + filename
},
{ where: { id }, returning: true, attributes: ['id'] }
)
return res.json({ message: 'Community Updated !!!' }).status(200)
}
} else {
Expand All @@ -340,11 +394,21 @@ const searchCommunityName = (req, res) => {
const { name } = req.query
const order = req.query.order || 'ASC'

db.Community.findAll({ where: { name: { [Op.iLike]: '%' + name + '%' } }, order: [['name', order]] })
.then(communities => res.json({
communities: communities.map(rec => ({ ...rec.dataValues, attachment: changeFormat(rec.attachment) }))
}).status(200))
.catch(err => res.json({ error: err }).status(400))
db.Community.findAll({
where: { name: { [Op.iLike]: '%' + name + '%' } },
order: [['name', order]]
})
.then((communities) =>
res
.json({
communities: communities.map((rec) => ({
...rec.dataValues,
attachment: changeFormat(rec.attachment)
}))
})
.status(200)
)
.catch((err) => res.json({ error: err }).status(400))
}

// @desc Search usercommunity name
Expand All @@ -355,26 +419,44 @@ const searchUserCommunityName = (req, res) => {
const order = req.query.order || 'ASC'

db.Community.findAll({
include: [{
model: db.User,
as: 'creator' && 'followers',
attributes: ['id'],
where: { id: req.user.id },
through: {
attributes: ['active'],
as: 'followStatus',
where: {
active: true
include: [
{
model: db.User,
as: 'creator' && 'followers',
attributes: ['id'],
where: { id: req.user.id },
through: {
attributes: ['active'],
as: 'followStatus',
where: {
active: true
}
}
}
}],
],
where: { name: { [Op.iLike]: '%' + name + '%' } },
order: [['name', order]]
})
.then(communities => res.json({
communities: communities.map(rec => ({ ...rec.dataValues, attachment: changeFormat(rec.attachment) }))
}).status(200))
.catch(err => res.json({ error: err }).status(400))
.then((communities) =>
res
.json({
communities: communities.map((rec) => ({
...rec.dataValues,
attachment: changeFormat(rec.attachment)
}))
})
.status(200)
)
.catch((err) => res.json({ error: err }).status(400))
}

module.exports = { getCommunities, getUserCommunities, searchUserCommunityName, createCommunity, getCommunityById, deleteCommunity, updateCommunity, searchCommunityName }
module.exports = {
getCommunities,
getUserCommunities,
searchUserCommunityName,
createCommunity,
getCommunityById,
deleteCommunity,
updateCommunity,
searchCommunityName
}
Loading