From 2369e682f5633fb59873d58dd7b2c0657e742e2d Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 4 Apr 2023 19:05:11 +0200 Subject: [PATCH 01/22] Change throw to return false and add console log --- backend/src/apis/KlicktippController.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 710e2c611..1df6bc7ab 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -97,15 +97,13 @@ export const addFieldsToSubscriber = async ( newemail = '', newsmsnumber = '', ) => { - return callKlickTippAPI( - async ({ email, fields, newemail, newsmsnumber }) => { - const isLogin = await loginKlicktippUser() - if (isLogin) { - const subscriberId = await klicktippConnector.subscriberSearch(email) - return klicktippConnector.subscriberUpdate(subscriberId, fields, newemail, newsmsnumber) - } - throw new LogError(`Could not add fields (${fields}) to subscriber ${email}`) - }, - { email, fields, newemail, newsmsnumber }, - ) + console.log('email', email) + console.log('fields', fields) + if (!CONFIG.KLICKTIPP) return true + const isLogin = await loginKlicktippUser() + if (isLogin) { + const subscriberId = await klicktippConnector.subscriberSearch(email) + return klicktippConnector.subscriberUpdate(subscriberId, fields, newemail, newsmsnumber) + } + return false } From 05e0ab1f7b99eb9942407f6feccb9c8b0ee38bc5 Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 4 Apr 2023 19:09:09 +0200 Subject: [PATCH 02/22] Query email to last createdAt for given event type --- backend/src/graphql/resolver/util/eventList.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 backend/src/graphql/resolver/util/eventList.ts diff --git a/backend/src/graphql/resolver/util/eventList.ts b/backend/src/graphql/resolver/util/eventList.ts new file mode 100644 index 000000000..856d327eb --- /dev/null +++ b/backend/src/graphql/resolver/util/eventList.ts @@ -0,0 +1,16 @@ +import { Event as DbEvent } from '@entity/Event' +import { User } from '@entity/User' +import { UserContact } from '@entity/UserContact' + +export const lastDateTimeEvents = async (eventType: string): Promise => { + return DbEvent + .createQueryBuilder('event') + .select('MAX(event.created_at)', 'value') + .leftJoin(User, 'user', 'affected_user_id = user.id') + .leftJoin(UserContact, 'usercontact', 'user.id = usercontact.user_id') + .addSelect('usercontact.email', 'email') + .where('event.type = :eventType', { eventType }) + .andWhere('usercontact.email IS NOT NULL') + .groupBy('event.affected_user_id') + .getRawMany() +} \ No newline at end of file From f486bc0fd0efe0f595d520cc6697402652fa4783 Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 4 Apr 2023 19:10:25 +0200 Subject: [PATCH 03/22] Extern trigger for setting fields to subscriber --- backend/src/util/klicktipp.ts | 39 +++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 4d90be134..4ee5455a8 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,8 +1,12 @@ + // eslint-disable @typescript-eslint/no-explicit-any import { User } from '@entity/User' -import { getKlickTippUser } from '@/apis/KlicktippController' +import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' import LogError from '@/server/LogError' import connection from '@/typeorm/connection' +import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' +import { EventType } from '@/event/EventType' +import { Event as DbEvent } from '@/event/Event' export async function retrieveNotRegisteredEmails(): Promise { const con = await connection() @@ -27,4 +31,35 @@ export async function retrieveNotRegisteredEmails(): Promise { return notRegisteredUser } -void retrieveNotRegisteredEmails() +function klickTippSendFieldToUser(events: any, value: string): void { + for(const event of events) { + addFieldsToSubscriber(event.email, { [value]: event.value }) + } +} + +export async function exportEventDataToKlickTipp(): Promise { + const connectionInstance = await connection() + if (!connectionInstance) { + throw new LogError('No connection to database') + } + + const lastLoginEvents = await lastDateTimeEvents(EventType.USER_LOGIN) + klickTippSendFieldToUser(lastLoginEvents, 'GDD last login') + + const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) + klickTippSendFieldToUser(registeredEvents, 'GDD date of registration') + + const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) + klickTippSendFieldToUser(receiveTransactionEvents, 'GDD last received') + + const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) + klickTippSendFieldToUser(contributionCreateEvents, 'GDD last sent') + + const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) + klickTippSendFieldToUser(linkRedeemedEvents, 'GDD last invited') + + const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) + klickTippSendFieldToUser(confirmContributionEvents, 'GDD last created') +} +void exportEventDataToKlickTipp() +// void retrieveNotRegisteredEmails() From d09aa6c43dc36e5fdaa568455f509b814f10eac1 Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 4 Apr 2023 19:38:17 +0200 Subject: [PATCH 04/22] Fix linting rules --- backend/src/apis/KlicktippController.ts | 2 ++ .../src/graphql/resolver/util/eventList.ts | 9 ++--- backend/src/util/klicktipp.ts | 34 ++++++++++--------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 1df6bc7ab..a705e70e0 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -97,7 +97,9 @@ export const addFieldsToSubscriber = async ( newemail = '', newsmsnumber = '', ) => { + // eslint-disable-next-line no-console console.log('email', email) + // eslint-disable-next-line no-console console.log('fields', fields) if (!CONFIG.KLICKTIPP) return true const isLogin = await loginKlicktippUser() diff --git a/backend/src/graphql/resolver/util/eventList.ts b/backend/src/graphql/resolver/util/eventList.ts index 856d327eb..45afe1832 100644 --- a/backend/src/graphql/resolver/util/eventList.ts +++ b/backend/src/graphql/resolver/util/eventList.ts @@ -2,9 +2,10 @@ import { Event as DbEvent } from '@entity/Event' import { User } from '@entity/User' import { UserContact } from '@entity/UserContact' -export const lastDateTimeEvents = async (eventType: string): Promise => { - return DbEvent - .createQueryBuilder('event') +export const lastDateTimeEvents = async ( + eventType: string, +): Promise<{ email: string; value: Date }[]> => { + return DbEvent.createQueryBuilder('event') .select('MAX(event.created_at)', 'value') .leftJoin(User, 'user', 'affected_user_id = user.id') .leftJoin(UserContact, 'usercontact', 'user.id = usercontact.user_id') @@ -13,4 +14,4 @@ export const lastDateTimeEvents = async (eventType: string): Promise .andWhere('usercontact.email IS NOT NULL') .groupBy('event.affected_user_id') .getRawMany() -} \ No newline at end of file +} diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 4ee5455a8..762dac961 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,12 +1,11 @@ - // eslint-disable @typescript-eslint/no-explicit-any +// eslint-disable @typescript-eslint/no-explicit-any import { User } from '@entity/User' import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' +import { EventType } from '@/event/EventType' +import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' import LogError from '@/server/LogError' import connection from '@/typeorm/connection' -import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' -import { EventType } from '@/event/EventType' -import { Event as DbEvent } from '@/event/Event' export async function retrieveNotRegisteredEmails(): Promise { const con = await connection() @@ -31,9 +30,12 @@ export async function retrieveNotRegisteredEmails(): Promise { return notRegisteredUser } -function klickTippSendFieldToUser(events: any, value: string): void { - for(const event of events) { - addFieldsToSubscriber(event.email, { [value]: event.value }) +async function klickTippSendFieldToUser( + events: { email: string; value: Date }[], + value: string, +): Promise { + for (const event of events) { + await addFieldsToSubscriber(event.email, { [value]: event.value }) } } @@ -44,22 +46,22 @@ export async function exportEventDataToKlickTipp(): Promise { } const lastLoginEvents = await lastDateTimeEvents(EventType.USER_LOGIN) - klickTippSendFieldToUser(lastLoginEvents, 'GDD last login') + void klickTippSendFieldToUser(lastLoginEvents, 'GDD last login') const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) - klickTippSendFieldToUser(registeredEvents, 'GDD date of registration') + void klickTippSendFieldToUser(registeredEvents, 'GDD date of registration') const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) - klickTippSendFieldToUser(receiveTransactionEvents, 'GDD last received') - + void klickTippSendFieldToUser(receiveTransactionEvents, 'GDD last received') + const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) - klickTippSendFieldToUser(contributionCreateEvents, 'GDD last sent') - + void klickTippSendFieldToUser(contributionCreateEvents, 'GDD last sent') + const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) - klickTippSendFieldToUser(linkRedeemedEvents, 'GDD last invited') - + void klickTippSendFieldToUser(linkRedeemedEvents, 'GDD last invited') + const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) - klickTippSendFieldToUser(confirmContributionEvents, 'GDD last created') + void klickTippSendFieldToUser(confirmContributionEvents, 'GDD last created') } void exportEventDataToKlickTipp() // void retrieveNotRegisteredEmails() From 5ae587f255dad0f7ca0ff2a03b7eafb53596511f Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 20 Apr 2023 14:06:57 +0200 Subject: [PATCH 05/22] Add logging and add fieldIndex --- backend/src/apis/KlicktippController.ts | 35 ++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index a705e70e0..a3e2c4ec6 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -5,12 +5,22 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import CONFIG from '@/config' +import { backendLogger as logger } from '@/server/logger' // eslint-disable-next-line import/no-relative-parent-imports import KlicktippConnector from 'klicktipp-api' const klicktippConnector = new KlicktippConnector() +export const fieldIndex = async () => { + const isLogin = await loginKlicktippUser() + if (isLogin) { + const result = await klicktippConnector.fieldIndex() + return result + } + return false +} + export const klicktippSignIn = async ( email: string, language: string, @@ -47,9 +57,14 @@ export const getKlickTippUser = async (email: string): Promise => { if (!CONFIG.KLICKTIPP) return true const isLogin = await loginKlicktippUser() if (isLogin) { - const subscriberId = await klicktippConnector.subscriberSearch(email) - const result = await klicktippConnector.subscriberGet(subscriberId) - return result + try { + const subscriberId = await klicktippConnector.subscriberSearch(email) + return klicktippConnector.subscriberGet(subscriberId) + } catch (e) { + logger.error(`Could not find subscriber ${email}`) + return false + } + } return false } @@ -97,15 +112,17 @@ export const addFieldsToSubscriber = async ( newemail = '', newsmsnumber = '', ) => { - // eslint-disable-next-line no-console - console.log('email', email) - // eslint-disable-next-line no-console - console.log('fields', fields) if (!CONFIG.KLICKTIPP) return true const isLogin = await loginKlicktippUser() if (isLogin) { - const subscriberId = await klicktippConnector.subscriberSearch(email) - return klicktippConnector.subscriberUpdate(subscriberId, fields, newemail, newsmsnumber) + try { + const subscriberId = await klicktippConnector.subscriberSearch(email) + const result = await klicktippConnector.subscriberUpdate(subscriberId, fields, newemail, newsmsnumber) + return result + } catch (e) { + logger.error(`Could not update subscriber ${email}, ${JSON.stringify(fields)}, ${e}`) + return false + } } return false } From 291146caf4c873d72c1a9fdcfcbd0140222e0380 Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 20 Apr 2023 14:08:37 +0200 Subject: [PATCH 06/22] Change field names to api id, add fieldIndex --- backend/src/util/klicktipp.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 762dac961..0eadd1aec 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,7 +1,7 @@ // eslint-disable @typescript-eslint/no-explicit-any import { User } from '@entity/User' -import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' +import { getKlickTippUser, addFieldsToSubscriber, fieldIndex } from '@/apis/KlicktippController' import { EventType } from '@/event/EventType' import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' import LogError from '@/server/LogError' @@ -40,28 +40,31 @@ async function klickTippSendFieldToUser( } export async function exportEventDataToKlickTipp(): Promise { + // const fields = await fieldIndex() + // console.log('fields', fields) + // return const connectionInstance = await connection() if (!connectionInstance) { throw new LogError('No connection to database') } const lastLoginEvents = await lastDateTimeEvents(EventType.USER_LOGIN) - void klickTippSendFieldToUser(lastLoginEvents, 'GDD last login') + void klickTippSendFieldToUser(lastLoginEvents, 'field186060') - const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) - void klickTippSendFieldToUser(registeredEvents, 'GDD date of registration') + // const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) + // void klickTippSendFieldToUser(registeredEvents, 'field186061') - const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) - void klickTippSendFieldToUser(receiveTransactionEvents, 'GDD last received') + // const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) + // void klickTippSendFieldToUser(receiveTransactionEvents, 'field185674') - const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) - void klickTippSendFieldToUser(contributionCreateEvents, 'GDD last sent') + // const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) + // void klickTippSendFieldToUser(contributionCreateEvents, 'field185673') - const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) - void klickTippSendFieldToUser(linkRedeemedEvents, 'GDD last invited') + // const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) + // void klickTippSendFieldToUser(linkRedeemedEvents, 'field185676') - const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) - void klickTippSendFieldToUser(confirmContributionEvents, 'GDD last created') + // const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) + // void klickTippSendFieldToUser(confirmContributionEvents, 'field185675') } void exportEventDataToKlickTipp() // void retrieveNotRegisteredEmails() From d95f50b95411666ed336d794ef1e6fc8b5c86675 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 3 May 2023 12:48:02 +0200 Subject: [PATCH 07/22] Send date field to klicktipp api. --- backend/src/apis/KlicktippController.ts | 1 + backend/src/util/klicktipp.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index a3e2c4ec6..ea47a2f66 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -118,6 +118,7 @@ export const addFieldsToSubscriber = async ( try { const subscriberId = await klicktippConnector.subscriberSearch(email) const result = await klicktippConnector.subscriberUpdate(subscriberId, fields, newemail, newsmsnumber) + logger.info(`Update of subscriber (${email}) has been successful, ${result}`) return result } catch (e) { logger.error(`Could not update subscriber ${email}, ${JSON.stringify(fields)}, ${e}`) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 0eadd1aec..4e8bec2cd 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -35,7 +35,8 @@ async function klickTippSendFieldToUser( value: string, ): Promise { for (const event of events) { - await addFieldsToSubscriber(event.email, { [value]: event.value }) + const time = event.value.setSeconds(0) + await addFieldsToSubscriber(event.email, { [value]: Math.trunc(time / 1000) }) } } From fceddcec15f7c61c604d8964c7551ae31e2dde02 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 3 May 2023 12:50:21 +0200 Subject: [PATCH 08/22] Add other date fields to send to klicktipp. --- backend/src/util/klicktipp.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 4e8bec2cd..5427d61d8 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -52,20 +52,20 @@ export async function exportEventDataToKlickTipp(): Promise { const lastLoginEvents = await lastDateTimeEvents(EventType.USER_LOGIN) void klickTippSendFieldToUser(lastLoginEvents, 'field186060') - // const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) - // void klickTippSendFieldToUser(registeredEvents, 'field186061') + const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) + void klickTippSendFieldToUser(registeredEvents, 'field186061') - // const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) - // void klickTippSendFieldToUser(receiveTransactionEvents, 'field185674') + const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) + void klickTippSendFieldToUser(receiveTransactionEvents, 'field185674') - // const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) - // void klickTippSendFieldToUser(contributionCreateEvents, 'field185673') + const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) + void klickTippSendFieldToUser(contributionCreateEvents, 'field185673') - // const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) - // void klickTippSendFieldToUser(linkRedeemedEvents, 'field185676') + const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) + void klickTippSendFieldToUser(linkRedeemedEvents, 'field185676') - // const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) - // void klickTippSendFieldToUser(confirmContributionEvents, 'field185675') + const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) + void klickTippSendFieldToUser(confirmContributionEvents, 'field185675') } void exportEventDataToKlickTipp() // void retrieveNotRegisteredEmails() From bf0eabae98e79cea8bbf24566e2a8ef5f49a688d Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 4 May 2023 11:26:04 +0200 Subject: [PATCH 09/22] Remove unused import --- backend/src/middleware/klicktippMiddleware.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/middleware/klicktippMiddleware.ts b/backend/src/middleware/klicktippMiddleware.ts index 544289c4b..ad7553d97 100644 --- a/backend/src/middleware/klicktippMiddleware.ts +++ b/backend/src/middleware/klicktippMiddleware.ts @@ -8,7 +8,6 @@ import { MiddlewareFn } from 'type-graphql' import { KlickTipp } from '@model/KlickTipp' import { /* klicktippSignIn, */ getKlickTippUser } from '@/apis/KlicktippController' -import { CONFIG } from '@/config' import { klickTippLogger as logger } from '@/server/logger' // export const klicktippRegistrationMiddleware: MiddlewareFn = async ( From 77acaabe270917876020f6f9cf0233f5678dcc6c Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 4 May 2023 15:29:19 +0200 Subject: [PATCH 10/22] Fix linting rules. --- backend/src/util/klicktipp.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index bf3ed6514..b12c79c38 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,7 +1,7 @@ // eslint-disable @typescript-eslint/no-explicit-any import { User } from '@entity/User' -import { getKlickTippUser, addFieldsToSubscriber, fieldIndex } from '@/apis/KlicktippController' +import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' import { EventType } from '@/event/EventType' import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' import { LogError } from '@/server/LogError' From 225f41fbbf84571e4183c42bbb7b4efed06b2178 Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 5 May 2023 11:34:04 +0200 Subject: [PATCH 11/22] Remove unused test method. --- backend/src/apis/KlicktippController.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index be723dd26..c1a895415 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -13,15 +13,6 @@ import KlicktippConnector from 'klicktipp-api' const klicktippConnector = new KlicktippConnector() -export const fieldIndex = async () => { - const isLogin = await loginKlicktippUser() - if (isLogin) { - const result = await klicktippConnector.fieldIndex() - return result - } - return false -} - export const klicktippSignIn = async ( email: string, language: string, From 8c31fc5789b0947db78324cb506fd69bb36fcb1f Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 5 May 2023 11:34:29 +0200 Subject: [PATCH 12/22] Remove test code to find out the data of the fields. --- backend/src/util/klicktipp.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index b12c79c38..8af5cc0ba 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -41,9 +41,6 @@ async function klickTippSendFieldToUser( } export async function exportEventDataToKlickTipp(): Promise { - // const fields = await fieldIndex() - // console.log('fields', fields) - // return const connectionInstance = await connection() if (!connectionInstance) { throw new LogError('No connection to database') From 466ace75cd266200f322aaf39f0403938b5ad3ab Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 5 May 2023 12:23:44 +0200 Subject: [PATCH 13/22] Remove update of cypress version of this pr. --- e2e-tests/yarn.lock | 6 ------ 1 file changed, 6 deletions(-) diff --git a/e2e-tests/yarn.lock b/e2e-tests/yarn.lock index 6f86093d0..1fffb6870 100644 --- a/e2e-tests/yarn.lock +++ b/e2e-tests/yarn.lock @@ -2194,15 +2194,9 @@ crypto-browserify@^3.0.0: randomfill "^1.0.3" cypress@^12.7.0: -<<<<<<< HEAD - version "12.9.0" - resolved "https://registry.yarnpkg.com/cypress/-/cypress-12.9.0.tgz#e6ab43cf329fd7c821ef7645517649d72ccf0a12" - integrity sha512-Ofe09LbHKgSqX89Iy1xen2WvpgbvNxDzsWx3mgU1mfILouELeXYGwIib3ItCwoRrRifoQwcBFmY54Vs0zw7QCg== -======= version "12.8.1" resolved "https://registry.yarnpkg.com/cypress/-/cypress-12.8.1.tgz#0c6e67f34554d553138697aaf349b637d80004eb" integrity sha512-lIFbKdaSYAOarNLHNFa2aPZu6YSF+8UY4VRXMxJrFUnk6RvfG0AWsZ7/qle/aIz30TNUD4aOihz2ZgS4vuQVSA== ->>>>>>> master dependencies: "@cypress/request" "^2.88.10" "@cypress/xvfb" "^1.2.4" From 4da0f8d0f7994b21d5fb0ad01ce31b6bfc5b95be Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 11 May 2023 10:03:42 +0200 Subject: [PATCH 14/22] First steps for testing klicktipp external scripts. --- backend/src/util/klicktipp.test.ts | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 backend/src/util/klicktipp.test.ts diff --git a/backend/src/util/klicktipp.test.ts b/backend/src/util/klicktipp.test.ts new file mode 100644 index 000000000..9972c570d --- /dev/null +++ b/backend/src/util/klicktipp.test.ts @@ -0,0 +1,75 @@ +/* eslint-disable @typescript-eslint/no-unsafe-return */ +/* eslint-disable @typescript-eslint/no-unsafe-call */ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ +import { Connection } from '@dbTools/typeorm' +import { Event as DbEvent } from '@entity/Event' +import { ApolloServerTestClient } from 'apollo-server-testing' + +import { testEnvironment, cleanDB, resetToken } from '@test/helpers' + +import { addFieldsToSubscriber } from '@/apis/KlicktippController' +import { creations } from '@/seeds/creation' +import { creationFactory } from '@/seeds/factory/creation' +import { userFactory } from '@/seeds/factory/user' +import { login } from '@/seeds/graphql/mutations' +import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' + +import { exportEventDataToKlickTipp } from './klicktipp' + +jest.mock('@/apis/KlicktippController', () => { + const originalModule = jest.requireActual('@/apis/KlicktippController') + return { + __esModule: true, + ...originalModule, + getKlickTippUser: jest.fn((email) => originalModule.getKlickTippUser(email)), + addFieldsToSubscriber: jest.fn((email, a) => originalModule.addFieldsToSubscriber(email, a)), + } +}) + +let mutate: ApolloServerTestClient['mutate'], + query: ApolloServerTestClient['query'], + con: Connection +let testEnv: { + mutate: ApolloServerTestClient['mutate'] + query: ApolloServerTestClient['query'] + con: Connection +} + +beforeAll(async () => { + testEnv = await testEnvironment() + mutate = testEnv.mutate + query = testEnv.query + con = testEnv.con + await DbEvent.clear() +}) + +afterAll(async () => { + await cleanDB() + await con.close() +}) + +describe('klicktipp', () => { + beforeAll(async () => { + await userFactory(testEnv, bibiBloxberg) + const bibisCreation = creations.find((creation) => creation.email === 'bibi@bloxberg.de') + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + await creationFactory(testEnv, bibisCreation!) + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + void exportEventDataToKlickTipp() + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + describe('exportEventDataToKlickTipp', () => { + it('calls the KlicktippController', () => { + expect(addFieldsToSubscriber).toBeCalled() + }) + }) +}) From 10c0cf8370050aaba7c229f4e9c486e60ece2c15 Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 12 May 2023 18:23:36 +0200 Subject: [PATCH 15/22] Test for the klicktipp tag export. --- backend/src/util/klicktipp.test.ts | 16 ++++++++++++++-- backend/src/util/klicktipp.ts | 21 ++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/backend/src/util/klicktipp.test.ts b/backend/src/util/klicktipp.test.ts index 9972c570d..d6416a2f0 100644 --- a/backend/src/util/klicktipp.test.ts +++ b/backend/src/util/klicktipp.test.ts @@ -14,6 +14,8 @@ import { creationFactory } from '@/seeds/factory/creation' import { userFactory } from '@/seeds/factory/user' import { login } from '@/seeds/graphql/mutations' import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' +import { peterLustig } from '@/seeds/users/peter-lustig' +import { connection } from '@/typeorm/connection' import { exportEventDataToKlickTipp } from './klicktipp' @@ -27,6 +29,15 @@ jest.mock('@/apis/KlicktippController', () => { } }) +// jest.mock('@/typeorm/connection', () => { +// const originalModule = jest.requireActual('@/typeorm/connection') +// return { +// __esModule: true, +// ...originalModule, +// connection: jest.fn(() => Promise.resolve(con)), +// } +// }) + let mutate: ApolloServerTestClient['mutate'], query: ApolloServerTestClient['query'], con: Connection @@ -52,6 +63,7 @@ afterAll(async () => { describe('klicktipp', () => { beforeAll(async () => { await userFactory(testEnv, bibiBloxberg) + await userFactory(testEnv, peterLustig) const bibisCreation = creations.find((creation) => creation.email === 'bibi@bloxberg.de') // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await creationFactory(testEnv, bibisCreation!) @@ -59,11 +71,11 @@ describe('klicktipp', () => { mutation: login, variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, }) + await con.close() void exportEventDataToKlickTipp() }) - afterAll(async () => { - await cleanDB() + afterAll(() => { resetToken() }) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index f6c631205..ff5828c15 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,4 +1,5 @@ // eslint-disable @typescript-eslint/no-explicit-any +import { Connection } from '@dbTools/typeorm' import { User } from '@entity/User' import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' @@ -39,29 +40,35 @@ async function klickTippSendFieldToUser( } } +function getMyConnection(): Promise { + return connection() +} + export async function exportEventDataToKlickTipp(): Promise { - const connectionInstance = await connection() + const connectionInstance = await getMyConnection() if (!connectionInstance) { throw new LogError('No connection to database') } const lastLoginEvents = await lastDateTimeEvents(EventType.USER_LOGIN) - void klickTippSendFieldToUser(lastLoginEvents, 'field186060') + await klickTippSendFieldToUser(lastLoginEvents, 'field186060') const registeredEvents = await lastDateTimeEvents(EventType.USER_ACTIVATE_ACCOUNT) - void klickTippSendFieldToUser(registeredEvents, 'field186061') + await klickTippSendFieldToUser(registeredEvents, 'field186061') const receiveTransactionEvents = await lastDateTimeEvents(EventType.TRANSACTION_RECEIVE) - void klickTippSendFieldToUser(receiveTransactionEvents, 'field185674') + await klickTippSendFieldToUser(receiveTransactionEvents, 'field185674') const contributionCreateEvents = await lastDateTimeEvents(EventType.TRANSACTION_SEND) - void klickTippSendFieldToUser(contributionCreateEvents, 'field185673') + await klickTippSendFieldToUser(contributionCreateEvents, 'field185673') const linkRedeemedEvents = await lastDateTimeEvents(EventType.TRANSACTION_LINK_REDEEM) - void klickTippSendFieldToUser(linkRedeemedEvents, 'field185676') + await klickTippSendFieldToUser(linkRedeemedEvents, 'field185676') const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) - void klickTippSendFieldToUser(confirmContributionEvents, 'field185675') + await klickTippSendFieldToUser(confirmContributionEvents, 'field185675') + + await connectionInstance.close() } void exportEventDataToKlickTipp() // void retrieveNotRegisteredEmails() From 2ce14d1a89ca4337e6eddb00fa00272eab94e758 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 16 May 2023 16:38:59 +0200 Subject: [PATCH 16/22] refactor connection logic --- backend/src/server/createServer.ts | 4 ++-- backend/src/typeorm/connection.ts | 10 +++++++++- backend/src/util/klicktipp.ts | 6 +++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/src/server/createServer.ts b/backend/src/server/createServer.ts index d813c541e..ddc255268 100644 --- a/backend/src/server/createServer.ts +++ b/backend/src/server/createServer.ts @@ -8,7 +8,7 @@ import { Logger } from 'log4js' import { CONFIG } from '@/config' import { schema } from '@/graphql/schema' -import { connection } from '@/typeorm/connection' +import { getConnection } from '@/typeorm/connection' import { checkDBVersion } from '@/typeorm/DBVersion' import { elopageWebhook } from '@/webhook/elopage' @@ -37,7 +37,7 @@ export const createServer = async ( logger.debug('createServer...') // open mysql connection - const con = await connection() + const con = await getConnection() if (!con?.isConnected) { logger.fatal(`Couldn't open connection to database!`) throw new Error(`Fatal: Couldn't open connection to database`) diff --git a/backend/src/typeorm/connection.ts b/backend/src/typeorm/connection.ts index 7dec820b5..bd314fd3c 100644 --- a/backend/src/typeorm/connection.ts +++ b/backend/src/typeorm/connection.ts @@ -5,7 +5,15 @@ import { entities } from '@entity/index' import { CONFIG } from '@/config' -export const connection = async (): Promise => { +let connection: Connection | null + +export const getConnection = async (): Promise => { + if (connection) return connection + connection = await createMyConnection() + return connection +} + +const createMyConnection = async (): Promise => { try { return createConnection({ name: 'default', diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index f6c631205..a315d73e6 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -5,10 +5,10 @@ import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippControl import { EventType } from '@/event/EventType' import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' import { LogError } from '@/server/LogError' -import { connection } from '@/typeorm/connection' +import { getConnection } from '@/typeorm/connection' export async function retrieveNotRegisteredEmails(): Promise { - const con = await connection() + const con = await getConnection() if (!con) { throw new LogError('No connection to database') } @@ -40,7 +40,7 @@ async function klickTippSendFieldToUser( } export async function exportEventDataToKlickTipp(): Promise { - const connectionInstance = await connection() + const connectionInstance = await getConnection() if (!connectionInstance) { throw new LogError('No connection to database') } From b2a5f09df7d5d79a3f00f4a2a90a07399751fcde Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 16 May 2023 18:09:14 +0200 Subject: [PATCH 17/22] get things working, hurray --- backend/package.json | 2 +- backend/src/util/executeKlicktipp.ts | 16 ++++++++++++++++ backend/src/util/klicktipp.test.ts | 14 ++++++++++---- backend/src/util/klicktipp.ts | 23 ++++++----------------- 4 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 backend/src/util/executeKlicktipp.ts diff --git a/backend/package.json b/backend/package.json index 8a8d14e00..4e3c5887f 100644 --- a/backend/package.json +++ b/backend/package.json @@ -15,7 +15,7 @@ "lint": "eslint --max-warnings=0 .", "test": "cross-env TZ=UTC NODE_ENV=development jest --runInBand --forceExit --detectOpenHandles", "seed": "cross-env TZ=UTC NODE_ENV=development ts-node -r tsconfig-paths/register src/seeds/index.ts", - "klicktipp": "cross-env TZ=UTC NODE_ENV=development ts-node -r tsconfig-paths/register src/util/klicktipp.ts", + "klicktipp": "cross-env TZ=UTC NODE_ENV=development ts-node -r tsconfig-paths/register src/util/executeKlicktipp.ts", "locales": "scripts/sort.sh" }, "dependencies": { diff --git a/backend/src/util/executeKlicktipp.ts b/backend/src/util/executeKlicktipp.ts new file mode 100644 index 000000000..5a9699bf4 --- /dev/null +++ b/backend/src/util/executeKlicktipp.ts @@ -0,0 +1,16 @@ +import { getConnection } from '@/typeorm/connection' + +import { exportEventDataToKlickTipp } from './klicktipp' + +async function executeKlicktipp(): Promise { + const connection = await getConnection() + if (connection) { + await exportEventDataToKlickTipp() + await connection.close() + return true + } else { + return false + } +} + +void executeKlicktipp() diff --git a/backend/src/util/klicktipp.test.ts b/backend/src/util/klicktipp.test.ts index d6416a2f0..4f70f1196 100644 --- a/backend/src/util/klicktipp.test.ts +++ b/backend/src/util/klicktipp.test.ts @@ -4,6 +4,7 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { Connection } from '@dbTools/typeorm' import { Event as DbEvent } from '@entity/Event' +import { User as DbUser } from '@entity/User' import { ApolloServerTestClient } from 'apollo-server-testing' import { testEnvironment, cleanDB, resetToken } from '@test/helpers' @@ -15,10 +16,10 @@ import { userFactory } from '@/seeds/factory/user' import { login } from '@/seeds/graphql/mutations' import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' import { peterLustig } from '@/seeds/users/peter-lustig' -import { connection } from '@/typeorm/connection' import { exportEventDataToKlickTipp } from './klicktipp' +/* jest.mock('@/apis/KlicktippController', () => { const originalModule = jest.requireActual('@/apis/KlicktippController') return { @@ -28,6 +29,9 @@ jest.mock('@/apis/KlicktippController', () => { addFieldsToSubscriber: jest.fn((email, a) => originalModule.addFieldsToSubscriber(email, a)), } }) +*/ + +jest.mock('@/apis/KlicktippController') // jest.mock('@/typeorm/connection', () => { // const originalModule = jest.requireActual('@/typeorm/connection') @@ -71,8 +75,7 @@ describe('klicktipp', () => { mutation: login, variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, }) - await con.close() - void exportEventDataToKlickTipp() + // await con.close() }) afterAll(() => { @@ -80,7 +83,10 @@ describe('klicktipp', () => { }) describe('exportEventDataToKlickTipp', () => { - it('calls the KlicktippController', () => { + it('calls the KlicktippController', async () => { + // console.log(await lastDateTimeEvents('USER_LOGIN')) + // console.log(con) + await exportEventDataToKlickTipp() expect(addFieldsToSubscriber).toBeCalled() }) }) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 7070de411..2705b4f9e 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,6 +1,8 @@ // eslint-disable @typescript-eslint/no-explicit-any import { Connection } from '@dbTools/typeorm' +import { Event as DbEvent } from '@entity/Event' import { User } from '@entity/User' +import { UserContact } from '@entity/UserContact' import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' import { EventType } from '@/event/EventType' @@ -9,10 +11,6 @@ import { LogError } from '@/server/LogError' import { getConnection } from '@/typeorm/connection' export async function retrieveNotRegisteredEmails(): Promise { - const con = await getConnection() - if (!con) { - throw new LogError('No connection to database') - } const users = await User.find({ relations: ['emailContact'] }) const notRegisteredUser = [] for (const user of users) { @@ -24,7 +22,6 @@ export async function retrieveNotRegisteredEmails(): Promise { console.log(`${user.emailContact.email}`) } } - await con.close() // eslint-disable-next-line no-console console.log('User die nicht bei KlickTipp vorhanden sind: ', notRegisteredUser) return notRegisteredUser @@ -40,16 +37,7 @@ async function klickTippSendFieldToUser( } } -function getMyConnection(): Promise { - return connection() -} - -export async function exportEventDataToKlickTipp(): Promise { - const connectionInstance = await getConnection() - if (!connectionInstance) { - throw new LogError('No connection to database') - } - +export async function exportEventDataToKlickTipp(): Promise { const lastLoginEvents = await lastDateTimeEvents(EventType.USER_LOGIN) await klickTippSendFieldToUser(lastLoginEvents, 'field186060') @@ -68,7 +56,8 @@ export async function exportEventDataToKlickTipp(): Promise { const confirmContributionEvents = await lastDateTimeEvents(EventType.ADMIN_CONTRIBUTION_CONFIRM) await klickTippSendFieldToUser(confirmContributionEvents, 'field185675') - await connectionInstance.close() + return true } -void exportEventDataToKlickTipp() + +// void exportEventDataToKlickTipp() // void retrieveNotRegisteredEmails() From 76290e5c818cad06e6dd8d65383eaccc6958a295 Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 16 May 2023 18:53:50 +0200 Subject: [PATCH 18/22] Correct linting. --- backend/src/util/klicktipp.test.ts | 27 +-------------------------- backend/src/util/klicktipp.ts | 5 ----- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/backend/src/util/klicktipp.test.ts b/backend/src/util/klicktipp.test.ts index 4f70f1196..1751781b8 100644 --- a/backend/src/util/klicktipp.test.ts +++ b/backend/src/util/klicktipp.test.ts @@ -4,7 +4,6 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { Connection } from '@dbTools/typeorm' import { Event as DbEvent } from '@entity/Event' -import { User as DbUser } from '@entity/User' import { ApolloServerTestClient } from 'apollo-server-testing' import { testEnvironment, cleanDB, resetToken } from '@test/helpers' @@ -19,32 +18,9 @@ import { peterLustig } from '@/seeds/users/peter-lustig' import { exportEventDataToKlickTipp } from './klicktipp' -/* -jest.mock('@/apis/KlicktippController', () => { - const originalModule = jest.requireActual('@/apis/KlicktippController') - return { - __esModule: true, - ...originalModule, - getKlickTippUser: jest.fn((email) => originalModule.getKlickTippUser(email)), - addFieldsToSubscriber: jest.fn((email, a) => originalModule.addFieldsToSubscriber(email, a)), - } -}) -*/ - jest.mock('@/apis/KlicktippController') -// jest.mock('@/typeorm/connection', () => { -// const originalModule = jest.requireActual('@/typeorm/connection') -// return { -// __esModule: true, -// ...originalModule, -// connection: jest.fn(() => Promise.resolve(con)), -// } -// }) - -let mutate: ApolloServerTestClient['mutate'], - query: ApolloServerTestClient['query'], - con: Connection +let mutate: ApolloServerTestClient['mutate'], con: Connection let testEnv: { mutate: ApolloServerTestClient['mutate'] query: ApolloServerTestClient['query'] @@ -54,7 +30,6 @@ let testEnv: { beforeAll(async () => { testEnv = await testEnvironment() mutate = testEnv.mutate - query = testEnv.query con = testEnv.con await DbEvent.clear() }) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index 2705b4f9e..cb1955a86 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -1,14 +1,9 @@ // eslint-disable @typescript-eslint/no-explicit-any -import { Connection } from '@dbTools/typeorm' -import { Event as DbEvent } from '@entity/Event' import { User } from '@entity/User' -import { UserContact } from '@entity/UserContact' import { getKlickTippUser, addFieldsToSubscriber } from '@/apis/KlicktippController' import { EventType } from '@/event/EventType' import { lastDateTimeEvents } from '@/graphql/resolver/util/eventList' -import { LogError } from '@/server/LogError' -import { getConnection } from '@/typeorm/connection' export async function retrieveNotRegisteredEmails(): Promise { const users = await User.find({ relations: ['emailContact'] }) From 0d74f62478ffb00ba054b138d9c0dab9a5121fcc Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 19 May 2023 11:46:20 +0200 Subject: [PATCH 19/22] Add extension pug json and css to nodemon. --- backend/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/package.json b/backend/package.json index 8534b9434..dce2b5e6b 100644 --- a/backend/package.json +++ b/backend/package.json @@ -11,7 +11,7 @@ "build": "tsc --build && mkdir -p build/src/emails/templates/ && cp -r src/emails/templates/* build/src/emails/templates/ && mkdir -p build/src/locales/ && cp -r src/locales/*.json build/src/locales/", "clean": "tsc --build --clean", "start": "cross-env TZ=UTC TS_NODE_BASEURL=./build node -r tsconfig-paths/register build/src/index.js", - "dev": "cross-env TZ=UTC nodemon -w src --ext ts --exec ts-node -r tsconfig-paths/register src/index.ts", + "dev": "cross-env TZ=UTC nodemon -w src --ext ts,pug,json,css --exec ts-node -r tsconfig-paths/register src/index.ts", "lint": "eslint --max-warnings=0 .", "test": "cross-env TZ=UTC NODE_ENV=development jest --runInBand --forceExit --detectOpenHandles", "seed": "cross-env TZ=UTC NODE_ENV=development ts-node -r tsconfig-paths/register src/seeds/index.ts", From 81754449e927b5040c358c3aa3dd255e9874d6ac Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 19 May 2023 15:55:29 +0200 Subject: [PATCH 20/22] Removed commented code, implemented Singleton. --- backend/src/apis/KlicktippController.ts | 13 ++--- backend/src/server/createServer.ts | 8 +-- backend/src/typeorm/connection.ts | 76 +++++++++++++++---------- backend/src/util/executeKlicktipp.ts | 4 +- backend/src/util/klicktipp.test.ts | 3 - backend/src/util/klicktipp.ts | 7 +-- 6 files changed, 58 insertions(+), 53 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 57f85abf0..bc364c8ac 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-return */ @@ -43,10 +42,9 @@ export const getKlickTippUser = async (email: string): Promise => { const isLogin = await loginKlicktippUser() if (isLogin) { try { - const subscriberId = await klicktippConnector.subscriberSearch(email) - return klicktippConnector.subscriberGet(subscriberId) + return klicktippConnector.subscriberGet(await klicktippConnector.subscriberSearch(email)) } catch (e) { - logger.error(`Could not find subscriber ${email}`) + logger.error('Could not find subscriber', email) return false } } @@ -68,17 +66,16 @@ export const addFieldsToSubscriber = async ( const isLogin = await loginKlicktippUser() if (isLogin) { try { - const subscriberId = await klicktippConnector.subscriberSearch(email) + logger.info(`Update of subscriber (${email}) has been successful`) const result = await klicktippConnector.subscriberUpdate( - subscriberId, + await klicktippConnector.subscriberSearch(email), fields, newemail, newsmsnumber, ) - logger.info(`Update of subscriber (${email}) has been successful, ${result}`) return result } catch (e) { - logger.error(`Could not update subscriber ${email}, ${JSON.stringify(fields)}, ${e}`) + logger.error('Could not update subscriber', email, JSON.stringify(fields), e) return false } } diff --git a/backend/src/server/createServer.ts b/backend/src/server/createServer.ts index ddc255268..c162d9f6f 100644 --- a/backend/src/server/createServer.ts +++ b/backend/src/server/createServer.ts @@ -1,14 +1,14 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable @typescript-eslint/unbound-method */ -import { Connection } from '@dbTools/typeorm' +import { Connection as DbConnection } from '@dbTools/typeorm' import { ApolloServer } from 'apollo-server-express' import express, { Express, json, urlencoded } from 'express' import { Logger } from 'log4js' import { CONFIG } from '@/config' import { schema } from '@/graphql/schema' -import { getConnection } from '@/typeorm/connection' +import { Connection } from '@/typeorm/connection' import { checkDBVersion } from '@/typeorm/DBVersion' import { elopageWebhook } from '@/webhook/elopage' @@ -24,7 +24,7 @@ import { plugins } from './plugins' interface ServerDef { apollo: ApolloServer app: Express - con: Connection + con: DbConnection } export const createServer = async ( @@ -37,7 +37,7 @@ export const createServer = async ( logger.debug('createServer...') // open mysql connection - const con = await getConnection() + const con = await Connection.getInstance() if (!con?.isConnected) { logger.fatal(`Couldn't open connection to database!`) throw new Error(`Fatal: Couldn't open connection to database`) diff --git a/backend/src/typeorm/connection.ts b/backend/src/typeorm/connection.ts index bd314fd3c..3c8307478 100644 --- a/backend/src/typeorm/connection.ts +++ b/backend/src/typeorm/connection.ts @@ -1,41 +1,55 @@ // TODO This is super weird - since the entities are defined in another project they have their own globals. // We cannot use our connection here, but must use the external typeorm installation -import { Connection, createConnection, FileLogger } from '@dbTools/typeorm' +import { Connection as DbConnection, createConnection, FileLogger } from '@dbTools/typeorm' import { entities } from '@entity/index' import { CONFIG } from '@/config' -let connection: Connection | null +// eslint-disable-next-line @typescript-eslint/no-extraneous-class +export class Connection { + private static instance: DbConnection -export const getConnection = async (): Promise => { - if (connection) return connection - connection = await createMyConnection() - return connection -} + /** + * The Singleton's constructor should always be private to prevent direct + * construction calls with the `new` operator. + */ + // eslint-disable-next-line no-useless-constructor, @typescript-eslint/no-empty-function + private constructor() {} -const createMyConnection = async (): Promise => { - try { - return createConnection({ - name: 'default', - type: 'mysql', - host: CONFIG.DB_HOST, - port: CONFIG.DB_PORT, - username: CONFIG.DB_USER, - password: CONFIG.DB_PASSWORD, - database: CONFIG.DB_DATABASE, - entities, - synchronize: false, - logging: true, - logger: new FileLogger('all', { - logPath: CONFIG.TYPEORM_LOGGING_RELATIVE_PATH, - }), - extra: { - charset: 'utf8mb4_unicode_ci', - }, - }) - } catch (error) { - // eslint-disable-next-line no-console - console.log(error) - return null + /** + * The static method that controls the access to the singleton instance. + * + * This implementation let you subclass the Singleton class while keeping + * just one instance of each subclass around. + */ + public static async getInstance(): Promise { + if (Connection.instance) { + return Connection.instance + } + try { + Connection.instance = await createConnection({ + name: 'default', + type: 'mysql', + host: CONFIG.DB_HOST, + port: CONFIG.DB_PORT, + username: CONFIG.DB_USER, + password: CONFIG.DB_PASSWORD, + database: CONFIG.DB_DATABASE, + entities, + synchronize: false, + logging: true, + logger: new FileLogger('all', { + logPath: CONFIG.TYPEORM_LOGGING_RELATIVE_PATH, + }), + extra: { + charset: 'utf8mb4_unicode_ci', + }, + }) + return Connection.instance + } catch (error) { + // eslint-disable-next-line no-console + console.log(error) + return null + } } } diff --git a/backend/src/util/executeKlicktipp.ts b/backend/src/util/executeKlicktipp.ts index 5a9699bf4..74b453307 100644 --- a/backend/src/util/executeKlicktipp.ts +++ b/backend/src/util/executeKlicktipp.ts @@ -1,9 +1,9 @@ -import { getConnection } from '@/typeorm/connection' +import { Connection } from '@/typeorm/connection' import { exportEventDataToKlickTipp } from './klicktipp' async function executeKlicktipp(): Promise { - const connection = await getConnection() + const connection = await Connection.getInstance() if (connection) { await exportEventDataToKlickTipp() await connection.close() diff --git a/backend/src/util/klicktipp.test.ts b/backend/src/util/klicktipp.test.ts index 1751781b8..6639a0aa4 100644 --- a/backend/src/util/klicktipp.test.ts +++ b/backend/src/util/klicktipp.test.ts @@ -50,7 +50,6 @@ describe('klicktipp', () => { mutation: login, variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, }) - // await con.close() }) afterAll(() => { @@ -59,8 +58,6 @@ describe('klicktipp', () => { describe('exportEventDataToKlickTipp', () => { it('calls the KlicktippController', async () => { - // console.log(await lastDateTimeEvents('USER_LOGIN')) - // console.log(con) await exportEventDataToKlickTipp() expect(addFieldsToSubscriber).toBeCalled() }) diff --git a/backend/src/util/klicktipp.ts b/backend/src/util/klicktipp.ts index cb1955a86..c07b3128a 100644 --- a/backend/src/util/klicktipp.ts +++ b/backend/src/util/klicktipp.ts @@ -24,11 +24,11 @@ export async function retrieveNotRegisteredEmails(): Promise { async function klickTippSendFieldToUser( events: { email: string; value: Date }[], - value: string, + field: string, ): Promise { for (const event of events) { const time = event.value.setSeconds(0) - await addFieldsToSubscriber(event.email, { [value]: Math.trunc(time / 1000) }) + await addFieldsToSubscriber(event.email, { [field]: Math.trunc(time / 1000) }) } } @@ -53,6 +53,3 @@ export async function exportEventDataToKlickTipp(): Promise { return true } - -// void exportEventDataToKlickTipp() -// void retrieveNotRegisteredEmails() From 5de921f4f352d60f60f299458d1ce9cee3671ca4 Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 19 May 2023 16:06:23 +0200 Subject: [PATCH 21/22] Update logging. --- backend/src/apis/KlicktippController.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index bc364c8ac..15f29359b 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -66,14 +66,13 @@ export const addFieldsToSubscriber = async ( const isLogin = await loginKlicktippUser() if (isLogin) { try { - logger.info(`Update of subscriber (${email}) has been successful`) - const result = await klicktippConnector.subscriberUpdate( + logger.info('Updating of subscriber', email) + return klicktippConnector.subscriberUpdate( await klicktippConnector.subscriberSearch(email), fields, newemail, newsmsnumber, ) - return result } catch (e) { logger.error('Could not update subscriber', email, JSON.stringify(fields), e) return false From cfb0d7a61a74e609bf30a6ba5b2c8a6fe07fc281 Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 19 May 2023 16:09:17 +0200 Subject: [PATCH 22/22] Remove jsonstringify. --- backend/src/apis/KlicktippController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 15f29359b..ea6a8e47c 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -74,7 +74,7 @@ export const addFieldsToSubscriber = async ( newsmsnumber, ) } catch (e) { - logger.error('Could not update subscriber', email, JSON.stringify(fields), e) + logger.error('Could not update subscriber', email, fields, e) return false } }