Trouble with search filters

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.
1 Reply
Prisma AI Help
Prisma AI Help•3w ago
You selected the bot's instantaneous insights. Let me compile your answer right away! 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 },
};
}
}
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. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.

Did you find this page helpful?