Created
October 9, 2018 19:25
-
-
Save ivapie/bcb7a631de42fc04e8af877cb7789e6d to your computer and use it in GitHub Desktop.
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
/** | |
* Function to initiate the cronjob for all customers | |
* @params void | |
* @return void | |
*/ | |
import * as functions from 'firebase-functions'; | |
import * as req from "request"; | |
import * as admin from 'firebase-admin'; | |
const CronDaemon = functions.https.onRequest((request, response) => { | |
/*var serviceAccount = require('../../UserReach.json'); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://userreach-wordpress-prod.firebaseio.com/' | |
});*/ | |
const { reportType } = request.body; | |
const PUBLISH_URL = "https://scheduler-dot-userreach-wordpress-prod.appspot.com"; | |
/** | |
* Function to validate marketPlaceID or merchantID format. | |
* @param arg | |
* @return {boolean} | |
*/ | |
var AWSCredentialsIdValidation = function(arg){ | |
let regex = /^([A-Z0-9]){8,}$/; | |
return regex.test(arg) | |
} | |
/** | |
* Function to send the request to the gcloud function to produce the report | |
* @param data | |
* @return Promise | |
*/ | |
var sendRequest = function(data){ | |
return new Promise((resolve, reject) => { | |
let dates = determineInterval(reportType); | |
let vars = { | |
"merchantId": data.merchantID, | |
"reportType": reportType, | |
"authToken": data.mwsAuthToken, | |
"startDate": dates.startDate, | |
"endDate": dates.endDate | |
}; | |
req.post({ | |
url: PUBLISH_URL, | |
form: vars | |
}, function(error, result, body){ | |
resolve(body) | |
}); | |
}).catch(error => { | |
console.log(error); | |
}) | |
} | |
var processQueue = function(credentials){ | |
let queue = []; | |
if(credentials.length===1){ | |
return sendRequest(credentials[0]).then(result=>{ | |
return result; | |
}); | |
}else{ | |
for(let i of credentials){ | |
queue.push(sendRequest(i)); | |
} | |
return Promise.all(queue); | |
} | |
} | |
var determineInterval = function(reportType){ | |
let endDate = new Date().toISOString(); | |
let today = new Date(); | |
switch (reportType){ | |
// Every 4 hrs | |
case '_GET_MERCHANT_LISTINGS_ALL_DATA_': | |
case '_GET_AFN_INVENTORY_DATA_BY_COUNTRY_': | |
today.setHours(today.getHours() - 4 ); | |
return { | |
"startDate": today.toISOString(), | |
"endDate": endDate | |
}; | |
// Every hour | |
case '_GET_FLAT_FILE_OPEN_LISTINGS_DATA_': | |
today.setHours(today.getHours() - 1 ); | |
return { | |
"startDate": today.toISOString(), | |
"endDate": endDate | |
}; | |
// Every 15 mins | |
case '_GET_FLAT_FILE_ORDERS_DATA_': | |
today.setMinutes(today.getMinutes() - 15 ); | |
return { | |
"startDate": today.toISOString(), | |
"endDate": endDate | |
}; | |
} | |
} | |
// Get all valid users that have all necessary info. | |
return admin.database().ref('/users').once('value') | |
// Filter out invalid users. | |
.then(snapshot => { | |
const users = snapshot.val(); | |
let keys = Object.keys(users).filter(uid => users[uid].amazon); | |
let stock = []; | |
for(let i in keys){ | |
stock.push(users[keys[i]]); | |
} | |
return stock; | |
}) | |
// Request all orders per user account from last hour and write them to DB | |
.then(users => { | |
let credentialsStack = []; | |
console.log("users", users) | |
users.map((value,index)=>{ | |
for(let i in value["amazon"]){ | |
if(AWSCredentialsIdValidation(value["amazon"][i].marketplaceID) === true && AWSCredentialsIdValidation(value["amazon"][i].merchantID) === true | |
&& value["amazon"][i].mwsAuthToken !== ""){ | |
credentialsStack.push(value["amazon"][i]); | |
} | |
} | |
}); | |
return credentialsStack; | |
}).then(credentials=>{ | |
processQueue(credentials).then(result=>{ | |
response.send(result); | |
return result; | |
}); | |
}).catch(error => { | |
console.log(error); | |
}); | |
/** | |
* Test data | |
* | |
CronDaemon({body:{reportType:"_GET_MERCHANT_LISTINGS_ALL_DATA_"}}).then(response=>{ | |
console.log(response); | |
}).catch(error=>{ | |
console.log(error); | |
}); | |
*/ | |
}); | |
export default CronDaemon; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment