Untitled diff

Created Diff never expires
12 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
34 lines
12 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
34 lines
/**
/**
* Method to keep user funds in sync upon new transactions
* Method to keep user funds in sync upon new transactions
*/
*/
exports.walletSync = functions.region('europe-west1').firestore
exports.walletSync = functions.region('europe-west1').firestore
.document('transactions/{transactionId}')
.document('transactions/{transactionId}')
.onCreate( ( snapshot, context ) => {
.onCreate( ( snapshot, context ) => {

let item = snapshot.data()
let item = snapshot.data()


if ( item.type === 'debit' && item.description === 'subscription' )
if ( item.type === 'debit' && item.description === 'subscription' )
return null
return null


console.log( 'processing', context.eventId, item.processed, context.params.transactionId )
console.log( 'processing', context.eventId, context.params.transactionId )

if ( item.processed === context.eventId )
{
console.log( 'processed_already', context.params.transactionId )
return null
}


return firestore
return firestore
.runTransaction( transaction =>
.runTransaction( transaction =>
transaction.get( firestore.doc(`users/${item.owner}/personal/wallet`) ).then( doc => {
transaction.get( firestore.doc(`users/${item.owner}/personal/wallet`) ).then( doc => {


let wallet = doc.get('funds')
let wallet = doc.data()

if ( wallet.lastEventId === context.eventId )
{
console.log( 'processed_already', context.eventId )
return null
}
wallet.funds += item.amount
wallet.funds += item.amount


transaction.update( snapshot.ref, { processed: context.eventId })
return transaction.update( doc.ref, { funds: wallet.funds, lastEventId: context.eventId })
return transaction.update( doc.ref, { funds: wallet.funds })


})
})
)
)


})
})