diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 46cb00ed2..64f3fd667 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -4,7 +4,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0020-rename_and_clean_state_users', + DB_VERSION: '0021-elopagebuys_fields_nullable', } const server = { 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 diff --git a/database/entity/0003-login_server_tables/LoginElopageBuys.ts b/database/entity/0003-login_server_tables/LoginElopageBuys.ts index fc0d6c355..1f94faffe 100644 --- a/database/entity/0003-login_server_tables/LoginElopageBuys.ts +++ b/database/entity/0003-login_server_tables/LoginElopageBuys.ts @@ -5,8 +5,8 @@ export class LoginElopageBuys extends BaseEntity { @PrimaryGeneratedColumn('increment', { unsigned: true }) id: number - @Column({ name: 'elopage_user_id', nullable: false }) - elopageUserId: number + @Column({ type: 'int', width: 11, name: 'elopage_user_id', nullable: true, default: null }) + elopageUserId: number | null @Column({ name: 'affiliate_program_id', nullable: false }) affiliateProgramId: number diff --git a/database/entity/0021-elopagebuys_fields_nullable/LoginElopageBuys.ts b/database/entity/0021-elopagebuys_fields_nullable/LoginElopageBuys.ts new file mode 100644 index 000000000..501276ea7 --- /dev/null +++ b/database/entity/0021-elopagebuys_fields_nullable/LoginElopageBuys.ts @@ -0,0 +1,52 @@ +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm' + +@Entity('login_elopage_buys') +export class LoginElopageBuys extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ type: 'int', width: 11, name: 'elopage_user_id', nullable: true, default: null }) + elopageUserId: number | null + + @Column({ type: 'int', width: 11, name: 'affiliate_program_id', nullable: true, default: null }) + affiliateProgramId: number | null + + @Column({ type: 'int', width: 11, name: 'publisher_id', nullable: true, default: null }) + publisherId: number | null + + @Column({ type: 'int', width: 11, name: 'order_id', nullable: true, default: null }) + orderId: number | null + + @Column({ type: 'int', width: 11, name: 'product_id', nullable: true, default: null }) + productId: number | null + + @Column({ name: 'product_price', nullable: false }) + productPrice: number + + @Column({ + name: 'payer_email', + length: 255, + nullable: false, + charset: 'utf8', + collation: 'utf8_bin', + }) + payerEmail: string + + @Column({ + name: 'publisher_email', + length: 255, + nullable: false, + charset: 'utf8', + collation: 'utf8_bin', + }) + publisherEmail: string + + @Column({ nullable: false }) + payed: boolean + + @Column({ name: 'success_date', nullable: false }) + successDate: Date + + @Column({ length: 255, nullable: false }) + event: string +} diff --git a/database/entity/LoginElopageBuys.ts b/database/entity/LoginElopageBuys.ts index 6a1f3230b..cb0d212fa 100644 --- a/database/entity/LoginElopageBuys.ts +++ b/database/entity/LoginElopageBuys.ts @@ -1 +1 @@ -export { LoginElopageBuys } from './0003-login_server_tables/LoginElopageBuys' +export { LoginElopageBuys } from './0021-elopagebuys_fields_nullable/LoginElopageBuys' diff --git a/database/migrations/0021-elopagebuys_fields_nullable.ts b/database/migrations/0021-elopagebuys_fields_nullable.ts new file mode 100644 index 000000000..dabe9d1e4 --- /dev/null +++ b/database/migrations/0021-elopagebuys_fields_nullable.ts @@ -0,0 +1,25 @@ +/* MIGRATION TO ALLOW NULL FIELDS ON ELOPAGEBUYS + * + * This migration allows null on `affiliate_program_id`, + * `publisher_id`, `order_id`. `product_id`. + */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `login_elopage_buys` MODIFY COLUMN `affiliate_program_id` int(11) DEFAULT NULL;', + ) + await queryFn( + 'ALTER TABLE `login_elopage_buys` MODIFY COLUMN `publisher_id` int(11) DEFAULT NULL;', + ) + await queryFn('ALTER TABLE `login_elopage_buys` MODIFY COLUMN `order_id` int(11) DEFAULT NULL;') + await queryFn('ALTER TABLE `login_elopage_buys` MODIFY COLUMN `product_id` int(11) DEFAULT NULL;') +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `login_elopage_buys` MODIFY COLUMN `affiliate_program_id` int(11) NOT NULL;', + ) + await queryFn('ALTER TABLE `login_elopage_buys` MODIFY COLUMN `publisher_id` int(11) NOT NULL;') + await queryFn('ALTER TABLE `login_elopage_buys` MODIFY COLUMN `order_id` int(11) NOT NULL;') + await queryFn('ALTER TABLE `login_elopage_buys` MODIFY COLUMN `product_id` int(11) NOT NULL;') +}