This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
generated from reactioncommerce/api-plugin-example
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable many group IDs per invite & use query instead of aggregate
Signed-off-by: Loan Laux <[email protected]>
- Loading branch information
Showing
8 changed files
with
76 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters