From 51d572f0b795efa54f771a6d19db8675a1efa5cd Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 3 Apr 2023 14:58:26 +0200 Subject: [PATCH] Implement callKlickTippAPI --- backend/src/apis/KlicktippController.ts | 178 ++++++++++++++---------- 1 file changed, 103 insertions(+), 75 deletions(-) diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 1278b0d9c..d414bb9ec 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -4,107 +4,135 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/restrict-template-expressions */ import CONFIG from '@/config' +import LogError from '@/server/LogError' // eslint-disable-next-line import/no-relative-parent-imports import KlicktippConnector from 'klicktipp-api' const klicktippConnector = new KlicktippConnector() -export const klicktippSignIn = async ( +const callKlickTippAPI = (callback: (arg0: any) => any, args: any) => { + if (!CONFIG.KLICKTIPP) { + return true + } + return callback(args) +} + +export const klicktippSignIn = ( email: string, language: string, firstName?: string, lastName?: string, ): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - const fields = { - fieldFirstName: firstName, - fieldLastName: lastName, - } - const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN - const result = await klicktippConnector.signin(apiKey, email, fields) - return result + return callKlickTippAPI( + ({ fieldFirstName, fieldLastName, language, email }) => { + const fields = { + fieldFirstName, + fieldLastName, + } + const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN + return klicktippConnector.signin(apiKey, email, fields) + }, + { + fieldFirstName: firstName, + fieldLastName: lastName, + language, + email, + }, + ) } -export const signout = async (email: string, language: string): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN - const result = await klicktippConnector.signoff(apiKey, email) - return result +export const signout = (email: string, language: string): Promise => { + return callKlickTippAPI( + async ({ language, email }) => { + const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN + const result = await klicktippConnector.signoff(apiKey, email) + return result + }, + { + email, + language, + }, + ) } -export const unsubscribe = async (email: string): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - const isLogin = await loginKlicktippUser() - if (isLogin) { - return await klicktippConnector.unsubscribe(email) - } - throw new Error(`Could not unsubscribe ${email}`) +export const unsubscribe = (email: string): Promise => { + return callKlickTippAPI( + async ({ email }) => { + const isLogin = await loginKlicktippUser() + if (isLogin) { + return klicktippConnector.unsubscribe(email) + } + throw new LogError(`Could not unsubscribe ${email}`) + }, + { + email, + }, + ) } -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 - } - return false +export const getKlickTippUser = (email: string): Promise => { + return callKlickTippAPI( + async ({ email }) => { + const isLogin = await loginKlicktippUser() + if (isLogin) { + const subscriberId = await klicktippConnector.subscriberSearch(email) + return klicktippConnector.subscriberGet(subscriberId) + } + throw new LogError(`Could not get subscriber ${email}`) + }, + { + email, + }, + ) } -export const loginKlicktippUser = async (): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - return await klicktippConnector.login(CONFIG.KLICKTIPP_USER, CONFIG.KLICKTIPP_PASSWORD) +export const loginKlicktippUser = (): Promise => { + return callKlickTippAPI(() => { + return klicktippConnector.login(CONFIG.KLICKTIPP_USER, CONFIG.KLICKTIPP_PASSWORD) + }, {}) } -export const logoutKlicktippUser = async (): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - return await klicktippConnector.logout() +export const logoutKlicktippUser = (): Promise => { + return callKlickTippAPI(() => { + return klicktippConnector.logout() + }, {}) } -export const untagUser = async (email: string, tagId: string): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - const isLogin = await loginKlicktippUser() - if (isLogin) { - return await klicktippConnector.untag(email, tagId) - } - return false +export const untagUser = (email: string, tagId: string): Promise => { + return callKlickTippAPI( + async ({ email, tagId }) => { + const isLogin = await loginKlicktippUser() + if (isLogin) { + return await klicktippConnector.untag(email, tagId) + } + throw new LogError(`Could not untag ${email}`) + }, + { email, tagId }, + ) } -export const tagUser = async (email: string, tagIds: string): Promise => { - if (!CONFIG.KLICKTIPP) { - return true - } - const isLogin = await loginKlicktippUser() - if (isLogin) { - return await klicktippConnector.tag(email, tagIds) - } - return false +export const tagUser = (email: string, tagIds: string): Promise => { + return callKlickTippAPI( + async ({ email, tagIds }) => { + const isLogin = await loginKlicktippUser() + if (isLogin) { + return klicktippConnector.tag(email, tagIds) + } + throw new LogError(`Could not tag ${email}`) + }, + { email, tagIds }, + ) } -export const getKlicktippTagMap = async () => { - if (!CONFIG.KLICKTIPP) { - return '' - } - const isLogin = await loginKlicktippUser() - if (isLogin) { - return await klicktippConnector.tagIndex() - } - return '' +export const getKlicktippTagMap = (): Promise => { + return callKlickTippAPI(async () => { + const isLogin = await loginKlicktippUser() + if (isLogin) { + return klicktippConnector.tagIndex() + } + throw new LogError(`Could not get tagIndexes`) + }, {}) }