Fix exception handling. Don't save < 0.01 donations to DB.

refs #133
This commit is contained in:
softsimon 2020-10-17 15:01:58 +07:00
parent 0fca6f3a3b
commit 2715d02cf9
No known key found for this signature in database
GPG key ID: 488D7DCFB5A430D7

View file

@ -14,10 +14,9 @@ class Donations {
}; };
constructor() { constructor() {
this.runMigration();
} }
setNotfyDonationStatusCallback(fn: any) { setNotfyDonationStatusCallback(fn: any): void {
this.notifyDonationStatusCallback = fn; this.notifyDonationStatusCallback = fn;
} }
@ -49,7 +48,7 @@ class Donations {
}); });
} }
async $handleWebhookRequest(data: any) { async $handleWebhookRequest(data: any): Promise<void> {
if (!data || !data.id) { if (!data || !data.id) {
return; return;
} }
@ -67,27 +66,21 @@ class Donations {
return; return;
} }
let imageBlob = '';
let handle = '';
let imageUrl = '';
let twitter_id = null;
if (response.orderId !== '') { if (response.orderId !== '') {
try { try {
const userData = await this.$getTwitterUserData(response.orderId); const userData = await this.$getTwitterUserData(response.orderId);
imageUrl = userData.profile_image_url.replace('normal', '200x200'); const imageUrl = userData.profile_image_url.replace('normal', '200x200');
imageBlob = await this.$downloadProfileImageBlob(imageUrl); const imageBlob = await this.$downloadProfileImageBlob(imageUrl);
handle = userData.screen_name;
twitter_id = userData.id; logger.debug('Creating database entry for donation with invoice id: ' + response.id);
this.$addDonationToDatabase(response.btcPaid, userData.screen_name, userData.id, response.id, imageUrl, imageBlob);
} catch (e) { } catch (e) {
logger.err('Error fetching twitter data: ' + e.message); logger.err(`Error fetching twitter data for handle ${response.orderId}: ${e.message}`);
} }
} }
logger.debug('Creating database entry for donation with invoice id: ' + response.id);
this.$addDonationToDatabase(response.btcPaid, handle, twitter_id, response.id, imageUrl, imageBlob);
} }
async $getDonationsFromDatabase() { async $getDonationsFromDatabase(): Promise<any[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.pool.getConnection();
const query = `SELECT handle, imageUrl, TO_BASE64(image) AS image_64 FROM donations WHERE handle != '' ORDER BY id DESC`; const query = `SELECT handle, imageUrl, TO_BASE64(image) AS image_64 FROM donations WHERE handle != '' ORDER BY id DESC`;
@ -96,10 +89,11 @@ class Donations {
return rows; return rows;
} catch (e) { } catch (e) {
logger.err('$getDonationsFromDatabase() error' + e); logger.err('$getDonationsFromDatabase() error' + e);
return [];
} }
} }
private async $getLegacyDonations() { private async $getOldDonations(): Promise<any[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.pool.getConnection();
const query = `SELECT * FROM donations WHERE twitter_id IS NULL AND handle != ''`; const query = `SELECT * FROM donations WHERE twitter_id IS NULL AND handle != ''`;
@ -108,6 +102,7 @@ class Donations {
return rows; return rows;
} catch (e) { } catch (e) {
logger.err('$getLegacyDonations() error' + e); logger.err('$getLegacyDonations() error' + e);
return [];
} }
} }
@ -195,9 +190,9 @@ class Donations {
}); });
} }
private async runMigration() { private async refreshSponsors(): Promise<void> {
const legacyDonations = await this.$getLegacyDonations(); const oldDonations = await this.$getOldDonations();
legacyDonations.forEach(async (donation: any) => { oldDonations.forEach(async (donation: any) => {
logger.debug('Migrating donation for handle: ' + donation.handle); logger.debug('Migrating donation for handle: ' + donation.handle);
try { try {
const twitterData = await this.$getTwitterUserData(donation.handle); const twitterData = await this.$getTwitterUserData(donation.handle);