import Price from 'stripe' ; import { query } from '../utils/PostgresConnection' ; 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 { priceSchema } from '../schemas/price' ; const config = getConfig ( ) ; export const upsertPrice = async ( price ) : Promise < Price . Price [ ] > => { const productId = price . product . toString ( ) ; if ( productId && ! ( await verifyProductExists ( productId ) ) ) { await fetchAndInsertProduct ( productId ) ; } const upsertString = constructUpsertSql ( config . SCHEMA || 'stripe' , 'prices' , priceSchema ) ; const prepared = sql ( upsertString ) ( price ) ; const { rows } = await query ( prepared . text , prepared . values ) ; return rows ; } ; export const verifyPriceExists = async ( id ) : Promise < boolean > => { const prepared = sql ( `
    select id from "${ config . SCHEMA }"."prices" 
    where id = :id;
    ` ) ( { id } ) ; const { rows } = await query ( prepared . text , prepared . values ) ; return rows . length > 0 ; } ; export const fetchAndInsertPrice = async ( id ) : Promise < Price . Price [ ] > => { const price = await stripe . prices . retrieve ( id ) ; return upsertPrice ( price ) ; } ; export const deletePrice = async ( id ) : Promise < boolean > => { const prepared = sql ( `
    delete from "${ config . SCHEMA }"."prices" 
    where id = :id
    returning id;
    ` ) ( { id } ) ; const { rows } = await query ( prepared . text , prepared . values ) ; return rows . length > 0 ; } ; type fetchPriceResponse = Price . Response < Price . ApiList < Price . Price > > ; type fetchPriceParams = { limit : number ; id : string | undefined ; } ; const fetchProductsDefaults = { limit : 100 , id : undefined } ; export const fetchPrices = async ( options = fetchProductsDefaults ) : Promise < fetchPriceResponse > => { const prices = await stripe . prices . list ( { limit : options . limit } ) ; return prices ; } ;