Skip to content

Commit 3e01e49

Browse files
authored
better use account reference and fetchAccountWithReference (#11210)
1 parent 5c53025 commit 3e01e49

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

server/graphql/v2/mutation/AccountMutations.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { checkRemoteUserCanUseAccount, checkRemoteUserCanUseHost } from '../../c
3333
import { BadRequest, Forbidden, NotFound, Unauthorized, ValidationFailed } from '../../errors';
3434
import { AccountTypeToModelMapping } from '../enum/AccountType';
3535
import { GraphQLTwoFactorMethodEnum } from '../enum/TwoFactorMethodEnum';
36-
import { idDecode } from '../identifiers';
3736
import { fetchAccountWithReference, GraphQLAccountReferenceInput } from '../input/AccountReferenceInput';
3837
import { GraphQLAccountUpdateInput } from '../input/AccountUpdateInput';
3938
import { GraphQLDuplicateAccountDataTypeInput } from '../input/DuplicateAccountDataTypeInput';
@@ -374,7 +373,7 @@ const accountMutations = {
374373
async resolve(_: void, args, req: express.Request) {
375374
checkRemoteUserCanUseAccount(req);
376375

377-
const account = await fetchAccountWithReference(args.account);
376+
const account = await fetchAccountWithReference(args.account, { loaders: req.loaders, throwIfMissing: true });
378377

379378
if (!req.remoteUser.isAdminOfCollective(account)) {
380379
throw new Forbidden();
@@ -414,7 +413,7 @@ const accountMutations = {
414413
): Promise<Record<string, unknown>> {
415414
checkRemoteUserCanUseAccount(req);
416415

417-
const account = await fetchAccountWithReference(args.account);
416+
const account = await fetchAccountWithReference(args.account, { loaders: req.loaders, throwIfMissing: true });
418417

419418
if (!req.remoteUser.isAdminOfCollective(account)) {
420419
throw new Forbidden();
@@ -545,7 +544,7 @@ const accountMutations = {
545544
async resolve(_: void, args, req: express.Request): Promise<Collective> {
546545
checkRemoteUserCanUseAccount(req);
547546

548-
const account = await fetchAccountWithReference(args.account);
547+
const account = await fetchAccountWithReference(args.account, { loaders: req.loaders, throwIfMissing: true });
549548

550549
if (!req.remoteUser.isAdminOfCollective(account)) {
551550
throw new Forbidden();
@@ -651,11 +650,7 @@ const accountMutations = {
651650
async resolve(_: void, args, req: express.Request): Promise<Collective> {
652651
checkRemoteUserCanUseAccount(req);
653652

654-
const id = idDecode(args.account.id, 'account');
655-
const account = await req.loaders.Collective.byId.load(id);
656-
if (!account) {
657-
throw new NotFound('Account Not Found');
658-
}
653+
const account = await fetchAccountWithReference(args.account, { loaders: req.loaders, throwIfMissing: true });
659654

660655
if (!req.remoteUser.isAdminOfCollective(account) && !req.remoteUser.isRoot()) {
661656
throw new Forbidden();
@@ -808,11 +803,7 @@ const accountMutations = {
808803
async resolve(_: void, args, req: express.Request): Promise<Collective> {
809804
checkRemoteUserCanUseAccount(req);
810805

811-
const id = args.account.legacyId || idDecode(args.account.id, 'account');
812-
const account = await req.loaders.Collective.byId.load(id);
813-
if (!account) {
814-
throw new NotFound('Account Not Found');
815-
}
806+
const account = await fetchAccountWithReference(args.account, { loaders: req.loaders, throwIfMissing: true });
816807

817808
// Check host only policies
818809
const previousPolicies = account.data?.policies || {};
@@ -865,11 +856,7 @@ const accountMutations = {
865856
async resolve(_, args, req) {
866857
checkRemoteUserCanUseAccount(req);
867858

868-
const id = args.account.legacyId || idDecode(args.account.id, 'account');
869-
const account = await req.loaders.Collective.byId.load(id);
870-
if (!account) {
871-
throw new NotFound('Account Not Found');
872-
}
859+
const account = await fetchAccountWithReference(args.account, { loaders: req.loaders, throwIfMissing: true });
873860

874861
if (!req.remoteUser.isAdminOfCollective(account)) {
875862
throw new Unauthorized('You need to be logged in as an Admin of the account.');

0 commit comments

Comments
 (0)