From 9a834fa720e55cdf0f563263695a0cbd52e4e72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Thu, 9 Mar 2023 18:56:25 +0100 Subject: [PATCH] implement getCommunities in CommunityResolver --- backend/src/graphql/model/Community.ts | 46 ++++-- .../resolver/CommunityResolver.test.ts | 136 +++++++++--------- .../src/graphql/resolver/CommunityResolver.ts | 55 ++----- .../Community.ts | 6 +- 4 files changed, 111 insertions(+), 132 deletions(-) diff --git a/backend/src/graphql/model/Community.ts b/backend/src/graphql/model/Community.ts index 466028d00..9840dda9e 100644 --- a/backend/src/graphql/model/Community.ts +++ b/backend/src/graphql/model/Community.ts @@ -1,31 +1,47 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { ObjectType, Field } from 'type-graphql' +import { Community as DbCommunity } from '@entity/Community' @ObjectType() export class Community { - constructor(json?: any) { - if (json) { - this.id = Number(json.id) - this.name = json.name - this.url = json.url - this.description = json.description - this.registerUrl = json.registerUrl - } + 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 + '/') + + 'api/' + + dbCom.apiVersion + this.lastAnnouncedAt = dbCom.lastAnnouncedAt + this.verifiedAt = dbCom.verifiedAt + this.lastErrorAt = dbCom.lastErrorAt + this.createdAt = dbCom.createdAt + this.updatedAt = dbCom.updatedAt } @Field(() => Number) id: number + @Field(() => Boolean) + foreign: boolean + @Field(() => String) - name: string + publicKey: string @Field(() => String) url: string - @Field(() => String) - description: string + @Field(() => Date, { nullable: true }) + lastAnnouncedAt: Date | null - @Field(() => String) - registerUrl: string + @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 cb916e47c..c48ab0f25 100644 --- a/backend/src/graphql/resolver/CommunityResolver.test.ts +++ b/backend/src/graphql/resolver/CommunityResolver.test.ts @@ -3,7 +3,9 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '@/server/createServer' -import CONFIG from '@/config' +import { resetEntity } from '@test/helpers' +import { Community as DbCommunity } from '@entity/Community' +import { Community } from '../model/Community' jest.mock('@/config') @@ -16,6 +18,7 @@ beforeAll(async () => { const server = await createServer({}) con = server.con query = createTestClient(server.apollo).query + resetEntity(DbCommunity) }) afterAll(async () => { @@ -23,96 +26,93 @@ afterAll(async () => { }) describe('CommunityResolver', () => { - const getCommunityInfoQuery = ` + const getCommunities = ` query { - getCommunityInfo { - name - description - url - registerUrl - } - } - ` - - const communities = ` - query { - communities { + getCommunities { id - name + foreign + publicKey url - description - registerUrl + lastAnnouncedAt + verifiedAt + lastErrorAt + createdAt + updatedAt } } ` - describe('getCommunityInfo', () => { - it('returns the default values', async () => { - await expect(query({ query: getCommunityInfoQuery })).resolves.toMatchObject({ - data: { - getCommunityInfo: { - name: 'Gradido Entwicklung', - description: 'Die lokale Entwicklungsumgebung von Gradido.', - url: 'http://localhost/', - registerUrl: 'http://localhost/register', - }, - }, + describe('getCommunities', () => { + describe('with empty list', () => { + it('returns no community entry', async () => { + const result: Community[] = await query({ query: getCommunities }) + expect(result.length).decimalEqual(0) }) }) - }) - describe('communities', () => { - describe('PRODUCTION = false', () => { + describe('only home-communities entries', () => { beforeEach(() => { - CONFIG.PRODUCTION = false + const homeCom1 = DbCommunity.create() + homeCom1.foreign = false + homeCom1.publicKey = Buffer.from('publicKey-HomeCommunity') + homeCom1.apiVersion = '1_0' + homeCom1.endPoint = 'https://localhost' + homeCom1.createdAt = new Date() + DbCommunity.save(homeCom1) + + const homeCom2 = DbCommunity.create() + homeCom2.foreign = false + homeCom2.publicKey = Buffer.from('publicKey-HomeCommunity') + homeCom2.apiVersion = '1_1' + homeCom2.endPoint = 'https://localhost' + homeCom2.createdAt = new Date() + DbCommunity.save(homeCom2) + + const homeCom3 = DbCommunity.create() + homeCom3.foreign = false + homeCom3.publicKey = Buffer.from('publicKey-HomeCommunity') + homeCom3.apiVersion = '2_0' + homeCom3.endPoint = 'https://localhost' + homeCom3.createdAt = new Date() + DbCommunity.save(homeCom3) }) - it('returns three communities', async () => { - await expect(query({ query: communities })).resolves.toMatchObject({ + it('returns three home-community entries', async () => { + await expect(query({ query: getCommunities })).resolves.toMatchObject({ data: { communities: [ { id: 1, - name: 'Gradido Entwicklung', - description: 'Die lokale Entwicklungsumgebung von Gradido.', - url: 'http://localhost/', - registerUrl: 'http://localhost/register-community', + foreign: false, + publicKey: expect.stringMatching(Buffer.from('publicKey-HomeCommunity').toString()), + url: 'http://localhost/api/1_0', + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: expect.any(Date), + updatedAt: null, }, { id: 2, - name: 'Gradido Staging', - description: 'Der Testserver der Gradido-Akademie.', - url: 'https://stage1.gradido.net/', - registerUrl: 'https://stage1.gradido.net/register-community', + foreign: false, + publicKey: expect.stringMatching(Buffer.from('publicKey-HomeCommunity').toString()), + url: 'http://localhost/api/1_1', + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: expect.any(Date), + updatedAt: null, }, { id: 3, - name: 'Gradido-Akademie', - description: 'Freies Institut für Wirtschaftsbionik.', - url: 'https://gradido.net', - registerUrl: 'https://gdd1.gradido.com/register-community', - }, - ], - }, - }) - }) - }) - - describe('PRODUCTION = true', () => { - beforeEach(() => { - CONFIG.PRODUCTION = true - }) - - it('returns one community', async () => { - await expect(query({ query: communities })).resolves.toMatchObject({ - data: { - communities: [ - { - id: 3, - name: 'Gradido-Akademie', - description: 'Freies Institut für Wirtschaftsbionik.', - url: 'https://gradido.net', - registerUrl: 'https://gdd1.gradido.com/register-community', + foreign: false, + publicKey: expect.stringMatching(Buffer.from('publicKey-HomeCommunity').toString()), + url: 'http://localhost/api/2_0', + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: expect.any(Date), + updatedAt: null, }, ], }, diff --git a/backend/src/graphql/resolver/CommunityResolver.ts b/backend/src/graphql/resolver/CommunityResolver.ts index f56254e1f..8250170c6 100644 --- a/backend/src/graphql/resolver/CommunityResolver.ts +++ b/backend/src/graphql/resolver/CommunityResolver.ts @@ -1,58 +1,21 @@ import { Resolver, Query, Authorized } from 'type-graphql' import { Community } from '@model/Community' +import { Community as DbCommunity } from '@entity/Community' import { RIGHTS } from '@/auth/RIGHTS' -import CONFIG from '@/config' @Resolver() export class CommunityResolver { - @Authorized([RIGHTS.GET_COMMUNITY_INFO]) - @Query(() => Community) - async getCommunityInfo(): Promise { - return new Community({ - name: CONFIG.COMMUNITY_NAME, - description: CONFIG.COMMUNITY_DESCRIPTION, - url: CONFIG.COMMUNITY_URL, - registerUrl: CONFIG.COMMUNITY_REGISTER_URL, - }) - } - @Authorized([RIGHTS.COMMUNITIES]) @Query(() => [Community]) - async communities(): Promise { - if (CONFIG.PRODUCTION) - return [ - new Community({ - id: 3, - name: 'Gradido-Akademie', - description: 'Freies Institut für Wirtschaftsbionik.', - url: 'https://gradido.net', - registerUrl: 'https://gdd1.gradido.com/register-community', - }), - ] - return [ - new Community({ - id: 1, - name: 'Gradido Entwicklung', - description: 'Die lokale Entwicklungsumgebung von Gradido.', - url: 'http://localhost/', - registerUrl: 'http://localhost/register-community', - }), - new Community({ - id: 2, - name: 'Gradido Staging', - description: 'Der Testserver der Gradido-Akademie.', - url: 'https://stage1.gradido.net/', - registerUrl: 'https://stage1.gradido.net/register-community', - }), - new Community({ - id: 3, - name: 'Gradido-Akademie', - description: 'Freies Institut für Wirtschaftsbionik.', - url: 'https://gradido.net', - registerUrl: 'https://gdd1.gradido.com/register-community', - }), - ] + async getCommunities(): Promise { + const comList: Community[] = [] + const dbCommunities: DbCommunity[] = await DbCommunity.find() + dbCommunities.forEach(async function (dbCom) { + const com = new Community(dbCom) + comList.push(com) + }) + return comList } } diff --git a/database/entity/0060-update_communities_table/Community.ts b/database/entity/0060-update_communities_table/Community.ts index 5ded76cf9..ab7cfb58a 100644 --- a/database/entity/0060-update_communities_table/Community.ts +++ b/database/entity/0060-update_communities_table/Community.ts @@ -25,13 +25,13 @@ export class Community extends BaseEntity { endPoint: string @Column({ name: 'last_announced_at', type: 'datetime', nullable: true }) - lastAnnouncedAt: Date + lastAnnouncedAt: Date | null @Column({ name: 'verified_at', type: 'datetime', nullable: true }) - verifiedAt: Date + verifiedAt: Date | null @Column({ name: 'last_error_at', type: 'datetime', nullable: true }) - lastErrorAt: Date + lastErrorAt: Date | null @CreateDateColumn({ name: 'created_at',