mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Added a query checkEmail, that sends the data to the login_server, added a middleware call to the query that calls the KlicktippAPI after checkEmail.
This commit is contained in:
parent
3c17b7cd60
commit
7f91e5b937
@ -1,40 +1,53 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
import { KlicktippConnector } from './klicktippConnector'
|
||||
import CONFIG from '../config'
|
||||
|
||||
export class KlicktippController {
|
||||
private klicktippConnector: KlicktippConnector
|
||||
const klicktippConnector = new KlicktippConnector()
|
||||
|
||||
constructor(service?: string) {
|
||||
this.klicktippConnector = new KlicktippConnector(service)
|
||||
}
|
||||
export const signin = async (email: string, language: string): Promise<boolean> => {
|
||||
const fields = {}
|
||||
const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN
|
||||
const result = await klicktippConnector.signin(apiKey, email, fields)
|
||||
return result
|
||||
}
|
||||
|
||||
async signin(email: string, language: string): Promise<boolean> {
|
||||
const fields = {}
|
||||
const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN
|
||||
const result = await this.klicktippConnector.signin(apiKey, email, fields)
|
||||
export const signout = async (email: string, language: string): Promise<boolean> => {
|
||||
const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN
|
||||
const result = await klicktippConnector.signoff(apiKey, email)
|
||||
return result
|
||||
}
|
||||
|
||||
export const userTags = async (email: string): Promise<any> => {
|
||||
const isLogin = await loginKlicktippUser()
|
||||
if (isLogin) {
|
||||
const subscriberId = await klicktippConnector.subscriberSearch(email)
|
||||
const result = await klicktippConnector.subscriberGet(subscriberId)
|
||||
await logoutKlicktippUser()
|
||||
return result
|
||||
}
|
||||
|
||||
async signout(email: string, language: string): Promise<boolean> {
|
||||
const apiKey = language === 'de' ? CONFIG.KLICKTIPP_APIKEY_DE : CONFIG.KLICKTIPP_APIKEY_EN
|
||||
const result = await this.klicktippConnector.signoff(apiKey, email)
|
||||
return result
|
||||
}
|
||||
|
||||
async userTags(email: string): Promise<any> {
|
||||
await this.loginKlicktippUser()
|
||||
const subscriberId = await this.klicktippConnector.subscriberSearch(email)
|
||||
const result = await this.klicktippConnector.subscriberGet(subscriberId)
|
||||
console.log('The subscriber with the id: ', subscriberId, result)
|
||||
return result
|
||||
}
|
||||
|
||||
private async loginKlicktippUser() {
|
||||
return await this.klicktippConnector.login(CONFIG.KLICKTIPP_USER, CONFIG.KLICKTIPP_PASSWORD)
|
||||
}
|
||||
|
||||
async untagUser(email: string, tagid: string) {
|
||||
await this.loginKlicktippUser()
|
||||
return await this.klicktippConnector.untag(email, tagid)
|
||||
}
|
||||
}
|
||||
|
||||
export const loginKlicktippUser = async (): Promise<boolean> => {
|
||||
return await klicktippConnector.login(CONFIG.KLICKTIPP_USER, CONFIG.KLICKTIPP_PASSWORD)
|
||||
}
|
||||
|
||||
export const logoutKlicktippUser = async (): Promise<boolean> => {
|
||||
return await klicktippConnector.logout()
|
||||
}
|
||||
|
||||
export const untagUser = async (email: string, tagid: string): Promise<boolean> => {
|
||||
const isLogin = await loginKlicktippUser()
|
||||
if (isLogin) {
|
||||
return await klicktippConnector.untag(email, tagid)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export const tagUser = async (email: string, tagids: string): Promise<boolean> => {
|
||||
const isLogin = await loginKlicktippUser()
|
||||
if (isLogin) {
|
||||
return await klicktippConnector.tag(email, tagids)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
import axios, { AxiosRequestConfig, Method } from 'axios'
|
||||
|
||||
export class KlicktippConnector {
|
||||
|
||||
21
backend/src/graphql/models/CheckEmailResponse.ts
Normal file
21
backend/src/graphql/models/CheckEmailResponse.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
import { ObjectType, Field } from 'type-graphql'
|
||||
|
||||
@ObjectType()
|
||||
export class CheckEmailResponse {
|
||||
constructor(json: any) {
|
||||
this.sessionId = json.session_id
|
||||
this.email = json.user.email
|
||||
this.language = json.user.language
|
||||
}
|
||||
|
||||
@Field(() => Number)
|
||||
sessionId: number
|
||||
|
||||
@Field(() => String)
|
||||
email: string
|
||||
|
||||
@Field(() => String)
|
||||
language: string
|
||||
}
|
||||
@ -16,14 +16,12 @@ import {
|
||||
UpdateUserInfosArgs,
|
||||
} from '../inputs/LoginUserInput'
|
||||
import { apiPost, apiGet } from '../../apis/HttpRequest'
|
||||
import { KlicktippController } from '../../apis/KlicktippController'
|
||||
import { registerMiddleware } from '../../middleware/registerMiddleware'
|
||||
import { klicktippRegistrationMiddleware } from '../../middleware/klicktippMiddleware'
|
||||
import encode from '../../jwt/encode'
|
||||
import { CheckEmailResponse } from '../models/CheckEmailResponse'
|
||||
|
||||
@Resolver()
|
||||
export class UserResolver {
|
||||
private connector: KlicktippController = new KlicktippController(CONFIG.KLICKTTIPP_API_URL)
|
||||
|
||||
@Query(() => String)
|
||||
async login(@Args() { email, password }: UnsecureLoginArgs): Promise<string> {
|
||||
email = email.trim().toLowerCase()
|
||||
@ -67,7 +65,6 @@ export class UserResolver {
|
||||
}
|
||||
|
||||
@Query(() => String)
|
||||
@UseMiddleware(registerMiddleware)
|
||||
async create(
|
||||
@Args() { email, firstName, lastName, password, language }: CreateUserArgs,
|
||||
): Promise<string> {
|
||||
@ -165,4 +162,14 @@ export class UserResolver {
|
||||
if (!response.success) throw new Error(response.data)
|
||||
return new CheckUsernameResponse(response.data)
|
||||
}
|
||||
|
||||
@Query(() => CheckEmailResponse)
|
||||
@UseMiddleware(klicktippRegistrationMiddleware)
|
||||
async checkEmail(@Arg('optin') optin: string): Promise<CheckEmailResponse> {
|
||||
const result = await apiGet(CONFIG.LOGIN_API_URL + 'checkEmail/' + optin)
|
||||
if (!result.success) {
|
||||
throw new Error(result.data)
|
||||
}
|
||||
return new CheckEmailResponse(result.data)
|
||||
}
|
||||
}
|
||||
|
||||
15
backend/src/middleware/klicktippMiddleware.ts
Normal file
15
backend/src/middleware/klicktippMiddleware.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { MiddlewareFn } from 'type-graphql'
|
||||
import { signin } from '../apis/KlicktippController'
|
||||
|
||||
export const klicktippRegistrationMiddleware: MiddlewareFn = async (
|
||||
// Only for demo
|
||||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
||||
{ root, args, context, info },
|
||||
next,
|
||||
) => {
|
||||
// Do Something here before resolver is called
|
||||
const result = await next()
|
||||
// Do Something here after resolver is completed
|
||||
signin(result.data.checkEmail.email, result.data.checkEmail.language)
|
||||
return result
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
import { MiddlewareFn } from 'type-graphql'
|
||||
|
||||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
||||
export const registerMiddleware: MiddlewareFn = async ({ root, args, context, info }, next) => {
|
||||
const result = await next()
|
||||
// do something here
|
||||
return result
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user