Files
twenty/server/src/core/company/company-relations.resolver.ts
Jérémy M 2cd081234f chore: refacto NestJS in modules (#308)
* chore: wip refacto in modules

* fix: rollback port

* fix: jwt guard in wrong folder

* chore: rename folder exception-filter in filters

* fix: tests are running

* fix: excessive stack depth comparing types

* fix: auth issue

* chore: move createUser in UserService

* fix: test

* fix: guards

* fix: jwt guard don't handle falsy user
2023-06-16 10:38:11 +02:00

87 lines
2.3 KiB
TypeScript

import * as TypeGraphQL from '@nestjs/graphql';
import { CommentThread } from 'src/core/@generated/comment-thread/comment-thread.model';
import { Comment } from 'src/core/@generated/comment/comment.model';
import { Company } from 'src/core/@generated/company/company.model';
import { User } from 'src/core/@generated/user/user.model';
import { CompanyService } from './company.service';
import { CommentThreadService } from '../comment/services/comment-thread.service';
import { CommentService } from '../comment/services/comment.service';
@TypeGraphQL.Resolver(() => Company)
export class CompanyRelationsResolver {
constructor(
private readonly companyService: CompanyService,
private readonly commentThreadService: CommentThreadService,
private readonly commentService: CommentService,
) {}
@TypeGraphQL.ResolveField(() => User, {
nullable: true,
})
async accountOwner(
@TypeGraphQL.Parent() company: Company,
): Promise<User | null> {
return this.companyService
.findUniqueOrThrow({
where: {
id: company.id,
},
})
.accountOwner({});
}
@TypeGraphQL.ResolveField(() => [CommentThread], {
nullable: false,
})
async commentThreads(
@TypeGraphQL.Root() company: Company,
): Promise<CommentThread[]> {
return this.commentThreadService.findMany({
where: {
commentThreadTargets: {
some: {
commentableId: company.id,
commentableType: 'Company',
},
},
},
});
}
@TypeGraphQL.ResolveField(() => [Comment], {
nullable: false,
})
async comments(@TypeGraphQL.Root() company: Company): Promise<Comment[]> {
return this.commentService.findMany({
where: {
commentThread: {
commentThreadTargets: {
some: {
commentableId: company.id,
commentableType: 'Company',
},
},
},
},
});
}
@TypeGraphQL.ResolveField(() => TypeGraphQL.Int, {
nullable: false,
})
async _commentCount(@TypeGraphQL.Root() company: Company): Promise<number> {
return this.commentService.count({
where: {
commentThread: {
commentThreadTargets: {
some: {
commentableId: company.id,
commentableType: 'Company',
},
},
},
},
});
}
}