All files / src/modules/open-houses open-house.resolver.ts

81.57% Statements 31/38
60.71% Branches 17/28
70.58% Functions 12/17
80% Lines 28/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 6611x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x   22x 11x 11x     22x 11x         22x     11x 11x             22x     11x 11x             22x     11x 11x             22x   11x 11x            
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { OpenHouseService } from '@app/modules/open-houses/open-house.service';
import { JwtAuthGuard } from '@app/common/guards/jwt-auth.guard';
import { RolesGuard } from '@app/common/guards/roles.guard';
import { Roles } from '@app/common/decorators/roles.decorator';
import { Role, User } from '@prisma/client';
import { CurrentUser } from '@app/common/decorators/current-user.decorator';
import { CreateOpenHouseDto } from '@app/modules/open-houses/dto/create-open-house.dto';
import { UpdateOpenHouseDto } from '@app/modules/open-houses/dto/update-open-house.dto';
import { OpenHouseObject } from '@app/modules/open-houses/open-house.object';
 
@Resolver(() => OpenHouseObject)
export class OpenHouseResolver {
  constructor(private readonly service: OpenHouseService) {}
 
  // Queries
  @Query(() => [OpenHouseObject])
  async openHouses(@Args('listingId', { type: () => Int }) listingId: number) {
    return this.service.list(listingId);
  }
 
  // Mutations
  @Mutation(() => OpenHouseObject)
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.AGENT, Role.ADMIN)
  async createOpenHouse(
    @Args('listingId', { type: () => Int }) listingId: number,
    @Args('data') data: CreateOpenHouseDto,
    @CurrentUser() user: User,
  ) {
    return this.service.create(listingId, data, user);
  }
 
  @Mutation(() => OpenHouseObject)
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.AGENT, Role.ADMIN)
  async updateOpenHouse(
    @Args('id', { type: () => Int }) id: number,
    @Args('data') data: UpdateOpenHouseDto,
    @CurrentUser() user: User,
  ) {
    return this.service.update(id, data, user);
  }
 
  @Mutation(() => Boolean)
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.AGENT, Role.ADMIN)
  async cancelOpenHouse(
    @Args('id', { type: () => Int }) id: number,
    @CurrentUser() user: User,
  ) {
    await this.service.update(id, { status: 'CANCELLED' } as any, user);
    return true;
  }
 
  @Mutation(() => Boolean)
  @UseGuards(JwtAuthGuard)
  async registerForOpenHouse(
    @Args('id', { type: () => Int }) id: number,
    @CurrentUser() user: User,
  ) {
    await this.service.register(id, user);
    return true;
  }
}