const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://good-cash-8824a.firebaseio.com",
storageBucket: "good-cash-8824a.appspot.com",
});
const method = require('./method');
/**
* Borrower-related data from Loandisk
*
* @typedef {Object} borrower
*
* @property {string} borrower_address - Borrower’s address
* @property {string} borrower_firstname - Borrower’s first name
* @property {string} borrower_lastname - Borrower’s middle and last name
* @property {string} borrower_unique_number - Borrower’s HKID number
* @property {string} custom_field_3012 - Borrower’s Firebase UID
*
* @example
* {
* "borrower_address": "Unit B, 2/F, 38 Shing Wo Road, Repulse Bay",
* "borrower_firstname": "Dabra",
* "borrower_lastname": "Rodriguez Casa",
* "borrower_unique_number": "WX555555(6)",
* "custom_field_3012": "hk_wx5555556"
* }
*/
/**
* Loan-related data from Loandisk
*
* @typedef {Object} loan
*
* @property {string} loan_application_id - Loan’s Loan number
* @property {number} loan_duration - Loan’s duration in month
* @property {string} loan_interest - Loan’s interest rate in percentage form
* @property {string} loan_principal_amount - Loan’s principal
* @property {string} loan_released_date - Loans’s release date
* @property {string} custom_field_1757 - Loan’s APR (effective rate)
* @property {string} custom_field_2416 - Loan’s principal in written form
* @property {string} custom_field_2864 - Loan’s collected collateral amount
*
* @example
* {
* "loan_application_id": "1000016",
* "loan_duration": 12,
* "loan_interest": "2.6600",
* "loan_principal_amount": "13000.00",
* "loan_released_date": "30/10/2019",
* "custom_field_1757": "48.00",
* "custom_field_2416": "Thirteen Thousand",
* "custom_field_2864": "2000"
* }
*/
/**
* Original loan schedule of the loan with only repayment and maturity.
*
* @typedef {array} loanSchedules
*
* @property {object[]} loanSchedule - A scheduled repayment
* @property {string} loanSchedule[].Date - Date of the scheduled repayment
* @property {string} loanSchedule[].Due - Amount due on the scheduled repayment
*
* @example
* [
* {
* "Date": "08/11/2019",
* "Due": "1,429.13",
* },
* {
* "Date": "08/12/2019",
* "Due": "1,429.17",
* },
* ]
*/
/**
* Generate the contract for a loan
*
* @param {object} data - Wrapper object for data
* @param {borrower} data.borrower - Borrower-related data from Loandisk.
* @param {loan} data.loan - Loan-related data from Loandisk.
* @param {loanSchedules} data.loanSchedules - Original loan schedule of the loan
* with only repayment and maturity.
*
* @return {string} HTML sting of the full contract
*
* @example
* generateContract({
* borrower: {
* "borrower_address": "Unit B, 2/F, 38 Shing Wo Road, Repulse Bay",
* "borrower_firstname": "Dabra",
* "borrower_lastname": "Rodriguez Casa",
* "borrower_unique_number": "WX555555(6)",
* "custom_field_3012": "hk_wx5555556"
* },
* loan: {
* "loan_application_id": "1000016",
* "loan_duration": 12,
* "loan_interest": "2.6600",
* "loan_principal_amount": "13000.00",
* "loan_released_date": "30/10/2019",
* "custom_field_1757": "48.00",
* "custom_field_2416": "Thirteen Thousand",
* "custom_field_2864": "2000"
* },
* loanSchedules: [
* {
* "Date": "08/11/2019",
* "Due": "1,429.13",
* },
* {
* "Date": "08/12/2019",
* "Due": "1,429.17",
* }
* ]
* });
*/
exports.generateContract = functions.region('asia-east2').https
.onCall(({borrower, loan, loanSchedules}) => {
return method.generate(borrower, loan, loanSchedules, false);
});
/**
* Export the contract pdf to Firebase storage
*
* @param {object} data - Wrapper object for data.
* @param {borrower} data.borrower - Borrower-related data from Loandisk.
* @param {loan} data.loan - Loan-related data from Loandisk.
* @param {loanSchedules} data.loanSchedules - Original loan schedule of the loan
* with only repayment and maturity.
*
* @return {string} URL to the saved contract.
*
* @example
* saveContract({
* borrower: {
* "borrower_address": "Unit B, 2/F, 38 Shing Wo Road, Repulse Bay",
* "borrower_firstname": "Dabra",
* "borrower_lastname": "Rodriguez Casa",
* "borrower_unique_number": "WX555555(6)",
* "custom_field_3012": "hk_wx5555556"
* },
* loan: {
* "loan_application_id": "1000016",
* "loan_duration": 12,
* "loan_interest": "2.6600",
* "loan_principal_amount": "13000.00",
* "loan_released_date": "30/10/2019",
* "custom_field_1757": "48.00",
* "custom_field_2416": "Thirteen Thousand",
* "custom_field_2864": "2000"
* },
* loanSchedules: [
* {
* "Date": "08/11/2019",
* "Due": "1,429.13",
* },
* {
* "Date": "08/12/2019",
* "Due": "1,429.17",
* }
* ]
* });
*/
exports.saveContract = functions.region('asia-east2')
.runWith({
memory: '1GB',
timeoutSeconds: 300,
}).https
.onCall(({borrower, loan, loanSchedules}) => {
return method.export(borrower, loan, loanSchedules);
});