Skip to content
Snippets Groups Projects
agent.resolver.ts 2.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {
        Args,
        Context,
        Parent,
        Query,
        ResolveField,
        Resolver,
    } from '@nestjs/graphql';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    import { Agent } from './models/agent.model';
    import { PrismaService } from '../prisma/prisma.service';
    import { GraphQLString } from 'graphql';
    
    import { NotFoundException, UseGuards } from '@nestjs/common';
    
    import { GroupAuthGuard } from '../auth/group-auth.guard';
    
    import { Request } from 'express';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
    @Resolver(() => Agent)
    export class AgentResolver {
    
    Pascal Kosak's avatar
    Pascal Kosak committed
        constructor(private prismaService: PrismaService) {}
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
        @Query(() => [Agent])
    
        @UseGuards(GroupAuthGuard)
    
        listAgents() {
    
            return this.prismaService.agent.findMany({
    
                where: {
                    NOT: { uid: null },
                },
    
    Pascal Kosak's avatar
    Pascal Kosak committed
        }
    
        @Query(() => Agent)
        async getAgent(
            @Args({ name: 'code', type: () => GraphQLString }) code: string,
        ) {
            const agent = await this.prismaService.agent.findFirst({
                where: { slug: code },
    
    Pascal Kosak's avatar
    Pascal Kosak committed
            });
    
    
            if (!agent) throw new NotFoundException('That Agent does not exist');
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
            return agent;
        }
    
        @ResolveField('entries')
    
        @UseGuards(GroupAuthGuard)
    
        async entries(
            @Parent() entry: Agent,
            @Context() { req }: { req: Request },
        ) {
    
            const unlocks = await this.prismaService.group
                .findFirst({
                    where: { id: req.group.id },
                    select: {
                        unlocks: true,
                    },
                })
                .then((value) => value.unlocks)
                .then((unlocks) => unlocks.map((value) => value.id));
    
    Pascal Kosak's avatar
    Pascal Kosak committed
            const result = await this.prismaService.agent.findFirst({
    
                where: {
                    id: entry.id,
                },
    
                    entries: true,
                },
    
    Pascal Kosak's avatar
    Pascal Kosak committed
            });
    
    
            return result.entries.map((entry) => {
    
                if (!entry.private || unlocks.includes(entry.id))
                    return {
                        ...entry,
                        locked: false,
                    };
    
                return {
                    id: entry.id,
                    agentId: entry.agentId,
                    private: true,
                    createdAt: entry.createdAt,
                    locked: true,
                };
            });
    
    Pascal Kosak's avatar
    Pascal Kosak committed
        }
    }