move adjustet Community Model Code into separat Model Class

This commit is contained in:
einhornimmond 2024-02-21 18:15:54 +01:00
parent b587a51ee8
commit d187c541da
3 changed files with 85 additions and 44 deletions

View File

@ -0,0 +1,76 @@
import { Community as DbCommunity } from '@entity/Community'
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
import { ObjectType, Field } from 'type-graphql'
import { FederatedCommunity } from './FederatedCommunity'
@ObjectType()
export class AdminCommunityView {
constructor(dbCom: DbCommunity) {
if (dbCom.federatedCommunities && dbCom.federatedCommunities.length > 0) {
const federatedCommunity = dbCom.federatedCommunities[0]
this.foreign = federatedCommunity.foreign
const url = new URL(federatedCommunity.endPoint)
// use only the host part
this.url = url.protocol + '//' + url.host
this.publicKey = federatedCommunity.publicKey.toString('hex')
this.federatedCommunities = dbCom.federatedCommunities.map(
(federatedCom: DbFederatedCommunity) => new FederatedCommunity(federatedCom),
)
}
if (dbCom.foreign !== undefined) {
this.foreign = dbCom.foreign
}
this.name = dbCom.name
this.description = dbCom.description
this.gmsApiKey = dbCom.gmsApiKey
if (dbCom.url) {
this.url = dbCom.url
}
if (dbCom.publicKey && dbCom.publicKey.length === 32) {
this.publicKey = dbCom.publicKey.toString('hex')
}
this.creationDate = dbCom.creationDate
this.createdAt = dbCom.createdAt
this.updatedAt = dbCom.updatedAt
this.uuid = dbCom.communityUuid
this.authenticatedAt = dbCom.authenticatedAt
this.gmsApiKey = dbCom.gmsApiKey
}
@Field(() => Boolean)
foreign: boolean
@Field(() => String)
url: string
@Field(() => String)
publicKey: string
@Field(() => String, { nullable: true })
uuid: string | null
@Field(() => Date, { nullable: true })
authenticatedAt: Date | null
@Field(() => String, { nullable: true })
name: string | null
@Field(() => String, { nullable: true })
description: string | null
@Field(() => String, { nullable: true })
gmsApiKey: string | null
@Field(() => Date, { nullable: true })
creationDate: Date | null
@Field(() => Date, { nullable: true })
createdAt: Date | null
@Field(() => Date, { nullable: true })
updatedAt: Date | null
@Field(() => [FederatedCommunity], { nullable: true })
federatedCommunities: FederatedCommunity[] | null
}

View File

@ -1,39 +1,15 @@
import { Community as DbCommunity } from '@entity/Community'
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
import { ObjectType, Field, Int } from 'type-graphql'
import { FederatedCommunity } from './FederatedCommunity'
@ObjectType()
export class Community {
constructor(dbCom: DbCommunity) {
if (dbCom.federatedCommunities && dbCom.federatedCommunities.length > 0) {
const federatedCommunity = dbCom.federatedCommunities[0]
this.foreign = federatedCommunity.foreign
const url = new URL(federatedCommunity.endPoint)
// use only the host part
this.url = url.protocol + '//' + url.host
this.publicKey = federatedCommunity.publicKey.toString('hex')
this.federatedCommunities = dbCom.federatedCommunities.map(
(federatedCom: DbFederatedCommunity) => new FederatedCommunity(federatedCom),
)
}
this.id = dbCom.id ?? 0
if (dbCom.foreign !== undefined) {
this.foreign = dbCom.foreign
}
this.id = dbCom.id
this.foreign = dbCom.foreign
this.name = dbCom.name
this.description = dbCom.description
this.gmsApiKey = dbCom.gmsApiKey
if (dbCom.url) {
this.url = dbCom.url
}
if (dbCom.publicKey && dbCom.publicKey.length === 32) {
this.publicKey = dbCom.publicKey.toString('hex')
}
this.url = dbCom.url
this.creationDate = dbCom.creationDate
this.createdAt = dbCom.createdAt
this.updatedAt = dbCom.updatedAt
this.uuid = dbCom.communityUuid
this.authenticatedAt = dbCom.authenticatedAt
this.gmsApiKey = dbCom.gmsApiKey
@ -51,30 +27,18 @@ export class Community {
@Field(() => String, { nullable: true })
description: string | null
@Field(() => String, { nullable: true })
gmsApiKey: string | null
@Field(() => String)
url: string
@Field(() => String)
publicKey: string
@Field(() => Date, { nullable: true })
creationDate: Date | null
@Field(() => Date, { nullable: true })
createdAt: Date | null
@Field(() => Date, { nullable: true })
updatedAt: Date | null
@Field(() => String, { nullable: true })
uuid: string | null
@Field(() => Date, { nullable: true })
authenticatedAt: Date | null
@Field(() => [FederatedCommunity], { nullable: true })
federatedCommunities: FederatedCommunity[] | null
@Field(() => String, { nullable: true })
gmsApiKey: string | null
}

View File

@ -5,6 +5,7 @@ import { Resolver, Query, Authorized, Arg, Mutation, Args } from 'type-graphql'
import { CommunityArgs } from '@arg//CommunityArgs'
import { Paginated } from '@arg/Paginated'
import { AdminCommunityView } from '@model/AdminCommunityView'
import { Community } from '@model/Community'
import { FederatedCommunity } from '@model/FederatedCommunity'
@ -31,9 +32,9 @@ export class CommunityResolver {
}
@Authorized([RIGHTS.COMMUNITIES])
@Query(() => [Community])
async allCommunities(@Args() paginated: Paginated): Promise<Community[]> {
return (await getAllCommunities(paginated)).map((dbCom) => new Community(dbCom))
@Query(() => [AdminCommunityView])
async allCommunities(@Args() paginated: Paginated): Promise<AdminCommunityView[]> {
return (await getAllCommunities(paginated)).map((dbCom) => new AdminCommunityView(dbCom))
}
@Authorized([RIGHTS.COMMUNITIES])