Skip to content

Commit

Permalink
feat: generic solution to format array field
Browse files Browse the repository at this point in the history
  • Loading branch information
phamhieu committed Jan 20, 2022
1 parent 9129dae commit 1d097fb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/lib/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { query } from '../utils/PostgresConnection'
import { pg as sql } from 'yesql'
import { getConfig } from '../utils/config'
import { stripe } from '../utils/StripeClientManager'
import { constructUpsertSql } from '../utils/helpers'
import { cleanseArrayField, constructUpsertSql } from '../utils/helpers'
import { customerSchema } from '../schemas/customer'

const config = getConfig()
Expand All @@ -13,7 +13,8 @@ export const upsertCustomer = async (customer: Customer.Customer): Promise<Custo
const upsertString = constructUpsertSql(config.SCHEMA || 'stripe', 'customers', customerSchema)

// Inject the values
const prepared = sql(upsertString)(customer)
const cleansed = cleanseArrayField(customer)
const prepared = sql(upsertString)(cleansed)

// Run it
const { rows } = await query(prepared.text, prepared.values)
Expand Down
19 changes: 3 additions & 16 deletions src/lib/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { query } from '../utils/PostgresConnection'
import { pg as sql } from 'yesql'
import { getConfig } from '../utils/config'
import { stripe } from '../utils/StripeClientManager'
import { constructUpsertSql } from '../utils/helpers'
import { cleanseArrayField, constructUpsertSql } from '../utils/helpers'
import { invoiceSchema } from '../schemas/invoice'
import { verifyCustomerExists, fetchAndInsertCustomer } from './customers'
import { verifySubscriptionExists, fetchAndInsertSubscription } from './subscriptions'
Expand All @@ -23,25 +23,12 @@ export const upsertInvoice = async (invoice: Invoice.Invoice): Promise<Invoice.I
await fetchAndInsertSubscription(subscriptionId)
}

/**
* For array object field like invoice.custom_fields
* ex: [{"name":"Project name","value":"Test Project"}]
*
* we need to stringify it first cos passing array object directly will end up with
* {
* invalid input syntax for type json
* detail: 'Expected ":", but found "}".',
* where: 'JSON data, line 1: ...\\":\\"Project name\\",\\"value\\":\\"Test Project\\"}"}',
* }
*/
const customFields = invoice.custom_fields
const modifiedInvoice = { ...invoice, custom_fields: JSON.stringify(customFields) }

// Create the SQL
const upsertString = constructUpsertSql(config.SCHEMA || 'stripe', 'invoices', invoiceSchema)

// Inject the values
const prepared = sql(upsertString)(modifiedInvoice)
const cleansed = cleanseArrayField(invoice)
const prepared = sql(upsertString)(cleansed)

// Run it
const { rows } = await query(prepared.text, prepared.values)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pg as sql } from 'yesql'
import { getConfig } from '../utils/config'
import { stripe } from '../utils/StripeClientManager'
import { verifyProductExists, fetchAndInsertProduct } from './products'
import { constructUpsertSql } from '../utils/helpers'
import { cleanseArrayField, constructUpsertSql } from '../utils/helpers'
import { priceSchema } from '../schemas/price'

const config = getConfig()
Expand All @@ -20,7 +20,8 @@ export const upsertPrice = async (price: Price.Price): Promise<Price.Price[]> =>
const upsertString = constructUpsertSql(config.SCHEMA || 'stripe', 'prices', priceSchema)

// Inject the values
const prepared = sql(upsertString)(price)
const cleansed = cleanseArrayField(price)
const prepared = sql(upsertString)(cleansed)

// Run it
const { rows } = await query(prepared.text, prepared.values)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pg as sql } from 'yesql'
import { getConfig } from '../utils/config'
import { stripe } from '../utils/StripeClientManager'
import { productSchema } from '../schemas/product'
import { constructUpsertSql } from '../utils/helpers'
import { cleanseArrayField, constructUpsertSql } from '../utils/helpers'

const config = getConfig()

Expand All @@ -13,7 +13,8 @@ export const upsertProduct = async (product: Product.Product): Promise<Product.P
const upsertString = constructUpsertSql(config.SCHEMA || 'stripe', 'products', productSchema)

// Inject the values
const prepared = sql(upsertString)(product)
const cleansed = cleanseArrayField(product)
const prepared = sql(upsertString)(cleansed)

// Run it
const { rows } = await query(prepared.text, prepared.values)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/subscription_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { query } from '../utils/PostgresConnection'
import { pg as sql } from 'yesql'
import { getConfig } from '../utils/config'
import { stripe } from '../utils/StripeClientManager'
import { constructUpsertSql } from '../utils/helpers'
import { cleanseArrayField, constructUpsertSql } from '../utils/helpers'
import { subscriptionItemSchema } from '../schemas/subscription_item'

const config = getConfig()
Expand All @@ -29,7 +29,8 @@ export const upsertSubscriptionItem = async (
)

// Inject the values
const prepared = sql(upsertString)(modifiedSubscriptionItem)
const cleansed = cleanseArrayField(modifiedSubscriptionItem)
const prepared = sql(upsertString)(cleansed)

// Run it
const { rows } = await query(prepared.text, prepared.values)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { query } from '../utils/PostgresConnection'
import { pg as sql } from 'yesql'
import { getConfig } from '../utils/config'
import { stripe } from '../utils/StripeClientManager'
import { constructUpsertSql } from '../utils/helpers'
import { cleanseArrayField, constructUpsertSql } from '../utils/helpers'
import { subscriptionSchema } from '../schemas/subscription'
import { verifyCustomerExists, fetchAndInsertCustomer } from './customers'
import { upsertSubscriptionItem } from './subscription_items'
Expand All @@ -27,7 +27,8 @@ export const upsertSubscription = async (
)

// Inject the values
const prepared = sql(upsertString)(subscription)
const cleansed = cleanseArrayField(subscription)
const prepared = sql(upsertString)(cleansed)

// Run it
const { rows } = await query(prepared.text, prepared.values)
Expand Down
26 changes: 26 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,29 @@ export const constructUpsertSql = (
.join(',')}
;`
}

/**
* For array object field like invoice.custom_fields
* ex: [{"name":"Project name","value":"Test Project"}]
*
* we need to stringify it first cos passing array object directly will end up with
* {
* invalid input syntax for type json
* detail: 'Expected ":", but found "}".',
* where: 'JSON data, line 1: ...\\":\\"Project name\\",\\"value\\":\\"Test Project\\"}"}',
* }
*/
export const cleanseArrayField = (obj: {
[Key: string]: any
}): {
[Key: string]: any
} => {
const cleansed = { ...obj }
Object.keys(cleansed).map((k) => {
const data = cleansed[k]
if (Array.isArray(data)) {
cleansed[k] = JSON.stringify(data)
}
})
return cleansed
}

0 comments on commit 1d097fb

Please sign in to comment.