-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new GraphQL Goal object and resolver on Account.goal
- Loading branch information
1 parent
8d7163f
commit 801d387
Showing
9 changed files
with
325 additions
and
3 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,12 @@ | ||
/** | ||
* Constants for the goal type | ||
* | ||
*/ | ||
|
||
enum GoalTypes { | ||
ALL_TIME = 'ALL_TIME', | ||
MONTHLY = 'MONTHLY', | ||
YEARLY = 'YEARLY', | ||
} | ||
|
||
export default GoalTypes; |
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,19 @@ | ||
import { GraphQLEnumType } from 'graphql'; | ||
|
||
import goalType from '../../../constants/goal-types'; | ||
|
||
export const GraphQLGoalType = new GraphQLEnumType({ | ||
name: 'GoalType', | ||
description: 'All supported goal types', | ||
values: { | ||
[goalType.ALL_TIME]: { | ||
description: 'Total contributions', | ||
}, | ||
[goalType.MONTHLY]: { | ||
description: 'Contributions per month', | ||
}, | ||
[goalType.YEARLY]: { | ||
description: 'Contributions per year', | ||
}, | ||
}, | ||
}); |
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,12 @@ | ||
import { GraphQLInputObjectType, GraphQLInt, GraphQLNonNull } from 'graphql'; | ||
|
||
import { GraphQLGoalType } from '../enum/GoalType'; | ||
|
||
export const GraphQLGoalInput = new GraphQLInputObjectType({ | ||
name: 'GoalInput', | ||
description: 'Input type for Goals', | ||
fields: () => ({ | ||
type: { type: new GraphQLNonNull(GraphQLGoalType) }, | ||
amount: { type: new GraphQLNonNull(GraphQLInt) }, | ||
}), | ||
}); |
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,72 @@ | ||
import { GraphQLNonNull } from 'graphql'; | ||
import { cloneDeep, set } from 'lodash'; | ||
|
||
import activities from '../../../constants/activities'; | ||
import models, { sequelize } from '../../../models'; | ||
import { checkRemoteUserCanUseAccount } from '../../common/scope-check'; | ||
import { Forbidden, Unauthorized } from '../../errors'; | ||
import { fetchAccountWithReference, GraphQLAccountReferenceInput } from '../input/AccountReferenceInput'; | ||
import { GraphQLGoalInput } from '../input/GoalInput'; | ||
import { GraphQLGoal } from '../object/Goal'; | ||
|
||
const goalMutations = { | ||
setGoal: { | ||
type: GraphQLGoal, | ||
description: 'Set a goal for your account.', | ||
args: { | ||
account: { | ||
type: new GraphQLNonNull(GraphQLAccountReferenceInput), | ||
}, | ||
goal: { | ||
type: GraphQLGoalInput, | ||
description: 'The goal to set for the account. Setting goal to undefined or null will remove any current goal.', | ||
}, | ||
}, | ||
async resolve(_: void, args: Record<string, unknown>, req: Express.Request): Promise<typeof GraphQLGoal> { | ||
if (!req.remoteUser) { | ||
throw new Unauthorized(); | ||
} | ||
|
||
return sequelize.transaction(async transaction => { | ||
const account = await fetchAccountWithReference(args.account, { | ||
throwIfMissing: true, | ||
lock: true, | ||
dbTransaction: transaction, | ||
}); | ||
|
||
if (!req.remoteUser.isAdminOfCollective(account)) { | ||
throw new Forbidden(); | ||
} | ||
|
||
checkRemoteUserCanUseAccount(req); | ||
|
||
const settings = account.settings ? cloneDeep(account.settings) : {}; | ||
set(settings, 'goal', args.goal); | ||
// Remove legacy goals | ||
set(settings, 'goals', undefined); | ||
const previousData = { | ||
settings: { goal: account.settings?.goal, ...(account.settings?.goals && { goals: account.settings.goals }) }, | ||
}; | ||
const updatedAccount = await account.update({ settings }, { transaction }); | ||
await models.Activity.create( | ||
{ | ||
type: activities.COLLECTIVE_EDITED, | ||
UserId: req.remoteUser.id, | ||
UserTokenId: req.userToken?.id, | ||
CollectiveId: account.id, | ||
FromCollectiveId: account.id, | ||
HostCollectiveId: account.approvedAt ? account.HostCollectiveId : null, | ||
data: { | ||
previousData, | ||
newData: { settings: { goal: args.goal } }, | ||
}, | ||
}, | ||
{ transaction }, | ||
); | ||
return updatedAccount.settings.goal; | ||
}); | ||
}, | ||
}, | ||
}; | ||
|
||
export default goalMutations; |
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
Oops, something went wrong.