diff --git a/server/graphql/CollectiveInterface.js b/server/graphql/CollectiveInterface.js index 1617152beff..d19c3f89a8b 100644 --- a/server/graphql/CollectiveInterface.js +++ b/server/graphql/CollectiveInterface.js @@ -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", @@ -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: { diff --git a/server/graphql/inputTypes.js b/server/graphql/inputTypes.js index 000d2b9b63f..0c84023a0c3 100644 --- a/server/graphql/inputTypes.js +++ b/server/graphql/inputTypes.js @@ -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 }, diff --git a/server/graphql/mutations/orders.js b/server/graphql/mutations/orders.js index ee49696fd57..99cbb2f5d07 100644 --- a/server/graphql/mutations/orders.js +++ b/server/graphql/mutations/orders.js @@ -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`); } } }) @@ -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 diff --git a/server/models/Collective.js b/server/models/Collective.js index 51c2478d1c5..db5f239be4d 100644 --- a/server/models/Collective.js +++ b/server/models/Collective.js @@ -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 }); diff --git a/test/graphql.mutation.test.js b/test/graphql.mutation.test.js index 70517860d84..b6327ce1cdb 100644 --- a/test/graphql.mutation.test.js +++ b/test/graphql.mutation.test.js @@ -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'); }); }); });