From 1a1e011e6dddce8d98a71b9167a66bf9e16a69e4 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Sun, 13 Feb 2022 12:41:16 +0100 Subject: [PATCH] null fields when there is no valid integer arriving from elopage. Furthermore try catch on save to prevent server crashes --- backend/src/webhook/elopage.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/backend/src/webhook/elopage.ts b/backend/src/webhook/elopage.ts index af6b5a097..d2090a54b 100644 --- a/backend/src/webhook/elopage.ts +++ b/backend/src/webhook/elopage.ts @@ -53,12 +53,13 @@ export const elopageWebhook = async (req: any, res: any): Promise => { membership, } = req.body - loginElopageBuy.affiliateProgramId = parseInt(product.affiliate_program_id) - loginElopageBuy.publisherId = parseInt(publisher.id) - loginElopageBuy.orderId = parseInt(order_id) - loginElopageBuy.productId = parseInt(product_id) + loginElopageBuy.affiliateProgramId = parseInt(product.affiliate_program_id) || null + loginElopageBuy.publisherId = parseInt(publisher.id) || null + loginElopageBuy.orderId = parseInt(order_id) || null + loginElopageBuy.productId = parseInt(product_id) || null // TODO: WHAT THE ACTUAL FUK? Please save this as float in the future directly in the database - loginElopageBuy.productPrice = Math.trunc(parseFloat(product.price) * 100) + const productPrice = parseFloat(product.price) + loginElopageBuy.productPrice = productPrice ? Math.trunc(productPrice * 100) : 0 loginElopageBuy.payerEmail = payer.email loginElopageBuy.publisherEmail = publisher.email // eslint-disable-next-line camelcase @@ -66,7 +67,7 @@ export const elopageWebhook = async (req: any, res: any): Promise => { loginElopageBuy.successDate = new Date(success_date) loginElopageBuy.event = event // TODO this was never set on login_server - its unclear if this is the correct value - loginElopageBuy.elopageUserId = parseInt(membership.id) + loginElopageBuy.elopageUserId = parseInt(membership.id) || null const firstName = payer.first_name const lastName = payer.last_name @@ -79,7 +80,13 @@ export const elopageWebhook = async (req: any, res: any): Promise => { } // Save the hook data - await LoginElopageBuys.save(loginElopageBuy) + try { + await LoginElopageBuys.save(loginElopageBuy) + } catch (error) { + // eslint-disable-next-line no-console + console.log('Error saving LoginElopageBuy', error) + return + } // create user for certain products /* @@ -90,7 +97,10 @@ export const elopageWebhook = async (req: any, res: any): Promise => { Business-Mitgliedschaft, 43960 Förderbeitrag: 49106 */ - if ([36001, 43741, 43870, 43944, 43960, 49106].includes(loginElopageBuy.productId)) { + if ( + loginElopageBuy.productId && + [36001, 43741, 43870, 43944, 43960, 49106].includes(loginElopageBuy.productId) + ) { const email = loginElopageBuy.payerEmail const VALIDATE_EMAIL = /^[a-zA-Z0-9.!#$%&?*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/ @@ -123,7 +133,7 @@ export const elopageWebhook = async (req: any, res: any): Promise => { email, firstName, lastName, - publisherId: loginElopageBuy.publisherId, + publisherId: loginElopageBuy.publisherId || 0, // This seemed to be the default value if not set }) } catch (error) { // eslint-disable-next-line no-console