Skip to content
Snippets Groups Projects
entry.resolver.ts 3.05 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pascal Kosak's avatar
    Pascal Kosak committed
    import {
        Args,
        Context,
        Mutation,
        Parent,
        ResolveField,
        Resolver,
    
        Subscription,
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    } from '@nestjs/graphql';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    import { Entry } from './models/entry.model';
    import { PrismaService } from '../prisma/prisma.service';
    
    import { GraphQLString } from 'graphql';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    import {
        BadRequestException,
        NotFoundException,
    
    Pascal Kosak's avatar
    Pascal Kosak committed
        UseGuards,
    } from '@nestjs/common';
    
    import { Request } from 'express';
    
    import { GroupAuthGuard } from '../auth/group-auth.guard';
    
    import { pubSub } from '../pubSub.instance';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
    @Resolver(() => Entry)
    export class EntryResolver {
    
    Pascal Kosak's avatar
    Pascal Kosak committed
        constructor(private prismaService: PrismaService) {}
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
        @ResolveField('agent')
        async agent(@Parent() entry: Entry) {
            const result = await this.prismaService.entry.findFirst({
                where: { id: entry.id },
                select: { agent: true },
            });
    
    Pascal Kosak's avatar
    Pascal Kosak committed
            result.agent.slug = '';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
            return result.agent;
        }
    
        @Subscription(() => Entry, {
            filter: (payload, variables) =>
                !payload.newEntry.groupSlug ||
                payload.newEntry.groupSlug === variables.groupSlug,
        })
        async newEntry(
            @Args({ name: 'groupSlug', type: () => GraphQLString })
            groupSlug: string,
        ) {
            if (!groupSlug) throw new UnauthorizedException();
    
            const group = await this.prismaService.group.findFirst({
                where: {
                    slug: groupSlug,
                },
            });
    
            if (!group) throw new NotFoundException('That Group does not exist');
    
    
            return pubSub.asyncIterator('newEntry');
        }
    
    
        @UseGuards(GroupAuthGuard)
    
        @Mutation(() => Entry)
        async unlockEntry(
            @Args({ name: 'id', type: () => GraphQLString }) id: string,
            @Context() { req }: { req: Request },
        ) {
            const unlock = await this.prismaService.entry.findFirst({
    
    Pascal Kosak's avatar
    Pascal Kosak committed
                where: {
    
            if (!unlock) throw new NotFoundException('That entry does not exist');
    
    
            if (req.group.tokens <= 0)
                throw new BadRequestException('No remaining tokens');
    
    
            const count = await this.prismaService.group.count({
                where: {
    
            if (count > 0)
                throw new BadRequestException('Entry is already unlocked');
    
            const group = await this.prismaService.group.update({
    
                where: { id: req.group.id },
                data: {
                    tokens: req.group.tokens - 1,
                    unlocks: {
                        connect: { id },
                    },
                },
            });
    
    
            await pubSub.publish('updateToken', {
                updateToken: group,
            });
    
    
            await pubSub.publish('newEntry', {
                newEntry: {
                    ...newEntry,
                    groupSlug: group.slug,
                },
            });
    
            return newEntry;
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    }