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

Add hasReacted to Feed response data #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion src/api/project/controllers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const mergeEntities = (projects, exclusiveContents, profiles, start, limit, page
owner: extractProfile(profiles, project?.users_permissions_user?.id),
images: project.images,
reaction_count: project.reaction_count,
has_reaction: project.hasReaction,
createdAt: project.createdAt,
}));

Expand Down Expand Up @@ -145,6 +146,8 @@ export default factories.createCoreController(
// GET
async feed(ctx) {
const { page = 1, pageSize = 20 } = ctx.query;
//get userID
const userId = ctx.state.user.id
const limit = Number(pageSize)
const start = (Number(page) - 1) * limit

Expand All @@ -154,10 +157,21 @@ export default factories.createCoreController(
populate: {
users_permissions_user: true,
images: true,
reactions: {
populate: {
users_permissions_user: true,
},
},
},
limit,
start,
});

const updatedProjects = projects.map((project) => ({
...project,
hasReaction: project.reactions.some(({ users_permissions_user }) => users_permissions_user.id === userId),
}));

const exclusiveContents = await contentDocs.findMany({
sort: [{ createdAt: 'desc' }, { reaction_count: 'desc' }],
populate: {
Expand Down Expand Up @@ -187,7 +201,7 @@ export default factories.createCoreController(
});

// Paginate the merged data
const { data, total } = mergeEntities(projects, exclusiveContents, profiles, start, limit, page)
const { data, total } = mergeEntities(updatedProjects, exclusiveContents, profiles, start, limit, page)

// Return the response
return ctx.send({
Expand Down