"use strict";
class PrismaOOORepository {
  constructor(prismaClient) {
    this.prismaClient = prismaClient;
  }
  async findManyOOO({
    startTimeDate,
    endTimeDate,
    allUserIds
  }) {
    return this.prismaClient.outOfOfficeEntry.findMany({
      where: {
        userId: {
          in: allUserIds
        },
        OR: [
          // outside of range
          // (start <= 'dateTo' AND end >= 'dateFrom')
          {
            start: {
              lte: endTimeDate
            },
            end: {
              gte: startTimeDate
            }
          },
          // start is between dateFrom and dateTo but end is outside of range
          // (start <= 'dateTo' AND end >= 'dateTo')
          {
            start: {
              lte: endTimeDate
            },
            end: {
              gte: endTimeDate
            }
          },
          // end is between dateFrom and dateTo but start is outside of range
          // (start <= 'dateFrom' OR end <= 'dateTo')
          {
            start: {
              lte: startTimeDate
            },
            end: {
              lte: endTimeDate
            }
          }
        ]
      },
      select: {
        id: true,
        start: true,
        end: true,
        user: {
          select: {
            id: true,
            name: true
          }
        },
        toUser: {
          select: {
            id: true,
            username: true,
            name: true
          }
        },
        reason: {
          select: {
            id: true,
            emoji: true,
            reason: true
          }
        }
      }
    });
  }
  async findUserOOODays({ userId, dateTo, dateFrom }) {
    return this.prismaClient.outOfOfficeEntry.findMany({
      where: {
        userId,
        OR: [
          // outside of range
          // (start <= 'dateTo' AND end >= 'dateFrom')
          {
            start: {
              lte: dateTo
            },
            end: {
              gte: dateFrom
            }
          },
          // start is between dateFrom and dateTo but end is outside of range
          // (start <= 'dateTo' AND end >= 'dateTo')
          {
            start: {
              lte: dateTo
            },
            end: {
              gte: dateTo
            }
          },
          // end is between dateFrom and dateTo but start is outside of range
          // (start <= 'dateFrom' OR end <= 'dateTo')
          {
            start: {
              lte: dateFrom
            },
            end: {
              lte: dateTo
            }
          }
        ]
      },
      select: {
        id: true,
        start: true,
        end: true,
        user: {
          select: {
            id: true,
            name: true
          }
        },
        toUser: {
          select: {
            id: true,
            username: true,
            name: true
          }
        },
        reason: {
          select: {
            id: true,
            emoji: true,
            reason: true
          }
        }
      }
    });
  }
}
class RoutingFormResponseRepository {
  constructor(prismaClient) {
    this.prismaClient = prismaClient;
  }
  generateCreateFormResponseData(input) {
    return {
      formId: input.formId,
      response: input.response,
      chosenRouteId: input.chosenRouteId,
      ...input.queuedFormResponseId ? {
        queuedFormResponse: {
          connect: {
            id: input.queuedFormResponseId
          }
        }
      } : {}
    };
  }
  async recordFormResponse(input) {
    return await this.prismaClient.app_RoutingForms_FormResponse.create({
      data: this.generateCreateFormResponseData(input)
    });
  }
  async recordQueuedFormResponse(input) {
    return await this.prismaClient.app_RoutingForms_QueuedFormResponse.create({
      data: this.generateCreateFormResponseData(input)
    });
  }
  async findFormResponseIncludeForm({ routingFormResponseId }) {
    return await this.prismaClient.app_RoutingForms_FormResponse.findUnique({
      where: {
        id: routingFormResponseId
      },
      select: {
        response: true,
        form: {
          select: {
            routes: true,
            fields: true
          }
        },
        chosenRouteId: true
      }
    });
  }
  async findQueuedFormResponseIncludeForm({ queuedFormResponseId }) {
    return await this.prismaClient.app_RoutingForms_QueuedFormResponse.findUnique({
      where: {
        id: queuedFormResponseId
      },
      select: {
        response: true,
        form: {
          select: {
            routes: true,
            fields: true
          }
        },
        chosenRouteId: true
      }
    });
  }
  async getQueuedFormResponseFromId(id) {
    return await this.prismaClient.app_RoutingForms_QueuedFormResponse.findUnique({
      where: {
        id
      },
      select: {
        id: true,
        formId: true,
        response: true,
        chosenRouteId: true,
        createdAt: true,
        updatedAt: true,
        actualResponseId: true,
        form: {
          select: {
            team: {
              select: {
                parentId: true
              }
            },
            user: {
              select: {
                id: true,
                email: true
              }
            },
            id: true,
            description: true,
            position: true,
            routes: true,
            createdAt: true,
            updatedAt: true,
            name: true,
            fields: true,
            updatedById: true,
            userId: true,
            teamId: true,
            disabled: true,
            settings: true
          }
        }
      }
    });
  }
}
exports.PrismaOOORepository = PrismaOOORepository;
exports.RoutingFormResponseRepository = RoutingFormResponseRepository;
