Newer
Older
import {
Args,
Context,
Mutation,
Parent,
ResolveField,
Resolver,
import { Entry } from './models/entry.model';
import { PrismaService } from '../prisma/prisma.service';
import { GraphQLString } from 'graphql';
import {
BadRequestException,
NotFoundException,
UnauthorizedException,
import { Request } from 'express';
import { GroupAuthGuard } from '../auth/group-auth.guard';
import { pubSub } from '../pubSub.instance';
@ResolveField('agent')
async agent(@Parent() entry: Entry) {
const result = await this.prismaService.entry.findFirst({
where: { id: entry.id },
select: { agent: true },
});
@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({
id,
private: true,
},
});
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: {
unlocks: {
some: { id },
},
},
});
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,
});
const newEntry = {
...unlock,
locked: false,
};
await pubSub.publish('newEntry', {
newEntry: {
...newEntry,
groupSlug: group.slug,
},
});
return newEntry;