diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 3f7136de2..ebc3d6cef 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -18,6 +18,7 @@ export const klicktippSignIn = async ( firstName?: string, lastName?: string, ): Promise => { + if (!CONFIG.KLICKTIPP) return true const fields = { fieldFirstName: firstName, fieldLastName: lastName, @@ -28,12 +29,14 @@ export const klicktippSignIn = async ( } 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 unsubscribe = async (email: string): Promise => { + if (!CONFIG.KLICKTIPP) return true const isLogin = await loginKlicktippUser() if (isLogin) { return await klicktippConnector.unsubscribe(email) @@ -42,6 +45,7 @@ export const unsubscribe = async (email: string): Promise => { } export const getKlickTippUser = async (email: string): Promise => { + if (!CONFIG.KLICKTIPP) return true const isLogin = await loginKlicktippUser() if (isLogin) { const subscriberId = await klicktippConnector.subscriberSearch(email) @@ -52,14 +56,17 @@ export const getKlickTippUser = async (email: string): Promise => { } export const loginKlicktippUser = async (): Promise => { + if (!CONFIG.KLICKTIPP) return true return await klicktippConnector.login(CONFIG.KLICKTIPP_USER, CONFIG.KLICKTIPP_PASSWORD) } export const logoutKlicktippUser = async (): Promise => { + if (!CONFIG.KLICKTIPP) return true return await 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) @@ -68,6 +75,7 @@ export const untagUser = async (email: string, tagId: string): Promise } 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) @@ -76,6 +84,7 @@ export const tagUser = async (email: string, tagIds: string): Promise = } export const getKlicktippTagMap = async () => { + if (!CONFIG.KLICKTIPP) return true const isLogin = await loginKlicktippUser() if (isLogin) { return await klicktippConnector.tagIndex() diff --git a/backend/src/auth/RIGHTS.ts b/backend/src/auth/RIGHTS.ts index bcdb2f7ec..df4eed8a1 100644 --- a/backend/src/auth/RIGHTS.ts +++ b/backend/src/auth/RIGHTS.ts @@ -5,8 +5,6 @@ export enum RIGHTS { COMMUNITIES = 'COMMUNITIES', LIST_GDT_ENTRIES = 'LIST_GDT_ENTRIES', EXIST_PID = 'EXIST_PID', - GET_KLICKTIPP_USER = 'GET_KLICKTIPP_USER', - GET_KLICKTIPP_TAG_MAP = 'GET_KLICKTIPP_TAG_MAP', UNSUBSCRIBE_NEWSLETTER = 'UNSUBSCRIBE_NEWSLETTER', SUBSCRIBE_NEWSLETTER = 'SUBSCRIBE_NEWSLETTER', TRANSACTION_LIST = 'TRANSACTION_LIST', diff --git a/backend/src/auth/ROLES.ts b/backend/src/auth/ROLES.ts index df1ee0271..bc868a199 100644 --- a/backend/src/auth/ROLES.ts +++ b/backend/src/auth/ROLES.ts @@ -9,8 +9,6 @@ export const ROLE_USER = new Role('user', [ RIGHTS.BALANCE, RIGHTS.LIST_GDT_ENTRIES, RIGHTS.EXIST_PID, - RIGHTS.GET_KLICKTIPP_USER, - RIGHTS.GET_KLICKTIPP_TAG_MAP, RIGHTS.UNSUBSCRIBE_NEWSLETTER, RIGHTS.SUBSCRIBE_NEWSLETTER, RIGHTS.TRANSACTION_LIST, diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 490462d4e..9417aa5e2 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0065-x-community-sendcoins-transactions_table', + DB_VERSION: '0066-x-community-sendcoins-transactions_table', DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/backend/src/event/EVENT_NEWSLETTER_SUBSCRIBE.ts b/backend/src/event/EVENT_NEWSLETTER_SUBSCRIBE.ts new file mode 100644 index 000000000..717bb8296 --- /dev/null +++ b/backend/src/event/EVENT_NEWSLETTER_SUBSCRIBE.ts @@ -0,0 +1,8 @@ +import { Event as DbEvent } from '@entity/Event' +import { User as DbUser } from '@entity/User' + +import { Event } from './Event' +import { EventType } from './EventType' + +export const EVENT_NEWSLETTER_SUBSCRIBE = async (user: DbUser): Promise => + Event(EventType.NEWSLETTER_SUBSCRIBE, user, user).save() diff --git a/backend/src/event/EVENT_NEWSLETTER_UNSUBSCRIBE.ts b/backend/src/event/EVENT_NEWSLETTER_UNSUBSCRIBE.ts new file mode 100644 index 000000000..f8adc69d1 --- /dev/null +++ b/backend/src/event/EVENT_NEWSLETTER_UNSUBSCRIBE.ts @@ -0,0 +1,8 @@ +import { Event as DbEvent } from '@entity/Event' +import { User as DbUser } from '@entity/User' + +import { Event } from './Event' +import { EventType } from './EventType' + +export const EVENT_NEWSLETTER_UNSUBSCRIBE = async (user: DbUser): Promise => + Event(EventType.NEWSLETTER_UNSUBSCRIBE, user, user).save() diff --git a/backend/src/event/EventType.ts b/backend/src/event/EventType.ts index 959a848f5..b2b6f9322 100644 --- a/backend/src/event/EventType.ts +++ b/backend/src/event/EventType.ts @@ -21,6 +21,8 @@ export enum EventType { EMAIL_ADMIN_CONFIRMATION = 'EMAIL_ADMIN_CONFIRMATION', EMAIL_CONFIRMATION = 'EMAIL_CONFIRMATION', EMAIL_FORGOT_PASSWORD = 'EMAIL_FORGOT_PASSWORD', + NEWSLETTER_SUBSCRIBE = 'NEWSLETTER_SUBSCRIBE', + NEWSLETTER_UNSUBSCRIBE = 'NEWSLETTER_UNSUBSCRIBE', TRANSACTION_SEND = 'TRANSACTION_SEND', TRANSACTION_RECEIVE = 'TRANSACTION_RECEIVE', TRANSACTION_LINK_CREATE = 'TRANSACTION_LINK_CREATE', diff --git a/backend/src/event/Events.ts b/backend/src/event/Events.ts index d217cde28..4f10afbce 100644 --- a/backend/src/event/Events.ts +++ b/backend/src/event/Events.ts @@ -21,6 +21,8 @@ export { EVENT_EMAIL_ACCOUNT_MULTIREGISTRATION } from './EVENT_EMAIL_ACCOUNT_MUL export { EVENT_EMAIL_ADMIN_CONFIRMATION } from './EVENT_EMAIL_ADMIN_CONFIRMATION' export { EVENT_EMAIL_CONFIRMATION } from './EVENT_EMAIL_CONFIRMATION' export { EVENT_EMAIL_FORGOT_PASSWORD } from './EVENT_EMAIL_FORGOT_PASSWORD' +export { EVENT_NEWSLETTER_SUBSCRIBE } from './EVENT_NEWSLETTER_SUBSCRIBE' +export { EVENT_NEWSLETTER_UNSUBSCRIBE } from './EVENT_NEWSLETTER_UNSUBSCRIBE' export { EVENT_TRANSACTION_SEND } from './EVENT_TRANSACTION_SEND' export { EVENT_TRANSACTION_RECEIVE } from './EVENT_TRANSACTION_RECEIVE' export { EVENT_TRANSACTION_LINK_CREATE } from './EVENT_TRANSACTION_LINK_CREATE' diff --git a/backend/src/federation/client/1_0/FederationClient.ts b/backend/src/federation/client/1_0/FederationClient.ts index 13f05e761..743d17348 100644 --- a/backend/src/federation/client/1_0/FederationClient.ts +++ b/backend/src/federation/client/1_0/FederationClient.ts @@ -1,14 +1,16 @@ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { gql } from 'graphql-request' import { GraphQLGetClient } from '@/federation/client/GraphQLGetClient' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' -export async function requestGetPublicKey(dbCom: DbCommunity): Promise { +export async function requestGetPublicKey( + dbCom: DbFederatedCommunity, +): Promise { let endpoint = dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/' endpoint = `${endpoint}${dbCom.apiVersion}/` logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`) diff --git a/backend/src/federation/client/1_1/FederationClient.ts b/backend/src/federation/client/1_1/FederationClient.ts index bda185fba..35c88bf3b 100644 --- a/backend/src/federation/client/1_1/FederationClient.ts +++ b/backend/src/federation/client/1_1/FederationClient.ts @@ -1,14 +1,16 @@ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { gql } from 'graphql-request' import { GraphQLGetClient } from '@/federation/client/GraphQLGetClient' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' -export async function requestGetPublicKey(dbCom: DbCommunity): Promise { +export async function requestGetPublicKey( + dbCom: DbFederatedCommunity, +): Promise { let endpoint = dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/' endpoint = `${endpoint}${dbCom.apiVersion}/` logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`) diff --git a/backend/src/federation/validateCommunities.test.ts b/backend/src/federation/validateCommunities.test.ts index ab426b603..ed4897e09 100644 --- a/backend/src/federation/validateCommunities.test.ts +++ b/backend/src/federation/validateCommunities.test.ts @@ -1,5 +1,12 @@ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/unbound-method */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ +/* eslint-disable @typescript-eslint/no-unsafe-call */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ + import { Connection } from '@dbTools/typeorm' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { ApolloServerTestClient } from 'apollo-server-testing' import { testEnvironment, cleanDB } from '@test/helpers' @@ -58,9 +65,9 @@ describe('validate Communities', () => { endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), } - await DbCommunity.createQueryBuilder() + await DbFederatedCommunity.createQueryBuilder() .insert() - .into(DbCommunity) + .into(DbFederatedCommunity) .values(variables1) .orUpdate({ conflict_target: ['id', 'publicKey', 'apiVersion'], @@ -89,9 +96,9 @@ describe('validate Communities', () => { endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), } - await DbCommunity.createQueryBuilder() + await DbFederatedCommunity.createQueryBuilder() .insert() - .into(DbCommunity) + .into(DbFederatedCommunity) .values(variables2) .orUpdate({ conflict_target: ['id', 'publicKey', 'apiVersion'], @@ -117,7 +124,7 @@ describe('validate Communities', () => { }) }) describe('with three Communities of api 1_0, 1_1 and 2_0', () => { - let dbCom: DbCommunity + let dbCom: DbFederatedCommunity beforeEach(async () => { const variables3 = { publicKey: Buffer.from('11111111111111111111111111111111'), @@ -125,16 +132,16 @@ describe('validate Communities', () => { endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), } - await DbCommunity.createQueryBuilder() + await DbFederatedCommunity.createQueryBuilder() .insert() - .into(DbCommunity) + .into(DbFederatedCommunity) .values(variables3) .orUpdate({ conflict_target: ['id', 'publicKey', 'apiVersion'], overwrite: ['end_point', 'last_announced_at'], }) .execute() - dbCom = await DbCommunity.findOneOrFail({ + dbCom = await DbFederatedCommunity.findOneOrFail({ where: { publicKey: variables3.publicKey, apiVersion: variables3.apiVersion }, }) jest.clearAllMocks() diff --git a/backend/src/federation/validateCommunities.ts b/backend/src/federation/validateCommunities.ts index 0e8c7cb12..b38f38ee9 100644 --- a/backend/src/federation/validateCommunities.ts +++ b/backend/src/federation/validateCommunities.ts @@ -1,5 +1,7 @@ +/** eslint-disable @typescript-eslint/no-unsafe-call */ +/** eslint-disable @typescript-eslint/no-unsafe-assignment */ import { IsNull } from '@dbTools/typeorm' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' @@ -23,13 +25,14 @@ export function startValidateCommunities(timerInterval: number): void { } export async function validateCommunities(): Promise { - const dbCommunities: DbCommunity[] = await DbCommunity.createQueryBuilder() - .where({ foreign: true, verifiedAt: IsNull() }) - .orWhere('verified_at < last_announced_at') - .getMany() + const dbFederatedCommunities: DbFederatedCommunity[] = + await DbFederatedCommunity.createQueryBuilder() + .where({ foreign: true, verifiedAt: IsNull() }) + .orWhere('verified_at < last_announced_at') + .getMany() - logger.debug(`Federation: found ${dbCommunities.length} dbCommunities`) - for (const dbCom of dbCommunities) { + logger.debug(`Federation: found ${dbFederatedCommunities.length} dbCommunities`) + for (const dbCom of dbFederatedCommunities) { logger.debug('Federation: dbCom', dbCom) const apiValueStrings: string[] = Object.values(ApiVersionType) logger.debug(`suppported ApiVersions=`, apiValueStrings) @@ -46,7 +49,7 @@ export async function validateCommunities(): Promise { ) if (pubKey && pubKey === dbCom.publicKey.toString()) { logger.info(`Federation: matching publicKey: ${pubKey}`) - await DbCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() }) + await DbFederatedCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() }) logger.debug(`Federation: updated dbCom: ${JSON.stringify(dbCom)}`) } else { logger.warn( @@ -74,7 +77,9 @@ function isLogError(err: unknown) { return err instanceof LogError } -async function invokeVersionedRequestGetPublicKey(dbCom: DbCommunity): Promise { +async function invokeVersionedRequestGetPublicKey( + dbCom: DbFederatedCommunity, +): Promise { switch (dbCom.apiVersion) { case ApiVersionType.V1_0: return v1_0_requestGetPublicKey(dbCom) diff --git a/backend/src/graphql/model/Community.ts b/backend/src/graphql/model/Community.ts index d79d40034..43e0a7108 100644 --- a/backend/src/graphql/model/Community.ts +++ b/backend/src/graphql/model/Community.ts @@ -6,14 +6,12 @@ export class Community { constructor(dbCom: DbCommunity) { this.id = dbCom.id this.foreign = dbCom.foreign - this.publicKey = dbCom.publicKey.toString() - this.url = - (dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/') + dbCom.apiVersion - this.lastAnnouncedAt = dbCom.lastAnnouncedAt - this.verifiedAt = dbCom.verifiedAt - this.lastErrorAt = dbCom.lastErrorAt - this.createdAt = dbCom.createdAt - this.updatedAt = dbCom.updatedAt + this.name = dbCom.name + this.description = dbCom.description + this.url = dbCom.url + this.creationDate = dbCom.creationDate + this.uuid = dbCom.communityUuid + this.authenticatedAt = dbCom.authenticatedAt } @Field(() => Int) @@ -22,24 +20,21 @@ export class Community { @Field(() => Boolean) foreign: boolean - @Field(() => String) - publicKey: string + @Field(() => String, { nullable: true }) + name: string | null + + @Field(() => String, { nullable: true }) + description: string | null @Field(() => String) url: string @Field(() => Date, { nullable: true }) - lastAnnouncedAt: Date | null + creationDate: Date | null + + @Field(() => String, { nullable: true }) + uuid: string | null @Field(() => Date, { nullable: true }) - verifiedAt: Date | null - - @Field(() => Date, { nullable: true }) - lastErrorAt: Date | null - - @Field(() => Date, { nullable: true }) - createdAt: Date | null - - @Field(() => Date, { nullable: true }) - updatedAt: Date | null + authenticatedAt: Date | null } diff --git a/backend/src/graphql/model/FederatedCommunity.ts b/backend/src/graphql/model/FederatedCommunity.ts new file mode 100644 index 000000000..856a10d23 --- /dev/null +++ b/backend/src/graphql/model/FederatedCommunity.ts @@ -0,0 +1,45 @@ +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' +import { ObjectType, Field, Int } from 'type-graphql' + +@ObjectType() +export class FederatedCommunity { + constructor(dbCom: DbFederatedCommunity) { + this.id = dbCom.id + this.foreign = dbCom.foreign + this.publicKey = dbCom.publicKey.toString() + this.url = + (dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/') + dbCom.apiVersion + this.lastAnnouncedAt = dbCom.lastAnnouncedAt + this.verifiedAt = dbCom.verifiedAt + this.lastErrorAt = dbCom.lastErrorAt + this.createdAt = dbCom.createdAt + this.updatedAt = dbCom.updatedAt + } + + @Field(() => Int) + id: number + + @Field(() => Boolean) + foreign: boolean + + @Field(() => String) + publicKey: string + + @Field(() => String) + url: string + + @Field(() => Date, { nullable: true }) + lastAnnouncedAt: Date | null + + @Field(() => Date, { nullable: true }) + verifiedAt: Date | null + + @Field(() => Date, { nullable: true }) + lastErrorAt: Date | null + + @Field(() => Date, { nullable: true }) + createdAt: Date | null + + @Field(() => Date, { nullable: true }) + updatedAt: Date | null +} diff --git a/backend/src/graphql/resolver/CommunityResolver.test.ts b/backend/src/graphql/resolver/CommunityResolver.test.ts index 7d0b11d51..840544e51 100644 --- a/backend/src/graphql/resolver/CommunityResolver.test.ts +++ b/backend/src/graphql/resolver/CommunityResolver.test.ts @@ -1,6 +1,12 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ +/* eslint-disable @typescript-eslint/no-unsafe-call */ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/unbound-method */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ + import { Connection } from '@dbTools/typeorm' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { ApolloServerTestClient } from 'apollo-server-testing' import { testEnvironment } from '@test/helpers' @@ -19,7 +25,7 @@ beforeAll(async () => { testEnv = await testEnvironment() query = testEnv.query con = testEnv.con - await DbCommunity.clear() + await DbFederatedCommunity.clear() }) afterAll(async () => { @@ -28,12 +34,12 @@ afterAll(async () => { describe('CommunityResolver', () => { describe('getCommunities', () => { - let homeCom1: DbCommunity - let homeCom2: DbCommunity - let homeCom3: DbCommunity - let foreignCom1: DbCommunity - let foreignCom2: DbCommunity - let foreignCom3: DbCommunity + let homeCom1: DbFederatedCommunity + let homeCom2: DbFederatedCommunity + let homeCom3: DbFederatedCommunity + let foreignCom1: DbFederatedCommunity + let foreignCom2: DbFederatedCommunity + let foreignCom3: DbFederatedCommunity describe('with empty list', () => { it('returns no community entry', async () => { @@ -51,29 +57,29 @@ describe('CommunityResolver', () => { beforeEach(async () => { jest.clearAllMocks() - homeCom1 = DbCommunity.create() + homeCom1 = DbFederatedCommunity.create() homeCom1.foreign = false homeCom1.publicKey = Buffer.from('publicKey-HomeCommunity') homeCom1.apiVersion = '1_0' homeCom1.endPoint = 'http://localhost/api' homeCom1.createdAt = new Date() - await DbCommunity.insert(homeCom1) + await DbFederatedCommunity.insert(homeCom1) - homeCom2 = DbCommunity.create() + homeCom2 = DbFederatedCommunity.create() homeCom2.foreign = false homeCom2.publicKey = Buffer.from('publicKey-HomeCommunity') homeCom2.apiVersion = '1_1' homeCom2.endPoint = 'http://localhost/api' homeCom2.createdAt = new Date() - await DbCommunity.insert(homeCom2) + await DbFederatedCommunity.insert(homeCom2) - homeCom3 = DbCommunity.create() + homeCom3 = DbFederatedCommunity.create() homeCom3.foreign = false homeCom3.publicKey = Buffer.from('publicKey-HomeCommunity') homeCom3.apiVersion = '2_0' homeCom3.endPoint = 'http://localhost/api' homeCom3.createdAt = new Date() - await DbCommunity.insert(homeCom3) + await DbFederatedCommunity.insert(homeCom3) }) it('returns 3 home-community entries', async () => { @@ -123,29 +129,29 @@ describe('CommunityResolver', () => { beforeEach(async () => { jest.clearAllMocks() - foreignCom1 = DbCommunity.create() + foreignCom1 = DbFederatedCommunity.create() foreignCom1.foreign = true foreignCom1.publicKey = Buffer.from('publicKey-ForeignCommunity') foreignCom1.apiVersion = '1_0' foreignCom1.endPoint = 'http://remotehost/api' foreignCom1.createdAt = new Date() - await DbCommunity.insert(foreignCom1) + await DbFederatedCommunity.insert(foreignCom1) - foreignCom2 = DbCommunity.create() + foreignCom2 = DbFederatedCommunity.create() foreignCom2.foreign = true foreignCom2.publicKey = Buffer.from('publicKey-ForeignCommunity') foreignCom2.apiVersion = '1_1' foreignCom2.endPoint = 'http://remotehost/api' foreignCom2.createdAt = new Date() - await DbCommunity.insert(foreignCom2) + await DbFederatedCommunity.insert(foreignCom2) - foreignCom3 = DbCommunity.create() + foreignCom3 = DbFederatedCommunity.create() foreignCom3.foreign = true foreignCom3.publicKey = Buffer.from('publicKey-ForeignCommunity') foreignCom3.apiVersion = '1_2' foreignCom3.endPoint = 'http://remotehost/api' foreignCom3.createdAt = new Date() - await DbCommunity.insert(foreignCom3) + await DbFederatedCommunity.insert(foreignCom3) }) it('returns 3 home community and 3 foreign community entries', async () => { diff --git a/backend/src/graphql/resolver/CommunityResolver.ts b/backend/src/graphql/resolver/CommunityResolver.ts index 19a499a9b..4c6c8e785 100644 --- a/backend/src/graphql/resolver/CommunityResolver.ts +++ b/backend/src/graphql/resolver/CommunityResolver.ts @@ -1,22 +1,37 @@ import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { Resolver, Query, Authorized } from 'type-graphql' import { Community } from '@model/Community' +import { FederatedCommunity } from '@model/FederatedCommunity' import { RIGHTS } from '@/auth/RIGHTS' @Resolver() export class CommunityResolver { @Authorized([RIGHTS.COMMUNITIES]) - @Query(() => [Community]) - async getCommunities(): Promise { - const dbCommunities: DbCommunity[] = await DbCommunity.find({ + @Query(() => [FederatedCommunity]) + async getCommunities(): Promise { + const dbFederatedCommunities: DbFederatedCommunity[] = await DbFederatedCommunity.find({ order: { foreign: 'ASC', createdAt: 'DESC', lastAnnouncedAt: 'DESC', }, }) + return dbFederatedCommunities.map( + (dbCom: DbFederatedCommunity) => new FederatedCommunity(dbCom), + ) + } + + @Authorized([RIGHTS.COMMUNITIES]) + @Query(() => [Community]) + async getCommunitySelections(): Promise { + const dbCommunities: DbCommunity[] = await DbCommunity.find({ + order: { + name: 'ASC', + }, + }) return dbCommunities.map((dbCom: DbCommunity) => new Community(dbCom)) } } diff --git a/backend/src/graphql/resolver/KlicktippResolver.test.ts b/backend/src/graphql/resolver/KlicktippResolver.test.ts new file mode 100644 index 000000000..6a2250bc9 --- /dev/null +++ b/backend/src/graphql/resolver/KlicktippResolver.test.ts @@ -0,0 +1,138 @@ +/* eslint-disable @typescript-eslint/no-unsafe-argument */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ +/* eslint-disable @typescript-eslint/no-unsafe-call */ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +import { Event as DbEvent } from '@entity/Event' +import { UserContact } from '@entity/UserContact' +import { GraphQLError } from 'graphql' + +import { cleanDB, resetToken, testEnvironment } from '@test/helpers' +import { logger, i18n as localization } from '@test/testSetup' + +import { EventType } from '@/event/Events' +import { userFactory } from '@/seeds/factory/user' +import { login, subscribeNewsletter, unsubscribeNewsletter } from '@/seeds/graphql/mutations' +import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' + +let testEnv: any, mutate: any, con: any + +beforeAll(async () => { + testEnv = await testEnvironment(logger, localization) + mutate = testEnv.mutate + con = testEnv.con + await cleanDB() +}) + +afterAll(async () => { + await cleanDB() + await con.close() +}) + +describe('KlicktippResolver', () => { + beforeAll(async () => { + await userFactory(testEnv, bibiBloxberg) + }) + + afterAll(async () => { + await cleanDB() + }) + + describe('subscribeNewsletter', () => { + describe('unauthenticated', () => { + it('returns an error', async () => { + const { errors: errorObjects }: { errors: [GraphQLError] } = await mutate({ + mutation: subscribeNewsletter, + }) + + expect(errorObjects).toEqual([new GraphQLError('401 Unauthorized')]) + }) + }) + + describe('authenticated', () => { + beforeAll(async () => { + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + }) + + afterAll(() => { + resetToken() + }) + + it('calls API', async () => { + const { + data: { subscribeNewsletter: isSubscribed }, + }: { data: { subscribeNewsletter: boolean } } = await mutate({ + mutation: subscribeNewsletter, + }) + + expect(isSubscribed).toEqual(true) + }) + + it('stores the NEWSLETTER_SUBSCRIBE event in the database', async () => { + const userConatct = await UserContact.findOneOrFail( + { email: 'bibi@bloxberg.de' }, + { relations: ['user'] }, + ) + await expect(DbEvent.find()).resolves.toContainEqual( + expect.objectContaining({ + type: EventType.NEWSLETTER_SUBSCRIBE, + affectedUserId: userConatct.user.id, + actingUserId: userConatct.user.id, + }), + ) + }) + }) + }) + + describe('unsubscribeNewsletter', () => { + describe('unauthenticated', () => { + it('returns an error', async () => { + const { errors: errorObjects }: { errors: [GraphQLError] } = await mutate({ + mutation: unsubscribeNewsletter, + }) + + expect(errorObjects).toEqual([new GraphQLError('401 Unauthorized')]) + }) + }) + + describe('authenticated', () => { + beforeAll(async () => { + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + }) + + afterAll(() => { + resetToken() + }) + + it('calls API', async () => { + const { + data: { unsubscribeNewsletter: isUnsubscribed }, + }: { data: { unsubscribeNewsletter: boolean } } = await mutate({ + mutation: unsubscribeNewsletter, + }) + + expect(isUnsubscribed).toEqual(true) + }) + + it('stores the NEWSLETTER_UNSUBSCRIBE event in the database', async () => { + const userConatct = await UserContact.findOneOrFail( + { email: 'bibi@bloxberg.de' }, + { relations: ['user'] }, + ) + await expect(DbEvent.find()).resolves.toContainEqual( + expect.objectContaining({ + type: EventType.NEWSLETTER_UNSUBSCRIBE, + affectedUserId: userConatct.user.id, + actingUserId: userConatct.user.id, + }), + ) + }) + }) + }) +}) diff --git a/backend/src/graphql/resolver/KlicktippResolver.ts b/backend/src/graphql/resolver/KlicktippResolver.ts index 31bde0581..6875abcc5 100644 --- a/backend/src/graphql/resolver/KlicktippResolver.ts +++ b/backend/src/graphql/resolver/KlicktippResolver.ts @@ -1,33 +1,17 @@ -/* eslint-disable @typescript-eslint/no-unsafe-return */ -import { Resolver, Query, Authorized, Arg, Mutation, Ctx } from 'type-graphql' +import { Resolver, Authorized, Mutation, Ctx } from 'type-graphql' -import { - getKlickTippUser, - getKlicktippTagMap, - unsubscribe, - klicktippSignIn, -} from '@/apis/KlicktippController' +import { unsubscribe, klicktippSignIn } from '@/apis/KlicktippController' import { RIGHTS } from '@/auth/RIGHTS' +import { EVENT_NEWSLETTER_SUBSCRIBE, EVENT_NEWSLETTER_UNSUBSCRIBE } from '@/event/Events' import { Context, getUser } from '@/server/context' @Resolver() export class KlicktippResolver { - @Authorized([RIGHTS.GET_KLICKTIPP_USER]) - @Query(() => String) - async getKlicktippUser(@Arg('email') email: string): Promise { - return await getKlickTippUser(email) - } - - @Authorized([RIGHTS.GET_KLICKTIPP_TAG_MAP]) - @Query(() => String) - async getKlicktippTagMap(): Promise { - return await getKlicktippTagMap() - } - @Authorized([RIGHTS.UNSUBSCRIBE_NEWSLETTER]) @Mutation(() => Boolean) async unsubscribeNewsletter(@Ctx() context: Context): Promise { const user = getUser(context) + await EVENT_NEWSLETTER_UNSUBSCRIBE(user) return unsubscribe(user.emailContact.email) } @@ -35,6 +19,7 @@ export class KlicktippResolver { @Mutation(() => Boolean) async subscribeNewsletter(@Ctx() context: Context): Promise { const user = getUser(context) + await EVENT_NEWSLETTER_SUBSCRIBE(user) return klicktippSignIn(user.emailContact.email, user.language) } } diff --git a/backend/src/middleware/klicktippMiddleware.ts b/backend/src/middleware/klicktippMiddleware.ts index c5d6dd0ff..4c5f8db4f 100644 --- a/backend/src/middleware/klicktippMiddleware.ts +++ b/backend/src/middleware/klicktippMiddleware.ts @@ -7,8 +7,7 @@ import { MiddlewareFn } from 'type-graphql' import { KlickTipp } from '@model/KlickTipp' -import { /* klicktippSignIn, */ getKlickTippUser } from '@/apis/KlicktippController' -import { CONFIG } from '@/config' +import { getKlickTippUser } from '@/apis/KlicktippController' import { klickTippLogger as logger } from '@/server/logger' // export const klicktippRegistrationMiddleware: MiddlewareFn = async ( @@ -32,15 +31,13 @@ export const klicktippNewsletterStateMiddleware: MiddlewareFn = async ( // eslint-disable-next-line n/callback-return const result = await next() let klickTipp = new KlickTipp({ status: 'Unsubscribed' }) - if (CONFIG.KLICKTIPP) { - try { - const klickTippUser = await getKlickTippUser(result.email) - if (klickTippUser) { - klickTipp = new KlickTipp(klickTippUser) - } - } catch (err) { - logger.error(`There is no user for (email='${result.email}') ${err}`) + try { + const klickTippUser = await getKlickTippUser(result.email) + if (klickTippUser) { + klickTipp = new KlickTipp(klickTippUser) } + } catch (err) { + logger.error(`There is no user for (email='${result.email}') ${err}`) } result.klickTipp = klickTipp return result diff --git a/backend/src/seeds/graphql/mutations.ts b/backend/src/seeds/graphql/mutations.ts index b0716ff74..67a01977f 100644 --- a/backend/src/seeds/graphql/mutations.ts +++ b/backend/src/seeds/graphql/mutations.ts @@ -1,14 +1,14 @@ import { gql } from 'graphql-tag' export const subscribeNewsletter = gql` - mutation ($email: String!, $language: String!) { - subscribeNewsletter(email: $email, language: $language) + mutation { + subscribeNewsletter } ` export const unsubscribeNewsletter = gql` - mutation ($email: String!) { - unsubscribeNewsletter(email: $email) + mutation { + unsubscribeNewsletter } ` diff --git a/database/entity/0065-refactor_communities_table/Community.ts b/database/entity/0065-refactor_communities_table/Community.ts new file mode 100644 index 000000000..5857634a6 --- /dev/null +++ b/database/entity/0065-refactor_communities_table/Community.ts @@ -0,0 +1,60 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('communities') +export class Community extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'url', length: 255, nullable: false }) + url: string + + @Column({ name: 'public_key', type: 'binary', length: 64, nullable: false }) + publicKey: Buffer + + @Column({ + name: 'community_uuid', + type: 'char', + length: 36, + nullable: true, + collation: 'utf8mb4_unicode_ci', + }) + communityUuid: string | null + + @Column({ name: 'authenticated_at', type: 'datetime', nullable: true }) + authenticatedAt: Date | null + + @Column({ name: 'name', type: 'varchar', length: 40, nullable: true }) + name: string | null + + @Column({ name: 'description', type: 'varchar', length: 255, nullable: true }) + description: string | null + + @CreateDateColumn({ name: 'creation_date', type: 'datetime', nullable: true }) + creationDate: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/0065-refactor_communities_table/FederatedCommunity.ts b/database/entity/0065-refactor_communities_table/FederatedCommunity.ts new file mode 100644 index 000000000..0adbf4612 --- /dev/null +++ b/database/entity/0065-refactor_communities_table/FederatedCommunity.ts @@ -0,0 +1,51 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('federated_communities') +export class FederatedCommunity extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'public_key', type: 'binary', length: 64, default: null, nullable: true }) + publicKey: Buffer + + @Column({ name: 'api_version', length: 10, nullable: false }) + apiVersion: string + + @Column({ name: 'end_point', length: 255, nullable: false }) + endPoint: string + + @Column({ name: 'last_announced_at', type: 'datetime', nullable: true }) + lastAnnouncedAt: Date | null + + @Column({ name: 'verified_at', type: 'datetime', nullable: true }) + verifiedAt: Date | null + + @Column({ name: 'last_error_at', type: 'datetime', nullable: true }) + lastErrorAt: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/Community.ts b/database/entity/Community.ts index 80e5ace30..ee08323b6 100644 --- a/database/entity/Community.ts +++ b/database/entity/Community.ts @@ -1 +1 @@ -export { Community } from './0060-update_communities_table/Community' +export { Community } from './0065-refactor_communities_table/Community' diff --git a/database/entity/FederatedCommunity.ts b/database/entity/FederatedCommunity.ts new file mode 100644 index 000000000..cacaaff9c --- /dev/null +++ b/database/entity/FederatedCommunity.ts @@ -0,0 +1 @@ +export { FederatedCommunity } from './0065-refactor_communities_table/FederatedCommunity' diff --git a/database/entity/index.ts b/database/entity/index.ts index 2d9d04c3b..d44029754 100644 --- a/database/entity/index.ts +++ b/database/entity/index.ts @@ -10,6 +10,7 @@ import { Contribution } from './Contribution' import { Event } from './Event' import { ContributionMessage } from './ContributionMessage' import { Community } from './Community' +import { FederatedCommunity } from './FederatedCommunity' export const entities = [ Community, @@ -17,6 +18,7 @@ export const entities = [ ContributionLink, ContributionMessage, Event, + FederatedCommunity, LoginElopageBuys, LoginEmailOptIn, Migration, diff --git a/database/migrations/0065-refactor_communities_table.ts b/database/migrations/0065-refactor_communities_table.ts new file mode 100644 index 000000000..06f5b3990 --- /dev/null +++ b/database/migrations/0065-refactor_communities_table.ts @@ -0,0 +1,36 @@ +/* MIGRATION TO CREATE THE FEDERATION COMMUNITY TABLES + * + * This migration creates the `community` and 'communityfederation' tables in the `apollo` database (`gradido_community`). + */ + +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn(`RENAME TABLE communities TO federated_communities;`) + await queryFn(` + CREATE TABLE communities ( + \`id\` int unsigned NOT NULL AUTO_INCREMENT, + \`foreign\` tinyint(4) NOT NULL DEFAULT 1, + \`url\` varchar(255) NOT NULL, + \`public_key\` binary(64) NOT NULL, + \`community_uuid\` char(36) NULL, + \`authenticated_at\` datetime(3) NULL, + \`name\` varchar(40) NULL, + \`description\` varchar(255) NULL, + \`creation_date\` datetime(3) NULL, + \`created_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + \`updated_at\` datetime(3), + PRIMARY KEY (id), + UNIQUE KEY url_key (url), + UNIQUE KEY uuid_key (community_uuid), + UNIQUE KEY public_key_key (public_key) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + `) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // write downgrade logic as parameter of queryFn + await queryFn(`DROP TABLE communities;`) + await queryFn(`RENAME TABLE federated_communities TO communities;`) +} diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 078c0fbf3..5c4676337 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -3,7 +3,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0064-event_rename', + DB_VERSION: '0065-refactor_communities_table', LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL || 'info', diff --git a/dht-node/src/dht_node/index.test.ts b/dht-node/src/dht_node/index.test.ts index ac5b1b21a..e76e6ac9f 100644 --- a/dht-node/src/dht_node/index.test.ts +++ b/dht-node/src/dht_node/index.test.ts @@ -5,7 +5,7 @@ import { startDHT } from './index' import DHT from '@hyperswarm/dht' import CONFIG from '@/config' import { logger } from '@test/testSetup' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { testEnvironment, cleanDB } from '@test/helpers' CONFIG.FEDERATION_DHT_SEED = '64ebcb0e3ad547848fef4197c6e2332f' @@ -261,7 +261,7 @@ describe('federation', () => { describe('with receiving wrong but tolerated property data', () => { let jsonArray: any[] - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() jsonArray = [ @@ -277,7 +277,7 @@ describe('federation', () => { }, ] await socketEventMocks.data(Buffer.from(JSON.stringify(jsonArray))) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { @@ -523,7 +523,7 @@ describe('federation', () => { describe('with receiving data of exact max allowed properties length', () => { let jsonArray: any[] - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() jsonArray = [ @@ -538,7 +538,7 @@ describe('federation', () => { { api: 'toolong api', url: 'some valid url' }, ] await socketEventMocks.data(Buffer.from(JSON.stringify(jsonArray))) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { @@ -570,7 +570,7 @@ describe('federation', () => { describe('with receiving data of exact max allowed buffer length', () => { let jsonArray: any[] - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() jsonArray = [ @@ -592,7 +592,7 @@ describe('federation', () => { }, ] await socketEventMocks.data(Buffer.from(JSON.stringify(jsonArray))) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { @@ -711,7 +711,7 @@ describe('federation', () => { }) describe('with proper data', () => { - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() await socketEventMocks.data( @@ -728,7 +728,7 @@ describe('federation', () => { ]), ), ) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index d101037ae..0db7a28c2 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -3,7 +3,7 @@ import DHT from '@hyperswarm/dht' import { logger } from '@/server/logger' import CONFIG from '@/config' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' const KEY_SECRET_SEEDBYTES = 32 const getSeed = (): Buffer | null => @@ -31,7 +31,7 @@ export const startDHT = async (topic: string): Promise => { logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) - const ownApiVersions = await writeHomeCommunityEnries(keyPair.publicKey) + const ownApiVersions = await writeFederatedHomeCommunityEnries(keyPair.publicKey) logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`) const node = new DHT({ keyPair }) @@ -92,9 +92,9 @@ export const startDHT = async (topic: string): Promise => { } logger.debug(`upsert with variables=${JSON.stringify(variables)}`) // this will NOT update the updatedAt column, to distingue between a normal update and the last announcement - await DbCommunity.createQueryBuilder() + await DbFederatedCommunity.createQueryBuilder() .insert() - .into(DbCommunity) + .into(DbFederatedCommunity) .values(variables) .orUpdate({ conflict_target: ['id', 'publicKey', 'apiVersion'], @@ -179,7 +179,7 @@ export const startDHT = async (topic: string): Promise => { } } -async function writeHomeCommunityEnries(pubKey: any): Promise { +async function writeFederatedHomeCommunityEnries(pubKey: any): Promise { const homeApiVersions: CommunityApi[] = Object.values(ApiVersionType).map(function (apiEnum) { const comApi: CommunityApi = { api: apiEnum, @@ -189,17 +189,17 @@ async function writeHomeCommunityEnries(pubKey: any): Promise { }) try { // first remove privious existing homeCommunity entries - DbCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() + DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() homeApiVersions.forEach(async function (homeApi) { - const homeCom = new DbCommunity() + const homeCom = new DbFederatedCommunity() homeCom.foreign = false homeCom.apiVersion = homeApi.api homeCom.endPoint = homeApi.url homeCom.publicKey = pubKey.toString('hex') // this will NOT update the updatedAt column, to distingue between a normal update and the last announcement - await DbCommunity.insert(homeCom) + await DbFederatedCommunity.insert(homeCom) logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`) }) } catch (err) { diff --git a/docu/Concepts/TechnicalRequirements/Federation.md b/docu/Concepts/TechnicalRequirements/Federation.md index 8bbd74a3e..84c4965af 100644 --- a/docu/Concepts/TechnicalRequirements/Federation.md +++ b/docu/Concepts/TechnicalRequirements/Federation.md @@ -50,6 +50,52 @@ Before starting in describing the details of the federation handshake, some prer With the federation additional data tables/entities have to be created. +##### 1st Draft + +The following diagramms shows the first draft of the possible database-model base on the migration 0063-event_link_fields.ts with 3 steps of migration to reach the required entities. All three diagramms are not exhaustive and are still a base for discussions: + +![img](./image/classdiagramm_x-community-readyness_step1.svg) + +In the first step the current communities table will be renamed to communities_federation. A new table communities is created. Because of the dynamic in the communities_federation data during dht-federation the relation between both entities will be on the collumn communities.communities_federation_public_key. This relation will allow to read a community-entry including its relation to the multi federation entries per api-version with the public key as identifier. + +![img](./image/classdiagramm_x-community-readyness_step2.svg) + +The 2nd step is an introduction of the entity accounts between the users and the transactions table. This will cause a separation of the transactions from the users, to avoid possible conflicts or dependencies between local users of the community and remote users of foreign users, who will be part of x-communitiy-transactions. + +![img](./image/classdiagramm_x-community-readyness_step3.svg) + +The 3rd step will introduce an additional foreign-users and a users_favorites table. A foreign_user could be stored in the existing users-table, but he will not need all the attributes of a home-user, especially he will never gets an AGE-account in this community. The user_favorites entity is designed to buildup the relations between users and foreign_users or in general between all users. This is simply a first idea for a future discussion. + +##### 2nd Draft + +After team discussion in architecture meeting a second vision draft for database migration is shown in the following picture. Only the concerned tables of the database migration are presented. The three elliptical surroundings shows the different steps, which should be done in separate issues. The model, table namings and columns are not exhaustive and are still a base for further discussions. + +![img](./image/class-diagramm_vision-draft2.svg) + +**The first step** with renaming the current `communities` table in `communities_federation` and creating a new `communities` table is not changed. More details about motivation and arguments are described above. + +**The second step** is changed to migrate the `users `table by creating a new `users_settings` table and shift the most existing attributes from `users `table to it. The criterium for shifting a column to `user_settings` is to keep only those columns in the `users `table, which will be used for "home-users" and "foreign-users". A foreign-user at this point of time is a user of an other community, who is involved in a x-community-transaction as `linked_user`. He will not have the possibility to login to the home-community, because after a x-community-tx only the attributes of the `users `table will be exchanged during the handshake of transaction processing of both communities. Even the `transactions `table will be ready for x-community-tx with this `users `and `user_settings` migration, because it contains a reference to both participants of the transaction. For easier displaying and because of historical reasons it will be a good idea to add the columns `linked_user `and `user `(not shown in diagramm) to the `transactions `table with type varchar(512) to store the valid firstname and lastname of the participants at transaction-date. If one of the participants will change his names, there will be no migration of the transaction data necessary and a transaction-list will present always the currect names even over a long distance. + +**The third step** contains a migration for handling accounts and user_correlations. With the new table `gdd_accounts `a new entity will be introduced to support the different types of gradido accounts AGE, GMW and AUF. The second new table `user_correlations `is to buildup the different correlations a user will have: + +* user to user correlation like favorites, trustee, etc +* user to account correlation for cashier of a GMW and AUF community account, trustee of children or seniors, etc. + +The previous idea to replace the `user_id `with an `account_id` in the `transactions `table will be not necessary with this draft, because it will not cause a benefit and saves a lot refactoring efforts. + +##### 3rd Draft + +After further discussions about the database-model and the necessary migration steps the team decided to integrate an additional migration step for X-Community-Transaction. The decision base on keeping the goal focus on implementation of the x-community sendCoins feature as soon as possible. + +![img](./image/class-diagramm_vision-draft3.svg) + +The additional migration step 2 will simply concentrated on the `transactions `table by adding all necessary columns to handle a *x-community-tx* without a previous migration of the `users `table, shown as step 3 in the picture above. + +In concequence of these additional columns in the `transactions `table the database-model will be denormalized by containing redundanten columns. But this migration step will reduce the necessary efforts to reach the *x-community sendCoins* feature as soon as possible. On the other side it offers more possibilities to ensure data consitency, because of internal data checks in conjunction with the redundant columns. + +The feature *x-community-tx* per *sendCoins* will create several challenges, because there is no technical transaction bracket, which will ensure consistent data in both community databases after all write access are finished. The most favorite concept about handling a x-community-transaction and the upcoming handshake to ensure valid send- and receive-transaction entries in both databases is to follow the two-phase-commit protocol. To avoid blocking the transactions table or user dependent transactions-entries during the two-phase-commit processing the idea of pending_transactions is born. This additional pending_transactions table contains the same columns than the transactions table plus one column named `x_transactions_state`. + + ##### Community-Entity Create the new *Community* table to store attributes of the own community. This table is used more like a frame for own community data in the future like the list of federated foreign communities, own users, own futher accounts like AUF- and Welfare-account and the profile data of the own community: diff --git a/docu/Concepts/TechnicalRequirements/graphics/classdiagramm_x-community-readyness.drawio b/docu/Concepts/TechnicalRequirements/graphics/classdiagramm_x-community-readyness.drawio new file mode 100644 index 000000000..c618971f8 --- /dev/null +++ b/docu/Concepts/TechnicalRequirements/graphics/classdiagramm_x-community-readyness.drawio @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft2.png b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft2.png new file mode 100644 index 000000000..cd12f1fdd Binary files /dev/null and b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft2.png differ diff --git a/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft2.svg b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft2.svg new file mode 100644 index 000000000..a3a6fd0b4 --- /dev/null +++ b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft2.svg @@ -0,0 +1 @@ +
X-Community-Readyness
Step 3
X-Community-Read...
X-Community-Readyness
Step 1
X-Community-Read...
X-Community-Readyness
Step 2
X-Community-Read...
Text is not SVG - cannot display
\ No newline at end of file diff --git a/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft3.png b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft3.png new file mode 100644 index 000000000..b6643fd32 Binary files /dev/null and b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft3.png differ diff --git a/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft3.svg b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft3.svg new file mode 100644 index 000000000..acf9ebc0d --- /dev/null +++ b/docu/Concepts/TechnicalRequirements/image/class-diagramm_vision-draft3.svg @@ -0,0 +1 @@ +
X-Community-Readyness
Step 4
X-Community-Read...
X-Community-Readyness
Step 1
X-Community-Read...
X-Community-Readyness
Step 3
X-Community-Read...
X-Community-Readyness
Step 2
X-Community-Read...
Text is not SVG - cannot display
\ No newline at end of file diff --git a/docu/Concepts/TechnicalRequirements/image/classdiagramm_communities-communities_federation.png b/docu/Concepts/TechnicalRequirements/image/classdiagramm_communities-communities_federation.png new file mode 100644 index 000000000..a794f9e75 Binary files /dev/null and b/docu/Concepts/TechnicalRequirements/image/classdiagramm_communities-communities_federation.png differ diff --git a/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step1.svg b/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step1.svg new file mode 100644 index 000000000..28bf0d314 --- /dev/null +++ b/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step1.svg @@ -0,0 +1 @@ +
X-Community-Readyness
Step 1
X-Community-Read...
Text is not SVG - cannot display
\ No newline at end of file diff --git a/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step2.svg b/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step2.svg new file mode 100644 index 000000000..41300d046 --- /dev/null +++ b/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step2.svg @@ -0,0 +1 @@ +
X-Community-Readyness
Step 2
X-Community-Read...
Text is not SVG - cannot display
\ No newline at end of file diff --git a/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step3.svg b/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step3.svg new file mode 100644 index 000000000..382c9d728 --- /dev/null +++ b/docu/Concepts/TechnicalRequirements/image/classdiagramm_x-community-readyness_step3.svg @@ -0,0 +1 @@ +
X-Community-Readyness
Step 3
X-Community-Read...
Text is not SVG - cannot display
\ No newline at end of file diff --git a/federation/src/config/index.ts b/federation/src/config/index.ts index ce0c5a9a5..70a155d63 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -11,7 +11,7 @@ Decimal.set({ */ const constants = { - DB_VERSION: '0064-event_rename', + DB_VERSION: '0065-refactor_communities_table', // DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts index 20e6c8228..18d2a7599 100644 --- a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts +++ b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { createTestClient } from 'apollo-server-testing' import createServer from '@/server/createServer' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' let query: any @@ -13,7 +13,7 @@ beforeAll(async () => { const server = await createServer() con = server.con query = createTestClient(server.apollo).query - DbCommunity.clear() + DbFederatedCommunity.clear() }) afterAll(async () => { @@ -32,12 +32,12 @@ describe('PublicKeyResolver', () => { describe('getPublicKey', () => { beforeEach(async () => { - const homeCom = new DbCommunity() + const homeCom = new DbFederatedCommunity() homeCom.foreign = false homeCom.apiVersion = '1_0' homeCom.endPoint = 'endpoint-url' homeCom.publicKey = Buffer.from('homeCommunity-publicKey') - await DbCommunity.insert(homeCom) + await DbFederatedCommunity.insert(homeCom) }) it('returns homeCommunity-publicKey', async () => { diff --git a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts index f96f20c58..0145324fc 100644 --- a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts +++ b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts @@ -1,7 +1,7 @@ // eslint-disable-next-line @typescript-eslint/no-unused-vars import { Query, Resolver } from 'type-graphql' import { federationLogger as logger } from '@/server/logger' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { GetPublicKeyResult } from '../model/GetPublicKeyResult' @Resolver() @@ -10,7 +10,7 @@ export class PublicKeyResolver { @Query(() => GetPublicKeyResult) async getPublicKey(): Promise { logger.debug(`getPublicKey() via apiVersion=1_0 ...`) - const homeCom = await DbCommunity.findOneOrFail({ + const homeCom = await DbFederatedCommunity.findOneOrFail({ foreign: false, apiVersion: '1_0', })