gradido/backend/src/graphql/resolver/CommunityResolver.ts

96 lines
3.1 KiB
TypeScript

import { IsNull, Not } from '@dbTools/typeorm'
import { Community as DbCommunity } from '@entity/Community'
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
import { Resolver, Query, Authorized, Mutation, Args, Arg } from 'type-graphql'
import { Paginated } from '@arg/Paginated'
import { EditCommunityInput } from '@input/EditCommunityInput'
import { AdminCommunityView } from '@model/AdminCommunityView'
import { Community } from '@model/Community'
import { FederatedCommunity } from '@model/FederatedCommunity'
import { RIGHTS } from '@/auth/RIGHTS'
import { LogError } from '@/server/LogError'
import {
getAllCommunities,
getCommunityByIdentifier,
getCommunityByUuid,
getHomeCommunity,
} from './util/communities'
@Resolver()
export class CommunityResolver {
@Authorized([RIGHTS.COMMUNITIES])
@Query(() => [FederatedCommunity])
async getCommunities(): Promise<FederatedCommunity[]> {
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(() => [AdminCommunityView])
async allCommunities(@Args() paginated: Paginated): Promise<AdminCommunityView[]> {
return (await getAllCommunities(paginated)).map((dbCom) => new AdminCommunityView(dbCom))
}
@Authorized([RIGHTS.COMMUNITIES])
@Query(() => [Community])
async communities(): Promise<Community[]> {
const dbCommunities: DbCommunity[] = await DbCommunity.find({
where: { communityUuid: Not(IsNull()) }, //, authenticatedAt: Not(IsNull()) },
order: {
name: 'ASC',
},
})
return dbCommunities.map((dbCom: DbCommunity) => new Community(dbCom))
}
@Authorized([RIGHTS.COMMUNITY_BY_IDENTIFIER])
@Query(() => Community)
async communityByIdentifier(
@Arg('communityIdentifier') communityIdentifier: string,
): Promise<Community> {
const community = await getCommunityByIdentifier(communityIdentifier)
if (!community) {
throw new LogError('community not found', communityIdentifier)
}
return new Community(community)
}
@Authorized([RIGHTS.HOME_COMMUNITY])
@Query(() => Community)
async homeCommunity(): Promise<Community> {
const community = await getHomeCommunity()
if (!community) {
throw new LogError('no home community exist')
}
return new Community(community)
}
@Authorized([RIGHTS.COMMUNITY_UPDATE])
@Mutation(() => Community)
async updateHomeCommunity(@Args() { uuid, gmsApiKey }: EditCommunityInput): Promise<Community> {
const homeCom = await getCommunityByUuid(uuid)
if (!homeCom) {
throw new LogError('HomeCommunity with uuid not found: ', uuid)
}
if (homeCom.foreign) {
throw new LogError('Error: Only the HomeCommunity could be modified!')
}
if (homeCom.gmsApiKey !== gmsApiKey) {
homeCom.gmsApiKey = gmsApiKey
await DbCommunity.save(homeCom)
}
return new Community(homeCom)
}
}