Last active
December 18, 2019 15:10
-
-
Save gtcdevop/3c1d2f14f0cfbef706a8040870bc098b to your computer and use it in GitHub Desktop.
Cloud functions Trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(functions.config().firebase); | |
exports.addLoanReq = functions.firestore | |
.document('loans/{documentId}') | |
.onCreate(event => { | |
var loanInfo = event.data.data(); | |
var loanType = loanInfo.loanType; | |
var loanAmt = loanInfo.loanAmt; | |
var recUserId = loanInfo.userId; | |
console.log(`loan amout ${loanAmt}`); | |
console.log(`user id ${recUserId}`); | |
var loansRef = admin.firestore().collection('loans') | |
var loanReqCount; | |
loansRef.where('userId', '==', recUserId).get() | |
.then(snapshot => { | |
loanReqCount = snapshot.size; | |
console.log(`no of loan requests ${snapshot.size}`); | |
}) | |
.catch(err => { | |
console.log('Error getting total request loans', err); | |
}); | |
var statusVal; | |
if (loanReqCount > 6){ | |
statusVal = 'Loan is rejected due to limit on no of loans'; | |
}else if(loanAmt < 100000 && loanReqCount < 3){ | |
statusVal = 'Loan is accepted for further processing'; | |
}else if(loanAmt < 50000 && loanReqCount < 4){ | |
statusVal = 'Loan is approved, loan office will contact you'; | |
}else if(loanAmt < 20000 && loanReqCount < 2 && loanType == 'auto'){ | |
statusVal = 'Ready to issue disbursment check to auto retailer'; | |
}else{ | |
statusVal = 'Contact loan office at your nearest branch'; | |
} | |
return event.data.ref.set({status: statusVal}, {merge: true}); | |
}); | |
exports.updateLoanReq = functions.firestore | |
.document('loans/{documentId}') | |
.onUpdate(event => { | |
var loanInfo = event.data.data(); | |
var loanType = loanInfo.loanType; | |
var loanAmt = loanInfo.loanAmt; | |
var prevLoanInfo = event.data.previous.data(); | |
var prevLoanType = prevLoanInfo.loanType; | |
var prevLoanAmt = prevLoanInfo.loanAmt; | |
console.log(`prev loan amout ${prevLoanAmt}`); | |
var prevStatus = prevLoanInfo.status; | |
console.log(`prev loan status ${prevStatus}`); | |
if ( typeof prevStatus == 'undefined') | |
{ | |
return; | |
} | |
var statusVal; | |
if(prevStatus.indexOf('rejected') > -1) { | |
statusVal = 'Manual review'; | |
}else { | |
if (prevLoanAmt > loanAmt){ | |
statusVal = 'Loan is accepted for further processing'; | |
}else if(prevLoanType == loanType && prevLoanAmt < loanAmt){ | |
statusVal = prevStatus; | |
}else{ | |
statusVal = 'Contact loan office at your nearest branch'; | |
} | |
} | |
return event.data.ref.set({status: statusVal}, {merge: true}); | |
}); | |
exports.deleteLoanReq = functions.firestore | |
.document('loans/{documentId}') | |
.onDelete(event => { | |
var loanInfo = event.data.previous.data(); | |
var loanTypeDel = loanInfo.loanType; | |
var loanAmtDel = loanInfo.loanAmt; | |
var recUserIdDel = loanInfo.userId; | |
var addDoc = admin.firestore().collection('loansHistory').add({ | |
loanType: loanTypeDel, | |
loanAmt: loanAmtDel, | |
userId: recUserIdDel | |
}).then(ref => { | |
console.log('Added loan info to history table ', ref.id); | |
}); | |
return addDoc.then(res => { | |
console.log('Add: ', res); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FirebaseApp } from 'angularfire2'; | |
//... | |
export class QuestionComponent implements OnInit { | |
questionRef; | |
constructor(private fb: FirebaseApp) {} | |
ngOnInit() { | |
this.questionRef = this.fb.firestore().collection('questions').doc(this.id); | |
} | |
voteYes() { | |
this.fb.firestore().runTransaction(transaction => { | |
return transaction.get(this.questionRef).then(question => { | |
const newVotes = question.data().yesVotes + 1; | |
transaction.update(this.questionRef, { yesVotes: newVotes }); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment