From 81754449e927b5040c358c3aa3dd255e9874d6ac Mon Sep 17 00:00:00 2001 From: elweyn Date: Fri, 19 May 2023 15:55:29 +0200 Subject: [PATCH] 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()