changed sort -> sortOrder

This commit is contained in:
Wyatt Johnson
2017-08-25 09:59:37 -06:00
parent 48595a1bbf
commit 8a5db167ff
14 changed files with 75 additions and 79 deletions
+11 -11
View File
@@ -189,11 +189,11 @@ const getEndCursor = (ctx, nodes, {cursor, sortBy}) => {
* @param {Object} query the current mongoose query object
* @param {Object} params the params from the client describing the query
*/
const applySort = (ctx, query, {cursor, sort, sortBy}) => {
const applySort = (ctx, query, {cursor, sortOrder, sortBy}) => {
switch (sortBy) {
case 'CREATED_AT': {
if (cursor) {
if (sort === 'DESC') {
if (sortOrder === 'DESC') {
query = query.where({
created_at: {
$lt: cursor,
@@ -208,14 +208,14 @@ const applySort = (ctx, query, {cursor, sort, sortBy}) => {
}
}
return query.sort({created_at: sort === 'DESC' ? -1 : 1});
return query.sort({created_at: sortOrder === 'DESC' ? -1 : 1});
}
case 'REPLIES': {
if (cursor) {
query = query.skip(cursor);
}
return query.sort({reply_count: sort === 'DESC' ? -1 : 1, created_at: sort === 'DESC' ? -1 : 1});
return query.sort({reply_count: sortOrder === 'DESC' ? -1 : 1, created_at: sortOrder === 'DESC' ? -1 : 1});
}
}
@@ -224,7 +224,7 @@ const applySort = (ctx, query, {cursor, sort, sortBy}) => {
throw new Error(`unable to sort by ${sortBy}, no plugin was provided to handle this type`);
}
return ctx.plugins.Sort.Comments[SORT_KEY].sort(ctx, query, {cursor, sort});
return ctx.plugins.Sort.Comments[SORT_KEY].sort(ctx, query, {cursor, sortOrder});
};
/**
@@ -236,10 +236,10 @@ const applySort = (ctx, query, {cursor, sort, sortBy}) => {
* @param {Object} query the current mongoose query object
* @param {Object} params the params from the client describing the query
*/
const executeWithSort = async (ctx, query, {cursor, sort, sortBy, limit}) => {
const executeWithSort = async (ctx, query, {cursor, sortOrder, sortBy, limit}) => {
// Apply the sort to the query.
query = applySort(ctx, query, {cursor, sort, sortBy});
query = applySort(ctx, query, {cursor, sortOrder, sortBy});
// Apply the limit (if it exists, as it's applied universally).
if (limit) {
@@ -263,8 +263,8 @@ const executeWithSort = async (ctx, query, {cursor, sort, sortBy, limit}) => {
// Use the generator functions below to extract the cursor details based on
// the current sortBy parameter.
return {
startCursor: getStartCursor(ctx, nodes, {cursor, sort, sortBy, limit}),
endCursor: getEndCursor(ctx, nodes, {cursor, sort, sortBy, limit}),
startCursor: getStartCursor(ctx, nodes, {cursor, sortOrder, sortBy, limit}),
endCursor: getEndCursor(ctx, nodes, {cursor, sortOrder, sortBy, limit}),
hasNextPage,
nodes,
};
@@ -277,7 +277,7 @@ const executeWithSort = async (ctx, query, {cursor, sort, sortBy, limit}) => {
* @param {Object} context graph context
* @param {Object} query query terms to apply to the comments query
*/
const getCommentsByQuery = async (ctx, {ids, statuses, asset_id, parent_id, author_id, limit, cursor, sort, sortBy, excludeIgnored, tags, action_type}) => {
const getCommentsByQuery = async (ctx, {ids, statuses, asset_id, parent_id, author_id, limit, cursor, sortOrder, sortBy, excludeIgnored, tags, action_type}) => {
let comments = CommentModel.find();
// Only administrators can search for comments with statuses that are not
@@ -343,7 +343,7 @@ const getCommentsByQuery = async (ctx, {ids, statuses, asset_id, parent_id, auth
});
}
return executeWithSort(ctx, comments, {cursor, sort, sortBy, limit});
return executeWithSort(ctx, comments, {cursor, sortOrder, sortBy, limit});
};
/**
+12 -12
View File
@@ -54,13 +54,13 @@ const getAssetActivityMetrics = ({loaders: {Assets}}, {from, to, limit}) => {
/**
* Returns a list of assets with action metadata included on the models.
*/
const getAssetMetrics = async ({loaders: {Metrics, Assets, Comments}}, {from, to, sort, limit}) => {
const getAssetMetrics = async ({loaders: {Metrics, Assets, Comments}}, {from, to, sortBy, limit}) => {
// Get the recent actions.
let actionSummaries = await Metrics.getRecentActions.load({from, to});
let commentMetrics = actionSummaries.reduce((acc, {item_id, action_type, count}) => {
if (action_type !== sort) {
if (action_type !== sortBy) {
return acc;
}
@@ -94,9 +94,9 @@ const getAssetMetrics = async ({loaders: {Metrics, Assets, Comments}}, {from, to
return {action_summaries, id: asset_id};
})
.filter((asset) => {
let contextActionSummary = asset.action_summaries.find((({action_type}) => action_type === sort));
let contextActionSummary = asset.action_summaries.find((({action_type}) => action_type === sortBy));
if (contextActionSummary === null || contextActionSummary.actionCount === 0) {
return false;
}
@@ -108,8 +108,8 @@ const getAssetMetrics = async ({loaders: {Metrics, Assets, Comments}}, {from, to
// if the action summary does not exist on the object, that it is less
// prefered over the one that does have it.
.sort((a, b) => {
let aActionSummary = a.action_summaries.find((({action_type}) => action_type === sort));
let bActionSummary = b.action_summaries.find((({action_type}) => action_type === sort));
let aActionSummary = a.action_summaries.find((({action_type}) => action_type === sortBy));
let bActionSummary = b.action_summaries.find((({action_type}) => action_type === sortBy));
// Both of them had an actionCount, hence we can determine that we could
// compare the actual values directly.
@@ -144,15 +144,15 @@ const getAssetMetrics = async ({loaders: {Metrics, Assets, Comments}}, {from, to
* Returns a list of comments that are retrieved based on most activity within
* the indicated time range.
*/
const getCommentMetrics = async ({loaders: {Metrics, Comments}}, {from, to, sort, limit}) => {
const getCommentMetrics = async ({loaders: {Metrics, Comments}}, {from, to, sortBy, limit}) => {
let commentActionSummaries = {};
let actionSummaries = await Metrics.getRecentActions.load({from, to});
actionSummaries.sort((a, b) => {
let aActionSummary = a.action_type === sort ? a : null;
let bActionSummary = b.action_type === sort ? b : null;
let aActionSummary = a.action_type === sortBy ? a : null;
let bActionSummary = b.action_type === sortBy ? b : null;
// If either a or b don't have this action type, then one of them will
// automatically win.
@@ -178,7 +178,7 @@ const getCommentMetrics = async ({loaders: {Metrics, Comments}}, {from, to, sort
// Grab the comment id's for comment where they have at least one of the
// actions being sorted by.
let commentIDs = Object.keys(commentActionSummaries).filter((item_id) => {
let contextActionSummary = commentActionSummaries[item_id].find(({action_type}) => action_type === sort);
let contextActionSummary = commentActionSummaries[item_id].find(({action_type}) => action_type === sortBy);
if (contextActionSummary == null) {
return false;
}
@@ -247,11 +247,11 @@ module.exports = (context) => ({
cacheKeyFn: objectCacheKeyFn('from', 'to')
}),
Assets: {
get: ({from, to, sort, limit}) => getAssetMetrics(context, {from, to, sort, limit}),
get: ({from, to, sortBy, limit}) => getAssetMetrics(context, {from, to, sortBy, limit}),
getActivity: ({from, to, limit}) => getAssetActivityMetrics(context, {from, to, limit}),
},
Comments: {
get: ({from, to, sort, limit}) => getCommentMetrics(context, {from, to, sort, limit}),
get: ({from, to, sortBy, limit}) => getCommentMetrics(context, {from, to, sortBy, limit}),
}
}
});
+3 -3
View File
@@ -15,7 +15,7 @@ const genUserByIDs = (context, ids) => UsersService
* @param {Object} context graph context
* @param {Object} query query terms to apply to the users query
*/
const getUsersByQuery = ({user}, {ids, limit, cursor, statuses = null, sort}) => {
const getUsersByQuery = ({user}, {ids, limit, cursor, statuses = null, sortOrder}) => {
let users = UserModel.find();
@@ -36,7 +36,7 @@ const getUsersByQuery = ({user}, {ids, limit, cursor, statuses = null, sort}) =>
}
if (cursor) {
if (sort === 'DESC') {
if (sortOrder === 'DESC') {
users = users.where({
created_at: {
$lt: cursor
@@ -52,7 +52,7 @@ const getUsersByQuery = ({user}, {ids, limit, cursor, statuses = null, sort}) =>
}
return users
.sort({created_at: sort === 'DESC' ? -1 : 1})
.sort({created_at: sortOrder === 'DESC' ? -1 : 1})
.limit(limit);
};