add gms publishing in user creation

This commit is contained in:
Claus-Peter Huebner 2023-12-01 00:36:25 +01:00
parent 7296fae503
commit 7971324d99
9 changed files with 102 additions and 41 deletions

View File

@ -65,5 +65,7 @@ WEBHOOK_ELOPAGE_SECRET=secret
FEDERATION_VALIDATE_COMMUNITY_TIMER=60000
# GMS
# GMS_ACTIVE=true
# Coordinates of Illuminz test instance
GMS_HOST=54.176.169.179
GMS_PORT=3071

View File

@ -63,5 +63,6 @@ WEBHOOK_ELOPAGE_SECRET=$WEBHOOK_ELOPAGE_SECRET
FEDERATION_VALIDATE_COMMUNITY_TIMER=$FEDERATION_VALIDATE_COMMUNITY_TIMER
# GMS
GMS_ACTIVE=$GMS_ACTIVE
GMS_HOST=$GMS_HOST
GMS_PORT=$GMS_PORT

View File

@ -8,11 +8,11 @@ import { CONFIG } from '@/config'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger'
import { GmsCommunity } from './model/GmsCommunity'
import { GmsUser } from './model/GmsUser'
/*
export async function communityList(): Promise<GmsCommunity[] | string | undefined> {
const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const service = 'community/list?page=1&perPage=20'
const config = {
headers: {
@ -32,10 +32,10 @@ export async function communityList(): Promise<GmsCommunity[] | string | undefin
}
logger.debug('responseData:', result.data.responseData.data)
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const gmsCom = JSON.parse(result.data.responseData.data)
logger.debug('gmsCom:', gmsCom)
// const gmsCom = JSON.parse(result.data.responseData.data)
// logger.debug('gmsCom:', gmsCom)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return gmsCom
return result.data.responseData.data
} catch (error: any) {
logger.error('Error in Get community/list:', error)
const errMsg: string = error.message
@ -44,7 +44,7 @@ export async function communityList(): Promise<GmsCommunity[] | string | undefin
}
export async function userList(): Promise<GmsUser[] | string | undefined> {
const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const service = 'community-user/list?page=1&perPage=20'
const config = {
headers: {
@ -68,10 +68,10 @@ export async function userList(): Promise<GmsUser[] | string | undefined> {
}
logger.debug('responseData:', result.data.responseData.data)
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const gmsUser = JSON.parse(result.data.responseData.data)
logger.debug('gmsUser:', gmsUser)
// const gmsUser = JSON.parse(result.data.responseData.data)
// logger.debug('gmsUser:', gmsUser)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return gmsUser
return result.data.responseData.data
} catch (error: any) {
logger.error('Error in Get community-user/list:', error)
const errMsg: string = error.message
@ -80,7 +80,7 @@ export async function userList(): Promise<GmsUser[] | string | undefined> {
}
export async function userByUuid(uuid: string): Promise<GmsUser[] | string | undefined> {
const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const service = 'community-user/list?page=1&perPage=20'
const config = {
headers: {
@ -104,19 +104,20 @@ export async function userByUuid(uuid: string): Promise<GmsUser[] | string | und
}
logger.debug('responseData:', result.data.responseData.data)
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const gmsUser = JSON.parse(result.data.responseData.data)
logger.debug('gmsUser:', gmsUser)
// const gmsUser = JSON.parse(result.data.responseData.data)
// logger.debug('gmsUser:', gmsUser)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return gmsUser
return result.data.responseData.data
} catch (error: any) {
logger.error('Error in Get community-user/list:', error)
const errMsg: string = error.message
return errMsg
}
}
*/
export async function createGmsUser(apiKey: string, user: GmsUser): Promise<boolean> {
const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/')
const service = 'community-user'
const config = {
headers: {
@ -125,7 +126,6 @@ export async function createGmsUser(apiKey: string, user: GmsUser): Promise<bool
timezone: 'UTC',
connection: 'keep-alive',
authorization: apiKey,
// 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiVTJGc2RHVmtYMThuNzllbGJscThDbmxxZ0I2SGxicTZuajlpM2lmV3BTc3pHZFRtOFVTQjJZNWY2bG56elhuSUF0SEwvYVBWdE1uMjA3bnNtWDQ0M21xWVFyd0xJMklHNGtpRkZ3U2FKbVJwRk9VZXNDMXIyRGlta3VLMklwN1lYRTU0c2MzVmlScmMzaHE3djlFNkRabk4xeVMrU1QwRWVZRFI5c09pTDJCdmg4a05DNUc5NTdoZUJzeWlRbXcrNFFmMXFuUk5SNXpWdXhtZEE2WUUrT3hlcS85Y0d6NURyTmhoaHM3MTJZTFcvTmprZGNwdU55dUgxeWxhNEhJZyIsImlhdCI6MTcwMDUxMDg4OX0.WhtNGZc9A_hUfh8CcPjr44kWQWMkKJ7hlYXELOd3yy4',
},
}
try {

View File

@ -35,3 +35,15 @@ registerEnumType(GmsPublishPostType, {
name: 'GmsPublishPostType', // this one is mandatory
description: 'Type of name publishing', // this one is optional
})
export enum GmsLocationType {
GMS_LOCATION_TYPE_EXACT = 0,
GMS_LOCATION_TYPE_APPROXIMATE = 1,
GMS_LOCATION_TYPE_RANDOM = 2,
}
registerEnumType(GmsLocationType, {
name: 'GmsLocationType', // this one is mandatory
description: 'Type of location treatment in GMS', // this one is optional
})

View File

@ -1,6 +1,11 @@
import { User as dbUser } from '@entity/User'
import { GmsPublishNameType, GmsPublishPhoneType, GmsPublishPostType } from './GmsEnums'
import {
GmsLocationType,
GmsPublishNameType,
GmsPublishPhoneType,
GmsPublishPostType,
} from './GmsEnums'
export class GmsUser {
constructor(user: dbUser) {
@ -12,11 +17,11 @@ export class GmsUser {
this.firstName = this.getGmsFirstName(user)
this.lastName = this.getGmsLastName(user)
this.alias = user.alias ? user.alias : undefined
this.address = this.getGmsAddress(user) ?? 'Deutschland'
this.zipCode = this.getGmsZipCode(user)
this.city = this.getGmsCity(user)
this.country = this.getGmsCountry(user)
this.type = 2
this.address = this.getGmsAddress(user)
// this.zipCode = this.getGmsZipCode(user)
// this.city = this.getGmsCity(user)
// this.country = this.getGmsCountry(user)
this.type = GmsLocationType.GMS_LOCATION_TYPE_RANDOM
this.location = null
}
@ -45,15 +50,12 @@ export class GmsUser {
if (
user.gmsAllowed &&
(user.gmsPublishName === GmsPublishNameType.GMS_PUBLISH_NAME_FIRST ||
user.gmsPublishName === GmsPublishNameType.GMS_PUBLISH_NAME_FIRST_INITIAL ||
user.gmsPublishName === GmsPublishNameType.GMS_PUBLISH_NAME_FULL)
) {
return user.firstName
}
if (
user.gmsAllowed &&
(user.gmsPublishName === GmsPublishNameType.GMS_PUBLISH_NAME_FIRST_INITIAL ||
user.gmsPublishName === GmsPublishNameType.GMS_PUBLISH_NAME_INITIALS)
) {
if (user.gmsAllowed && user.gmsPublishName === GmsPublishNameType.GMS_PUBLISH_NAME_INITIALS) {
return user.firstName.substring(0, 1) + '.'
}
}
@ -72,7 +74,7 @@ export class GmsUser {
}
private getGmsEmail(user: dbUser): string | undefined {
if (user.gmsAllowed && user.emailContact.gmsPublishEmail === true) {
if (user.gmsAllowed && user.emailContact.gmsPublishEmail) {
return user.emailContact.email
}
}
@ -97,11 +99,19 @@ export class GmsUser {
}
private getGmsAddress(user: dbUser): string | undefined {
if (
user.gmsAllowed &&
user.emailContact.gmsPublishPost === GmsPublishPostType.GMS_PUBLISH_POST_FULL
) {
return user.emailContact.address
if (user.gmsAllowed) {
if (user.emailContact.gmsPublishPost === GmsPublishPostType.GMS_PUBLISH_POST_FULL) {
const address = [
'' + user.emailContact.country,
...(user.emailContact.zipCode ? user.emailContact.zipCode : ' '),
user.emailContact.city ? user.emailContact.city : ' ',
user.emailContact.address ? user.emailContact.address : ' ',
]
.map((a) => a.trim())
.filter(Boolean)
.join(', ')
return address
}
}
}

View File

@ -135,6 +135,7 @@ const federation = {
}
const gms = {
GMS_ACTIVE: process.env.GMS_ACTIVE === 'true' || false,
// koordinates of Illuminz-instance of GMS
GMS_HOST: process.env.GMS_HOST ?? 'localhost',
GMS_PORT: process.env.GMS_PORT ?? '4044',

View File

@ -71,6 +71,7 @@ import { findUserByIdentifier } from './util/findUserByIdentifier'
import { findUsers } from './util/findUsers'
import { getKlicktippState } from './util/getKlicktippState'
import { setUserRole, deleteUserRole } from './util/modifyUserRole'
import { sendUserToGms } from './util/sendUserToGms'
import { validateAlias } from './util/validateAlias'
const LANGUAGES = ['de', 'en', 'es', 'fr', 'nl']
@ -361,6 +362,18 @@ export class UserResolver {
} else {
await EVENT_USER_REGISTER(dbUser)
}
if (CONFIG.GMS_ACTIVE) {
logger.info('GMS deactivated per configuration! New user is not published to GMS.')
} else {
try {
if (dbUser.gmsAllowed && !dbUser.gmsRegistered) {
await sendUserToGms(dbUser, homeCom)
}
} catch (err) {
logger.error('Error publishing new created user to GMS:', err)
}
}
return new User(dbUser)
}

View File

@ -0,0 +1,27 @@
import { Community as DbCommunity } from '@entity/Community'
import { User as DbUser } from '@entity/User'
import { createGmsUser } from '@/apis/gms/GmsClient'
import { GmsUser } from '@/apis/gms/model/GmsUser'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger'
export async function sendUserToGms(user: DbUser, homeCom: DbCommunity): Promise<void> {
if (homeCom.gmsApiKey === null) {
throw new LogError('HomeCommunity needs GMS-ApiKey to publish user data to GMS.')
}
logger.debug('User send to GMS:', user)
const gmsUser = new GmsUser(user)
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
if (await createGmsUser(homeCom.gmsApiKey, gmsUser)) {
logger.debug('GMS user published successfully:', gmsUser)
user.gmsRegistered = true
user.gmsRegisteredAt = new Date()
await DbUser.save(user)
logger.debug('mark user as gms published:', user)
}
} catch (err) {
logger.warn('publishing user fails with ', err)
}
}

View File

@ -5,15 +5,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
import { entities } from '@entity/index'
import { User as DbUser } from '@entity/User'
import { createTestClient } from 'apollo-server-testing'
import { communityList, createGmsUser, userList } from '@/apis/gms/GmsClient'
import { GmsUser } from '@/apis/gms/model/GmsUser'
import { CONFIG } from '@/config'
import { getHomeCommunity } from '@/graphql/resolver/util/communities'
import { createServer } from '@/server/createServer'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger'
CONFIG.EMAIL = false
@ -52,10 +47,10 @@ const run = async () => {
// test GMS-Api Client
try {
const gmsComArray = await communityList()
logger.debug('GMS-Community-List:', gmsComArray)
const gmsUserArray = await userList()
logger.debug('GMS-Community-User-List:', gmsUserArray)
// const gmsComArray = await communityList()
// logger.debug('GMS-Community-List:', gmsComArray)
// const gmsUserArray = await userList()
// logger.debug('GMS-Community-User-List:', gmsUserArray)
} catch (err) {
logger.error('Error in GMS-API:', err)
}