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

Commit

Permalink
refactor: use field resolvers to join invitation data
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 25, 2020
1 parent db04e65 commit af47d72
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/queries/accountByUserId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ReactionError from "@reactioncommerce/reaction-error";

/**
* @name accountByUserId
* @method
* @memberof Accounts/NoMeteorQueries
* @summary query the Accounts collection and return user account data
* @param {Object} context - an object containing the per-request state
* @param {String} id - id of user to query
* @returns {Object} user account object
*/
export default async function accountByUserIdQuery(context, id) {
const { collections } = context;
const { Accounts } = collections;

const account = await Accounts.findOne({ userId: id });
if (!account) throw new ReactionError("not-found", "No account found");

// Check to make sure current user has permissions to view queried user
await context.validatePermissions("reaction:legacy:accounts", "read", {
owner: account.userId
});

return account;
}
2 changes: 2 additions & 0 deletions src/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import accountByUserId from "./accountByUserId.js";
import accounts from "./accounts.js";
import group from "./group.js";
import groups from "./groups.js";
Expand All @@ -6,6 +7,7 @@ import invitationsAggregate from "./invitationsAggregate.js";
import userAccount from "./userAccount.js";

export default {
accountByUserId,
accounts,
group,
groups,
Expand Down
5 changes: 0 additions & 5 deletions src/resolvers/Invitation.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/resolvers/Invitation/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Return group by ID
* @param parent
* @param _
* @param context
* @returns {Promise<*[]>}
*/
export default async function groups(parent, _, context) {
const group = await context.queries.group(context, parent.groupId);

return [group];
}
11 changes: 11 additions & 0 deletions src/resolvers/Invitation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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,
invitedBy,
shop: resolveShopFromShopId
};
13 changes: 13 additions & 0 deletions src/resolvers/Invitation/invitedBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Returns the account that sent the invitation
* @param parent
* @param _
* @param context
* @returns {Promise<Object|null>|null}
*/
export default function invitedBy(parent, _, context) {
const { invitedByUserId } = parent;
if (!invitedByUserId) return null;

return context.queries.accountByUserId(context, invitedByUserId);
}
2 changes: 1 addition & 1 deletion src/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getConnectionTypeResolvers from "@reactioncommerce/api-utils/graphql/getC
import Account from "./Account/index.js";
import AddAccountAddressBookEntryPayload from "./AddAccountAddressBookEntryPayload.js";
import Group from "./Group/index.js";
import Invitation from "./Invitation.js";
import Invitation from "./Invitation/index.js";
import Mutation from "./Mutation/index.js";
import Query from "./Query/index.js";
import Shop from "./Shop/index.js";
Expand Down

0 comments on commit af47d72

Please sign in to comment.