Skip to content

Commit

Permalink
#17 able to get balance info from public key
Browse files Browse the repository at this point in the history
  • Loading branch information
niponchi committed Aug 24, 2018
1 parent 92754bf commit 5d8b154
Show file tree
Hide file tree
Showing 9 changed files with 1,241 additions and 1,150 deletions.
2,258 changes: 1,132 additions & 1,126 deletions firebase/functions/package-lock.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions firebase/functions/src/api/oz/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// import { Request, Response, RequestHandler, Router } from 'express'
// import * as request from 'request'

// export const apiPath = '/account'



// export default (ticketingSystem): RequestHandler => {

// const router = Router()

// const getAccountInfoRequestHandler = (req: Request, res: Response) => {
// const accountId = req.params.accountId

// return ticketingSystem.getStellarAccountInfo(accountId)
// }

// router.use('/:accountId', getAccountInfoRequestHandler)
// return router
// }
14 changes: 6 additions & 8 deletions firebase/functions/src/api/oz/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ export default (ticketingSystem): RequestHandler => {
console.log(`start of oz request \n ${JSON.stringify(req.body)}`)
if (!req.body) return sendDialogflowTextMessage(res, 'No body...')

const { action, requestSource, userId, senderId, session, languageCode } = req.body
const { action, requestSource, userId, senderId, session, languageCode, queryText } = req.body
if (!action) return sendDialogflowTextMessage(res, 'No action...')

const requestParams = {
requestSource,
from: requestSource === 'LINE' ? userId : senderId,
languageCode,
session
session,
queryText
}

if (!requestParams.requestSource || !requestParams.from) {
Expand Down Expand Up @@ -50,13 +51,10 @@ export default (ticketingSystem): RequestHandler => {
.then(() => sendDialogflowTextMessage(res, ''))

default:
return sessionTask.then(isNew => {
// send greetig message if the session is new
isNew && ticketingSystem.sendWelcomeMessage(requestParams)
return isNew
return sessionTask.then(isNewSession => {
return ticketingSystem.handleUnknownEvent(isNewSession, requestParams)
})
// otherwise, don't know how to handle the message
.then(isNew => sendDialogflowTextMessage(res, !isNew ? 'hmm...?' : ''))
.then(handled => sendDialogflowTextMessage(res, !handled ? 'hmm...?' : ''))
}
}
}
5 changes: 5 additions & 0 deletions firebase/functions/src/ticketing/facebookFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ export const facebookMessageFormatter = ({ }): IMessageFormatter => {
return {}
}

const balanceInfoTemplate = (balanceInfo: any[]) => {
return {}
}

return {
listEvents,
ticketTemplate,
confirmTemplate,
welcomeTemplate,
confirmResultTemplate,
balanceInfoTemplate,
providerName: 'facebook'
}
}
4 changes: 2 additions & 2 deletions firebase/functions/src/ticketing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ticketing = (messagingProvider: IMessageingProvider, firebaseConfig
const useTicket = require('./useTicketHandler').default(eventStore, userStore, stellarWrapper, messagingProvider, messageFormatterProvider)
const confirmTicket = require('./confirmTicketHandler').default(eventStore, userStore, stellarWrapper, messagingProvider, messageFormatterProvider)
const getTicketParams = require('./getTicketParams').default(messagingProvider, firebaseConfig)
const sendWelcomeMessage = require('./sendWelcomeMessageHandler').default(messagingProvider, messageFormatterProvider)
const handleUnknownEvent = require('./unknowEventHandler').default(stellarWrapper, messagingProvider, messageFormatterProvider)
const isNewSession = require('./isNewSessionHander').default(sessionsRepository)

return {
Expand All @@ -43,7 +43,7 @@ export const ticketing = (messagingProvider: IMessageingProvider, firebaseConfig
confirmTicket,
useTicket,
getTicketParams,
sendWelcomeMessage,
handleUnknownEvent,
isNewSession,
}
}
11 changes: 11 additions & 0 deletions firebase/functions/src/ticketing/lineFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,23 @@ export const lineMessageFormatter = ({ imageResizeService }): IMessageFormatter
return template.build()
}

const balanceInfoTemplate = (balanceInfo) => {
console.log(balanceInfo)
return {
'type': 'text',
'text': !balanceInfo || balanceInfo.length <= 0
? 'Account not found'
: balanceInfo.map(balance => `${balance.balance} ${balance.code}`).join('\n')
}
}

return {
listEvents,
ticketTemplate,
confirmTemplate,
welcomeTemplate,
confirmResultTemplate,
providerName: 'line',
balanceInfoTemplate
}
}
1 change: 1 addition & 0 deletions firebase/functions/src/ticketing/messageFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export interface IMessageFormatter {
confirmTemplate: (pictureUrl: string, displayName: string, ownerProvider: string, eventTitle: string, tx: string) => object
welcomeTemplate: (message: string, ...options: string[]) => object
confirmResultTemplate: (burnttx: string, firebaseTime: number, stellarTime: number) => object
balanceInfoTemplate: (balanceInfo: any[]) => object
providerName: string
}
42 changes: 28 additions & 14 deletions firebase/functions/src/ticketing/stellarWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,34 @@ const stellarWrapperFactory = (server: StellarSdk.Server, masterSigner: StellarS
// console.log(`Success! Results (transfer): ${result._links.transaction.href}`)
return result.hash
})
.catch((error) => {
const errMsg = `Something went wrong! (transfer): ${getErrorCode(error)}`
console.warn(errMsg)
throw new Error(errMsg)
})
}
.catch((error) => {
const errMsg = `Something went wrong! (transfer): ${getErrorCode(error)}`
console.warn(errMsg)
throw new Error(errMsg)
})
}

const getBalanceInfo = (publickeyStr) => {
return server.loadAccount(publickeyStr)
.then(account => account.balances
.map(balance => {
return {
code: balance.asset_type === 'native' ? 'XLM' : balance.asset_code,
balance: balance.balance,
issuer: balance.asset_type === 'native' ? '' : balance.asset_issuer,
}
}))
.catch(err => console.log(err) || null)
}

return {
makeOffer,
doBookTicket,
queryTransactionAction,
transfer
}
return {
makeOffer,
doBookTicket,
queryTransactionAction,
transfer,
getBalanceInfo
}

}
}

export default stellarWrapperFactory
export default stellarWrapperFactory
36 changes: 36 additions & 0 deletions firebase/functions/src/ticketing/unknowEventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { HrtimeMarker } from '../utils/hrtimeMarker'
import { Keypair } from 'stellar-sdk'

const isValidStellarPublicKey = (key) => {
try {
Keypair.fromPublicKey(key)
return true
} catch (error) {
return false
}
}

export default (stellarWrapper, messagingProvider, messageFormatterProvider) => {
const sendWelcomeMessage = require('./sendWelcomeMessageHandler').default(messagingProvider, messageFormatterProvider)

return async (isNewSession, { requestSource, from, languageCode, queryText }, ) => {
const hrMarker = HrtimeMarker.create('unknownEvent')
try {
const formatter = messageFormatterProvider.get(requestSource)
const messageSender = messagingProvider.get(requestSource)

if (isValidStellarPublicKey(queryText)) {
const accountInfo = await stellarWrapper.getBalanceInfo(queryText)
messageSender.sendCustomMessages(from, formatter.balanceInfoTemplate(accountInfo))
return true
} else if (isNewSession) {
sendWelcomeMessage({ requestSource, from, languageCode })
return true
}

return false
} finally {
hrMarker.end().log()
}
}
}

0 comments on commit 5d8b154

Please sign in to comment.