Skip to content

Commit

Permalink
Merge pull request #1001 from oxaudo/updates
Browse files Browse the repository at this point in the history
Add acceptsCardPayments field for partner that combines info from Gravity and Lewitt-api
  • Loading branch information
alloy authored Apr 12, 2018
2 parents fb1f280 + 8cbe41a commit 4682eec
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GRAVITY_GRAPHQL_ENDPOINT=https://stagingapi.artsy.net/api
GRAVITY_API_URL=https://stagingapi.artsy.net/
HMAC_SECRET=https://www.youtube.com/watch?v=DLzxrzFCyOs
IMPULSE_API_BASE=https://impulse-staging.artsy.net/api
LEWITT_API_BASE=https://lewitt-api-staging.artsy.net
POSITRON_API_BASE=https://writer-staging.artsy.net/api
PREDICTION_ENDPOINT=https://live-staging.artsy.net
REDIS_URL=redis://127.0.0.1:6379
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GRAVITY_API_URL=https://api.artsy.test
HMAC_SECRET=https://www.youtube.com/watch?v=F5bAa6gFvLs
IMPULSE_API_BASE=https://impulse-staging.artsy.net/api
IMPULSE_APPLICATION_ID=sdfsdf234234f
LEWITT_API_BASE=https://lewitt-api-staging.artsy.net
METAPHYSICS_PRODUCTION_ENDPOINT=https://metaphysics-production.artsy.net
METAPHYSICS_STAGING_ENDPOINT=https://metaphysics-staging.artsy.net
POSITRON_API_BASE=https://writer.artsy.test/api
Expand Down
16 changes: 14 additions & 2 deletions scripts/dump-remote-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ import fetch from "node-fetch"

const destination = "src/data"

const httpLink = createHttpLink({
const httpConvectionLink = createHttpLink({
fetch,
uri: urljoin("https://convection-staging.artsy.net/api", "graphql"),
})

introspectSchema(httpLink).then(schema => {
introspectSchema(httpConvectionLink).then(schema => {
fs.writeFileSync(
path.join(destination, "convection.graphql"),
printSchema(schema, { commentDescriptions: true })
)
})

const httpLewittLink = createHttpLink({
fetch,
uri: urljoin("https://lewitt-api-staging.artsy.net", "graphql"),
})

introspectSchema(httpLewittLink).then(schema => {
fs.writeFileSync(
path.join(destination, "lewitt.graphql"),
printSchema(schema, { commentDescriptions: true })
)
})
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
HMAC_SECRET,
IMPULSE_API_BASE,
IMPULSE_APPLICATION_ID,
LEWITT_API_BASE,
METAPHYSICS_STAGING_ENDPOINT,
METAPHYSICS_PRODUCTION_ENDPOINT,
NODE_ENV,
Expand Down Expand Up @@ -62,6 +63,7 @@ const mustHave = {
GRAVITY_SECRET,
IMPULSE_API_BASE,
IMPULSE_APPLICATION_ID,
LEWITT_API_BASE,
POSITRON_API_BASE,
}

Expand Down Expand Up @@ -107,6 +109,7 @@ export default {
HMAC_SECRET,
IMPULSE_API_BASE,
IMPULSE_APPLICATION_ID,
LEWITT_API_BASE,
METAPHYSICS_STAGING_ENDPOINT,
METAPHYSICS_PRODUCTION_ENDPOINT,
NODE_ENV: NODE_ENV || "development",
Expand Down
94 changes: 94 additions & 0 deletions src/data/lewitt.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
schema {
query: RootQuery
mutation: Mutation
}

# An ArtworkGroup which has an artwork and its related line items
type ArtworkGroup {
artists: [String]
date: String
default_image_id: String
dimensions: Json
edition_set_id: String
id: ID!
images: Json
line_items: [LineItem]
price_in_cents: Int!
title: String
}

# Autogenerated input type of CreateInvoice
input CreateInvoiceInput {
# A unique identifier for the client performing the mutation.
clientMutationId: String
id: ID
partner_id: String!
payment_method: String
currency: Currencies
impulse_conversation_id: String
}

enum Currencies {
# US Dollar
usd
}

# An Invoice
type Invoice {
artwork_groups: [ArtworkGroup]
artwork_groups_count: Int
created_at: String
currency: String
discount_total_cents: Int
fee_in_cents: Int
id: ID!
impulse_conversation_id: Int
invoice_transactions_count: Int
line_items: [LineItem]
line_items_count: Int
merchant_account: [MerchantAccount]
merchant_account_id: Int
paid_at: String
partner_id: String
payment_method: String
refunded_at: String
sent_at: String
shipping_total_cents: Int
state: String
tax_total_cents: Int
token: String!
total_cents: Int
updated_at: String
voided_at: String
}

scalar Json

# A LineItem
type LineItem {
amount_cents: Int!
description: String!
id: ID!
line_item_type: String!
}

# A MerchantAccount
type MerchantAccount {
# Checks if account is properly configured for credit card payments
credit_card_enabled: Boolean!
id: ID!
partner_id: String!
}

type Mutation {
create_invoice(input: CreateInvoiceInput!): Invoice
}

# Root query
type RootQuery {
# Search Invoices
invoices(ids: [ID], partner_id: String!, token: String): [Invoice]

# Retrieve a Merchant Account
partner_product_merchant_account(partner_id: String!): MerchantAccount
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "lib/loaders/api/logger"
import { fetchPersistedQuery } from "./lib/fetchPersistedQuery"
import { info } from "./lib/loggers"
import { mergeSchemas } from "./lib/mergeSchemas"
import { executableLewittSchema, mergeSchemas } from "./lib/mergeSchemas"
import { middleware as requestIDsAdder } from "./lib/requestIDs"
import { middleware as requestTracer, makeSchemaTraceable } from "./lib/tracer"

Expand All @@ -42,6 +42,7 @@ async function startApp() {
config.GRAVITY_XAPP_TOKEN = xapp.token

let schema = localSchema
const lewittSchema = await executableLewittSchema()

if (enableSchemaStitching) {
try {
Expand Down Expand Up @@ -111,6 +112,7 @@ async function startApp() {
userID,
defaultTimezone,
span,
lewittSchema,
...createLoaders(accessToken, userID, {
requestIDs,
userAgent,
Expand Down
54 changes: 46 additions & 8 deletions src/lib/mergeSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { headers as requestIDHeaders } from "./requestIDs"

import config from "config"

const { CONVECTION_API_BASE, GRAVITY_GRAPHQL_ENDPOINT } = config
const {
CONVECTION_API_BASE,
GRAVITY_GRAPHQL_ENDPOINT,
LEWITT_API_BASE,
} = config

export function createConvectionLink() {
const httpLink = createHttpLink({
Expand Down Expand Up @@ -68,20 +72,54 @@ export function createGravityLink() {
return middlewareLink.concat(authMiddleware).concat(httpLink)
}

export async function mergeSchemas() {
// The below all relate to Convection stitching.
// TODO: Refactor when adding another service.
export async function executableConvectionSchema() {
const convectionLink = createConvectionLink()
const convectionTypeDefs = fs.readFileSync(
path.join("src/data/convection.graphql"),
"utf8"
)

const convectionLink = createConvectionLink()

const convectionSchema = await makeRemoteExecutableSchema({
return await makeRemoteExecutableSchema({
schema: convectionTypeDefs,
link: convectionLink,
})
}

export function createLewittLink() {
const httpLink = createHttpLink({
fetch,
uri: urljoin(LEWITT_API_BASE, "graphql"),
})

const middlewareLink = new ApolloLink((operation, forward) =>
forward(operation)
)

const authMiddleware = setContext((_request, context) => {
const locals = context.graphqlContext && context.graphqlContext.res.locals
const headers = { ...(locals && requestIDHeaders(locals.requestIDs)) }
// Lewitt uses no authentication for now
return { headers }
})

return middlewareLink.concat(authMiddleware).concat(httpLink)
}

export async function executableLewittSchema() {
const lewittLink = createLewittLink()
const lewittTypeDefs = fs.readFileSync(
path.join("src/data/lewitt.graphql"),
"utf8"
)
return await makeRemoteExecutableSchema({
schema: lewittTypeDefs,
link: lewittLink,
})
}

export async function mergeSchemas() {
// The below all relate to Convection stitching.
// TODO: Refactor when adding another service.
const convectionSchema = await executableConvectionSchema()

const gravityTypeDefs = fs.readFileSync(
path.join("src/data/gravity.graphql"),
Expand Down
75 changes: 74 additions & 1 deletion src/schema/__tests__/partner.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { runQuery } from "test/utils"
import gql from "test/gql"

import { makeExecutableSchema } from "graphql-tools"
import fs from "fs"
import path from "path"

describe("Partner type", () => {
let partner = null
Expand All @@ -7,6 +12,7 @@ describe("Partner type", () => {
beforeEach(() => {
partner = {
id: "catty-partner",
_id: "catty-partner",
name: "Catty Partner",
has_full_profile: true,
profile_banner_display: true,
Expand All @@ -27,7 +33,7 @@ describe("Partner type", () => {
})

it("returns a partner and categories", () => {
const query = `
const query = gql`
{
partner(id: "catty-partner") {
name
Expand Down Expand Up @@ -55,4 +61,71 @@ describe("Partner type", () => {
})
})
})

describe("acceptsCardPayments", () => {
let credit_card_enabled = true
const query = gql`
{
partner(id: "catty-partner") {
acceptsCardPayments
}
}
`

beforeEach(() => {
partner.payments_enabled = true

const typeDefs = fs.readFileSync(
path.resolve(__dirname, "../../data/lewitt.graphql"),
"utf8"
)

const resolvers = {
RootQuery: {
partner_product_merchant_account: () => {
return { credit_card_enabled }
},
},
}

const lewittSchema = makeExecutableSchema({
typeDefs,
resolvers,
})

rootValue.lewittSchema = lewittSchema
})

it("returns true if payments_enabled and partner_product_merchant_account is configured in lewitt", () => {
return runQuery(query, rootValue).then(data => {
expect(data).toEqual({
partner: {
acceptsCardPayments: true,
},
})
})
})

it("returns false if payments_enabled set to false on partner", () => {
partner.payments_enabled = false
return runQuery(query, rootValue).then(data => {
expect(data).toEqual({
partner: {
acceptsCardPayments: false,
},
})
})
})

it("returns false if partner_product_merchant_account is not configured in lewitt", () => {
credit_card_enabled = false
return runQuery(query, rootValue).then(data => {
expect(data).toEqual({
partner: {
acceptsCardPayments: false,
},
})
})
})
})
})
Loading

0 comments on commit 4682eec

Please sign in to comment.