Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
feat: enable many group IDs per invite & use query instead of aggregate
Browse files Browse the repository at this point in the history
Signed-off-by: Loan Laux <[email protected]>
  • Loading branch information
loan-laux committed May 26, 2020
1 parent af47d72 commit 282e7d0
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 97 deletions.
34 changes: 34 additions & 0 deletions src/queries/groupsById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ReactionError from "@reactioncommerce/reaction-error";

/**
* @name groupsById
* @method
* @memberof Accounts/NoMeteorQueries
* @summary query the Groups collection and return a MongoDB cursor
* @param {Object} context - an object containing the per-request state
* @param {Array|String} groupIds - IDs of the groups to get
* @returns {Array|Object} Group objects
*/
export default async function groupsById(context, groupIds) {
const { collections } = context;
const { Groups } = collections;

const groups = await Groups.find({
_id: {
$in: groupIds
}
}).toArray();


if (groups.length === 0) {
throw new ReactionError("not-found", "No groups matching the provided IDs were found");
}

if (groups.length !== groupIds.length) {
throw new ReactionError("not-found", `Could not find ${groupIds.length - groups.length} of ${groupIds.length} groups provided`);
}

await Promise.all(groups.map((group) => context.validatePermissions("reaction:legacy:groups", "read", { shopId: group.shopId })));

return groups;
}
6 changes: 4 additions & 2 deletions src/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import accounts from "./accounts.js";
import group from "./group.js";
import groups from "./groups.js";
import groupsByAccount from "./groupsByAccount.js";
import invitationsAggregate from "./invitationsAggregate.js";
import groupsById from "./groupsById.js";
import invitations from "./invitations.js";
import userAccount from "./userAccount.js";

export default {
Expand All @@ -12,6 +13,7 @@ export default {
group,
groups,
groupsByAccount,
invitationsAggregate,
groupsById,
invitations,
userAccount
};
28 changes: 28 additions & 0 deletions src/queries/invitations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @name accounts
* @method
* @memberof Accounts/NoMeteorQueries
* @summary Returns accounts optionally filtered by group IDs
* @param {Object} context - an object containing the per-request state
* @param {String} input - input for query
* @param {String} [input.shopIds] - Array of shop IDs to limit the results
* @returns {Promise} Mongo cursor
*/
export default async function accounts(context, { shopIds }) {
const { collections } = context;
const { AccountInvites } = collections;

if (Array.isArray(shopIds) && shopIds.length > 0) {
await Promise.all(shopIds.map((shopId) => context.validatePermissions("reaction:legacy:groups", "manage:accounts", { shopId })));

return AccountInvites.find({
shopId: {
$in: shopIds
}
});
}

await context.validatePermissions("reaction:legacy:invitations", "read");

return AccountInvites.find();
}
78 changes: 0 additions & 78 deletions src/queries/invitationsAggregate.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/resolvers/Invitation/groups.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/resolvers/Invitation/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import resolveShopFromShopId from "@reactioncommerce/api-utils/graphql/resolveShopFromShopId.js";
import { encodeInvitationOpaqueId } from "../../xforms/id.js";
import groups from "./groups.js";
import invitedBy from "./invitedBy.js";

export default {
_id: (node) => encodeInvitationOpaqueId(node._id),
groups,
groups: (parent, _, context) => context.queries.groupsById(context, parent.groupIds || [parent.groupId]),
invitedBy,
shop: resolveShopFromShopId
};
6 changes: 3 additions & 3 deletions src/resolvers/Query/invitations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getPaginatedResponseFromAggregate from "@reactioncommerce/api-utils/graphql/getPaginatedResponseFromAggregate.js";
import getPaginatedResponse from "@reactioncommerce/api-utils/graphql/getPaginatedResponse.js";
import wasFieldRequested from "@reactioncommerce/api-utils/graphql/wasFieldRequested.js";
import { decodeShopOpaqueId } from "../../xforms/id.js";

Expand All @@ -23,9 +23,9 @@ export default async function invitations(_, args, context, info) {
shopIds = encodedShopIds.map((shopId) => decodeShopOpaqueId(shopId));
}

const { collection, pipeline } = await context.queries.invitationsAggregate(context, { shopIds });
const query = await context.queries.invitations(context, { shopIds });

return getPaginatedResponseFromAggregate(collection, pipeline, connectionArgs, {
return getPaginatedResponse(query, connectionArgs, {
includeHasNextPage: wasFieldRequested("pageInfo.hasNextPage", info),
includeHasPreviousPage: wasFieldRequested("pageInfo.hasPreviousPage", info),
includeTotalCount: wasFieldRequested("totalCount", info)
Expand Down
6 changes: 6 additions & 0 deletions src/schemas/inviteShopMember.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ extend type Query {
"The shop IDs to get invitations for"
shopIds: [ID],

"Return only results that come after this cursor. Use this with `first` to specify the number of results to return."
after: ConnectionCursor,

"Return only results that come before this cursor. Use this with `last` to specify the number of results to return."
before: ConnectionCursor,

"Return at most this many results. This parameter may be used with either `after` or `offset` parameters."
first: ConnectionLimitInt,

Expand Down

0 comments on commit 282e7d0

Please sign in to comment.