Skip to content

Commit

Permalink
Merge pull request #1151 from opencollective/events
Browse files Browse the repository at this point in the history
create opencollective paymentMethod for event collectives
  • Loading branch information
xdamman authored Feb 27, 2018
2 parents 484b39d + 47dede3 commit 73fd019
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
40 changes: 38 additions & 2 deletions server/graphql/CollectiveInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,42 @@ export const ExpensesStatsType = new GraphQLObjectType({
}
});

export const TransactionsStatsType = new GraphQLObjectType({
name: "TransactionsStatsType",
description: "Breakdown of transactions per type (ALL/CREDIT/DEBIT)",
fields: () => {
return {
// We always have to return an id for apollo's caching
id: {
type: GraphQLInt,
resolve(collective) {
return collective.id;
}
},
all: {
type: GraphQLInt,
resolve(collective) {
return models.Transaction.count({ where: { CollectiveId: collective.id } });
}
},
credit: {
type: GraphQLInt,
description: "Returns the number of CREDIT transactions",
resolve(collective) {
return models.Transaction.count({ where: { CollectiveId: collective.id, type: 'CREDIT' } });
}
},
debit: {
type: GraphQLInt,
description: "Returns the number of DEBIT transactions",
async resolve(collective) {
return models.Transaction.count({ where: { CollectiveId: collective.id, type: 'DEBIT' } });
}
}
}
}
});

export const CollectiveStatsType = new GraphQLObjectType({
name: "CollectiveStatsType",
description: "Stats for the collective",
Expand Down Expand Up @@ -256,9 +292,9 @@ export const CollectiveStatsType = new GraphQLObjectType({
},
transactions: {
description: "Number of transactions",
type: GraphQLInt,
type: TransactionsStatsType,
resolve(collective) {
return models.Transaction.count({ where: { CollectiveId: collective.id } });
return collective;
}
},
totalAmountReceived: {
Expand Down
1 change: 1 addition & 0 deletions server/graphql/inputTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export const OrderInputType = new GraphQLInputObjectType({
totalAmount: { type: GraphQLInt },
hostFeePercent: { type: GraphQLInt },
platformFeePercent: { type: GraphQLInt },
currency: { type: GraphQLString },
interval: { type: GraphQLString },
description: { type: GraphQLString },
publicMessage: { type: GraphQLString },
Expand Down
5 changes: 4 additions & 1 deletion server/graphql/mutations/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function createOrder(_, args, req) {
.then(() => {
if (paymentRequired) {
if (!order.paymentMethod || !(order.paymentMethod.uuid || order.paymentMethod.token)) {
throw new Error(`This tier requires a payment method`);
throw new Error(`This order requires a payment method`);
}
}
})
Expand Down Expand Up @@ -154,6 +154,9 @@ export function createOrder(_, args, req) {
order.referral = { id: matchingFund.CollectiveId }; // if there is a matching fund, we force the referral to be the owner of the fund
}
const currency = tier && tier.currency || collective.currency;
if (order.currency && order.currency !== currency) {
throw new Error(`Invalid currency. Expected ${currency}.`);
}
const quantity = order.quantity || 1;
let totalAmount;
if (tier && tier.amount && !tier.presets) { // if the tier has presets, we can't enforce tier.amount
Expand Down
6 changes: 3 additions & 3 deletions server/models/Collective.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ export default function(Sequelize, DataTypes) {

},
afterCreate: (instance) => {
// We only create an "opencollective" paymentMethod for collectives
if (instance.type !== 'COLLECTIVE') {
// We only create an "opencollective" paymentMethod for collectives and events
if (instance.type !== 'COLLECTIVE' && instance.type !== 'EVENT') {
return null;
}
models.PaymentMethod.create({
CollectiveId: instance.id,
service: 'opencollective',
type: 'collective',
name: `${capitalize(instance.name)} Collective`,
name: `${capitalize(instance.name)} ${capitalize(instance.type.toLowerCase())}`,
primary: true,
currency: instance.currency
});
Expand Down
2 changes: 1 addition & 1 deletion test/graphql.mutation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ describe('Mutation Tests', () => {
quantity: 2
};
const result = await utils.graphqlQuery(query, { order });
expect(result.errors[0].message).to.equal('This tier requires a payment method');
expect(result.errors[0].message).to.equal('This order requires a payment method');
});
});
});
Expand Down

0 comments on commit 73fd019

Please sign in to comment.