Skip to content

Commit

Permalink
🔄 refactor: Update shared links pagination and response structure; re…
Browse files Browse the repository at this point in the history
…place pageNumber with cursor for improved data fetching
  • Loading branch information
berry-13 committed Jan 8, 2025
1 parent 4e6582e commit 4d62d7c
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 534 deletions.
52 changes: 25 additions & 27 deletions api/models/Share.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,56 +95,54 @@ async function getSharedMessages(shareId) {
}
}

async function getSharedLinks(
user,
pageNumber = 1,
pageSize = 25,
isPublic = true,
sortBy = 'createdAt',
sortDirection = 'desc',
search = '',
) {
async function getSharedLinks(user, pageParam, pageSize, isPublic, sortBy, sortDirection, search) {
try {
const query = { user, isPublic };

if (pageParam) {
if (sortDirection === 'desc') {
query[sortBy] = { $lt: pageParam };
} else {
query[sortBy] = { $gt: pageParam };
}
}

if (search) {
const searchResults = await Conversation.meiliSearch(search, {
attributesToHighlight: ['title'],
});

const conversationIds = searchResults.hits.map((hit) => hit.id);

query['conversation'] = { $in: conversationIds };
}

const sort = {};
sort[sortBy] = sortDirection === 'desc' ? -1 : 1;

const [totalConvos, sharedLinks] = await Promise.all([
SharedLink.countDocuments(query).exec(),
SharedLink.find(query)
.sort(sort)
.skip((pageNumber - 1) * pageSize)
.limit(pageSize)
.select('-__v -user')
.lean()
.exec(),
]);
const sharedLinks = await SharedLink.find(query)
.sort(sort)
.limit(pageSize + 1)
.select('-__v -user')
.lean()
.exec();

const hasNextPage = sharedLinks.length > pageSize;
const links = sharedLinks.slice(0, pageSize);

const nextCursor = hasNextPage ? links[links.length - 1][sortBy] : undefined;

return {
sharedLinks: sharedLinks.map((link) => ({
links: links.map((link) => ({
shareId: link.shareId,
title: link.conversation?.title || 'Untitled',
title: link?.title || 'Untitled',
isPublic: link.isPublic,
createdAt: link.createdAt,
})),
totalCount: totalConvos,
pages: Math.ceil((totalConvos || 1) / pageSize),
pageNumber,
pageSize,
nextCursor,
hasNextPage,
};
} catch (error) {
logger.error('[getShareByPage] Error getting shares', {
logger.error('[getSharedLinks] Error getting shares', {
error: error.message,
user,
});
Expand Down
12 changes: 5 additions & 7 deletions api/server/routes/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (allowSharedLinks) {
router.get('/', requireJwtAuth, async (req, res) => {
try {
const params = {
pageNumber: Math.max(1, parseInt(req.query.pageNumber) || 1),
pageParam: req.query.cursor,
pageSize: Math.max(1, parseInt(req.query.pageSize) || 10),
isPublic: isEnabled(req.query.isPublic),
sortBy: ['createdAt', 'title'].includes(req.query.sortBy) ? req.query.sortBy : 'createdAt',
Expand All @@ -59,7 +59,7 @@ router.get('/', requireJwtAuth, async (req, res) => {

const result = await getSharedLinks(
req.user.id,
params.pageNumber,
params.pageParam,
params.pageSize,
params.isPublic,
params.sortBy,
Expand All @@ -68,11 +68,9 @@ router.get('/', requireJwtAuth, async (req, res) => {
);

res.status(200).send({
links: result.sharedLinks,
totalCount: result.totalCount,
pageNumber: result.pageNumber,
pageSize: result.pageSize,
pages: result.pages,
links: result.links,
nextCursor: result.nextCursor,
hasNextPage: result.hasNextPage,
});
} catch (error) {
console.error('Error getting shared links:', error);
Expand Down
Loading

0 comments on commit 4d62d7c

Please sign in to comment.