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);
};
+5 -9
View File
@@ -14,7 +14,7 @@ const Comment = {
user({author_id}, _, {loaders: {Users}}) {
return Users.getByID.load(author_id);
},
replies({id, asset_id, reply_count}, {query: {sort, sortBy, limit, excludeIgnored}}, {loaders: {Comments}}) {
replies({id, asset_id, reply_count}, {query}, {loaders: {Comments}}) {
// Don't bother looking up replies if there aren't any there!
if (reply_count === 0) {
@@ -24,14 +24,10 @@ const Comment = {
};
}
return Comments.getByQuery({
asset_id,
parent_id: id,
sort,
sortBy,
limit,
excludeIgnored,
});
query.asset_id = asset_id;
query.parent_id = id;
return Comments.getByQuery(query);
},
replyCount({reply_count}) {
+1 -1
View File
@@ -2,7 +2,7 @@ const {SEARCH_OTHER_USERS} = require('../../perms/constants');
const CommentStatusHistory = {
assigned_by({assigned_by}, _, {user, loaders: {Users}}) {
if (!user || !user.can(SEARCH_OTHER_USERS) || assigned_by != null) {
if (!user || !user.can(SEARCH_OTHER_USERS) || assigned_by == null) {
return null;
}
+7 -7
View File
@@ -50,24 +50,25 @@ const RootQuery = {
return Comments.getCountByQuery(query);
},
assetMetrics(_, {from, to, sort, limit = 10}, {user, loaders: {Metrics: {Assets}}}) {
assetMetrics(_, query, {user, loaders: {Metrics: {Assets}}}) {
if (user == null || !user.can(SEARCH_ASSETS)) {
return null;
}
if (sort === 'ACTIVITY') {
return Assets.getActivity({from, to, limit});
const {sortBy} = query;
if (sortBy === 'ACTIVITY') {
return Assets.getActivity(query);
}
return Assets.get({from, to, sort, limit});
return Assets.get(query);
},
commentMetrics(_, {from, to, sort, limit = 10}, {user, loaders: {Metrics: {Comments}}}) {
commentMetrics(_, query, {user, loaders: {Metrics: {Comments}}}) {
if (user == null || !user.can(SEARCH_COMMENT_METRICS)) {
return null;
}
return Comments.get({from, to, sort, limit});
return Comments.get(query);
},
// This returns the current user, ensure that if we aren't logged in, we
@@ -97,7 +98,6 @@ const RootQuery = {
}
const {action_type} = query;
if (action_type) {
query.ids = await Actions.getByTypes({action_type, item_type: 'USERS'});
query.statuses = ['PENDING'];
+5 -5
View File
@@ -132,7 +132,7 @@ input UsersQuery {
cursor: Cursor
# Sort the results by created_at.
sort: SORT_ORDER = DESC
sortOrder: SORT_ORDER = DESC
}
# AssetsQuery allows teh ability to query assets by specific fields
@@ -247,7 +247,7 @@ input CommentsQuery {
cursor: Cursor
# Sort the results by from largest first.
sort: SORT_ORDER = DESC
sortOrder: SORT_ORDER = DESC
# The order to sort the comments by, sorting by default the created at
# timestamp.
@@ -263,7 +263,7 @@ input CommentsQuery {
input RepliesQuery {
# Sort the results by from smallest first.
sort: SORT_ORDER = ASC
sortOrder: SORT_ORDER = ASC
# The order to sort the comments by, sorting by default the created at
# timestamp.
@@ -737,11 +737,11 @@ type RootQuery {
# Asset metrics related to user actions are saturated into the assets
# returned. Parameters `from` and `to` are related to the action created_at field.
assetMetrics(from: Date!, to: Date!, sort: ASSET_METRICS_SORT!, limit: Int = 10): [Asset!]
assetMetrics(from: Date!, to: Date!, sortBy: ASSET_METRICS_SORT!, limit: Int = 10): [Asset!]
# Comment metrics related to user actions are saturated into the comments
# returned. Parameters `from` and `to` are related to the action created_at field.
commentMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Comment!]
commentMetrics(from: Date!, to: Date!, sortBy: ACTION_TYPE!, limit: Int = 10): [Comment!]
}
################################################################################