First, you will need to create a custom resolver for this use case. You can create a findAvailableVanVariations query that will take fromDate and toDate as arguments.
You would start by fetching all the bookings within the given date range and then fetch all the Vans that don't have a booking in that date range. You can use the Prisma queries in your service to achieve this. Here's an example implementation:
- Create a custom DTO for your
findAvailableVanVariations query. This will include fromDate and toDate.
// find-available-van-variations.dto.ts
import { InputType, Field } from '@nestjs/graphql';
import { IsNotEmpty } from 'class-validator';
@InputType()
export class FindAvailableVanVariationsDto {
@Field()
@IsNotEmpty()
fromDate: string;
@Field()
@IsNotEmpty()
toDate: string;
}
- Create a custom resolver for this query. I assume that you are familiar with creating resolvers in NestJS+GraphQL.
// van-variation.resolver.ts
import { Resolver, Query, Args } from '@nestjs/graphql';
import { VanVariationService } from './van-variation.service';
import { FindAvailableVanVariationsDto } from './dtos/find-available-van-variations.dto';
import { VanVariation } from './entities/van-variation.entity';
@Resolver()
export class VanVariationResolver {
constructor(private readonly vanVariationService: VanVariationService) {}
@Query(() => [VanVariation], { name: 'findAvailableVanVariations' })
async findAvailableVanVariations(
@Args('findAvailableVanVariationsData')
findAvailableVanVariationsDto: FindAvailableVanVariationsDto,
): Promise<VanVariation[]> {
return this.vanVariationService.findAvailableVanVariations(
findAvailableVanVariationsDto,
);
}
}
- Create the corresponding method in your
VanVariationService that will fetch the data using Prisma.
// van-variation.service.ts
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma.service';
import { FindAvailableVanVariationsDto } from './dtos/find-available-van-variations.dto';
import { VanVariation } from './entities/van-variation.entity';
@Injectable()
export class VanVariationService {
constructor(private readonly prisma: PrismaService) {}
async findAvailableVanVariations(
findAvailableVanVariationsDto: FindAvailableVanVariationsDto,
): Promise<VanVariation[]> {
const { fromDate, toDate } = findAvailableVanVariationsDto;
const bookings = await this.prisma.booking.findMany({
where: {
OR: [
{ fromDate: { gte: new Date(fromDate), lte: new Date(toDate) } },
{ toDate: { gte: new Date(fromDate), lte: new Date(toDate) } },
],
},
select: { vanId: true }, // we only need the vans' IDs
});
const bookedVanIds = bookings.map((booking) => booking.vanId);
const availableVans = await this.prisma.van.findMany({
where: { id: { notIn: bookedVanIds } },
include: { vanVariations: true },
});
const availableVanVariationsMap = new Map<string, VanVariation>();
availableVans.forEach((van) => {
van.vanVariations.forEach((vanVariation) => {
if (!availableVanVariationsMap.has(vanVariation.id)) {
availableVanVariationsMap.set(vanVariation.id, vanVariation);
}
});
});
return Array.from(availableVanVariationsMap.values());
}
}
With this implementation, you would be able to get the available VanVariations for the given fromDate and toDate. The resolver will return a list of available VanVariations with one variation per type, and the associated van ID.