null fields when there is no valid integer arriving from elopage.

Furthermore try catch on save to prevent server crashes
This commit is contained in:
Ulf Gebhardt 2022-02-13 12:41:16 +01:00
parent 65f76f82ed
commit 1a1e011e6d
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9

View File

@ -53,12 +53,13 @@ export const elopageWebhook = async (req: any, res: any): Promise<void> => {
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<void> => {
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<void> => {
}
// 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<void> => {
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<void> => {
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