#Trouble with search filters

3 messages · Page 1 of 1 (latest)

tender otter
#

let where: Prisma.ClinicWhereInput = baseWhere;

if (diseaseTreatment && diseaseTreatment.specialization) {
const specializations = diseaseTreatment.specialization.includes(",")
? diseaseTreatment.specialization.split(",").map(s => s.trim())
: [diseaseTreatment.specialization];

if (diseaseTreatment.treatmentCategory === "GP") {
where = {
...baseWhere,
OR: [
{ category: { equals: diseaseTreatment.treatmentCategory } },
{ specialization: { in: specializations } },
],
};
} else if (diseaseTreatment.treatmentCategory === "Specialist") {
where = {
...baseWhere,
specialization: { in: specializations },
};
}
}

For some reason, the search filters are not working, as it doesnt filter the clinics successfully.

dense forgeBOT
#

You selected the bot's instantaneous insights. Let me compile your answer right away!

dense forgeBOT
# dense forge You selected the bot's instantaneous insights. Let me compile your answer right ...

Based on the knowledge sources, I can see a potential issue with your OR filter structure.

The problem is likely that when you spread baseWhere and then add the OR condition, the top-level properties are being combined with AND logic. According to the Prisma documentation on OR operators, top-level keys of where are always joined with AND, while elements inside the OR array are joined with OR.

Here's how to fix your code:

let where: Prisma.ClinicWhereInput = baseWhere;

if (diseaseTreatment && diseaseTreatment.specialization) {
  const specializations = diseaseTreatment.specialization.includes(",")
    ? diseaseTreatment.specialization.split(",").map(s => s.trim())
    : [diseaseTreatment.specialization];
  
  if (diseaseTreatment.treatmentCategory === "GP") {
    where = {
      AND: [
        baseWhere,
        {
          OR: [
            { category: { equals: diseaseTreatment.treatmentCategory } },
            { specialization: { in: specializations } },
          ],
        },
      ],
    };
  } else if (diseaseTreatment.treatmentCategory === "Specialist") {
    where = {
      ...baseWhere,
      specialization: { in: specializations },
    };
  }
}

The key change is wrapping baseWhere and the OR condition inside an AND array. This ensures that:

  1. All conditions from baseWhere are applied
  2. The OR conditions work correctly (either category equals "GP" OR specialization is in the list)

This pattern is demonstrated in the Prisma Client reference for combining AND and OR.