import * as functions from 'firebase-functions' ; import * as helpers from './helpers' ; import * as connect from './connect' ; import { app } from './config' ; app . post ( '/charges' , ( req , res ) => { const userId = req . user . uid ; const sourceId = req . body . sourceId ; const amount = req . body . amount ; const currency = req . body . currency ; const promise = helpers . createCharge ( userId , sourceId , amount , currency ) ; defaultHandler ( promise , res ) ; } ) ; app . get ( '/charges' , ( req , res ) => { const userId = req . user . uid ; const promise = helpers . getUserCharges ( userId ) ; defaultHandler ( promise , res ) ; } ) ; app . post ( '/sources' , ( req , res ) => { const userId = req . user . uid ; const sourceId = req . body . sourceId ; const promise = helpers . attachSource ( userId , sourceId ) ; defaultHandler ( promise , res ) ; } ) ; app . get ( '/customer' , ( req , res ) => { const userId = req . user . uid ; const promise = helpers . getCustomer ( userId ) ; defaultHandler ( promise , res ) ; } ) ; app . post ( '/subscriptions' , ( req , res ) => { const userId = req . user . uid ; const sourceId = req . body . sourceId ; const planId = req . body . planId ; const promise = helpers . createSubscription ( userId , sourceId , planId ) ; defaultHandler ( promise , res ) ; } ) ; app . put ( '/subscriptions/cancel' , ( req , res ) => { const userId = req . user . uid ; const planId = req . body . planId ; const promise = helpers . cancelSubscription ( userId , planId ) ; defaultHandler ( promise , res ) ; } ) ; function defaultHandler ( promise , res ) { promise . then ( data => res . status ( 200 ) . send ( data ) ) . catch ( err => res . status ( 400 ) . send ( err ) ) ; } export const api = functions . https . onRequest ( app ) ;