Created
July 26, 2016 10:32
-
-
Save anonymous/4569190cee3f547cc9bc8fae55356fae 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
return SyncJob.find({ | |
syncFlag: false | |
}).then(function(pendingJobs) { | |
console.log("\n"); | |
console.log("Pendinig jobs are " + pendingJobs.length); | |
console.log("\n"); | |
return Promise.each(pendingJobs, function(job, index, length) { | |
if (job.type === "Ticket") { | |
return Ticket.getMasterData ({}); | |
} | |
}) |
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
My task is to get all the tickets from Connectwise.com and dump in my local DB | |
There are almost 12,000 tickets there, Connectwise have done pagination on there website | |
Problem is I do not know how many pages I have. The only thing I know is that the response object become empty | |
if there is no data | |
So I am calling the function again and again untill the response become empty | |
I am calling this function in a Cron job |
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
getMasterData: function(data) { | |
var self = this; | |
console.log("calling page num is "+ page); | |
console.log(); | |
return Rest.webService({ | |
apiUrl: "/v4_6_release/apis/3.0/service/tickets?page=" +page + "&pageSize=1000", | |
method: "GET", | |
}).then (function (result) { | |
if (result.data == 0) { | |
return {status: true,code:200}; | |
} else { | |
return Ticket.find().then(function(dbTickets) { | |
if (dbTickets.length === 0) { | |
return Ticket.create(result.data).then(function(tickets) { | |
page++; | |
self.getMasterData({}); | |
}).catch(function(err) { | |
return { | |
status: false, | |
code: 400, | |
message: "Error has been occured" | |
}; | |
}); | |
} else { | |
return Promise.each(result.data, function(value, index, length) { | |
if (typeof dbTickets[index] == "undefined") { | |
return Ticket.create(value); | |
} | |
if (value._info.lastUpdated) { | |
cwDate = new Date(value._info.lastUpdated); | |
dbDate = new Date(dbTickets[index]._info.lastUpdated); | |
if (value.id == dbTickets[index].id) { | |
if (cwDate > dbDate) { | |
return Ticket.update({ | |
"id": value.id | |
}, { | |
$set: value | |
}).then(function(result) { | |
}).catch(function(err) { | |
}); | |
} | |
} else { | |
return Ticket.create(value); | |
} | |
} else { | |
return Ticket.update ({ | |
"id": value.id | |
}, { | |
$set: value | |
}); | |
} | |
}).then(function(result) { | |
page++; | |
self.getMasterData({}); | |
}); | |
} | |
}).catch(function(err) { | |
return { | |
status: false, | |
code: 400, | |
message: "Error has been occured", | |
error: err | |
}; | |
}); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment