Return records based on a condition

I need to make a query that verify:

if type_answer is equal to Multipla Escolha so i only have to return the records that are not null in correct_answer_description_id
If the type_answer is not equal to Multipla Escolha, don’t make this rule.

So, i try this way:

async getIdQuestoesDisponveis(idUnidade) {

  return await BookUnitQuestion.query()

  .where(function(){

   this

    .where("student_id", idEstudante)

    .where("book_unit_id", idUnidade)

    .where("status", false)

    .whereNotNull("correct_answer_description_id")

    .where("type_answer", "Multipla Escolha")

    .ids();

  })

 }

But i’m getting a infinite loop.
How i can make this?

In sql, i can this result by this query:

SELECT * FROM book_unit_question
WHERE book_unit_id = 2 
AND status = false
AND (type_answer is null or type_answer != 'Multipla Escolha' or
correct_answer_description_id IS NOT NULL)

I don’t know if is the best way, but i fixed this way:

return await Database

   .query()

   .select('id')

   .from('book_unit_question')

   .where('book_unit_id', idUnidade)

   .where('status', false)

   .whereRaw('(type_answer is null or type_answer != ? or correct_answer_description_id IS NOT NULL)', ['Multipla Escolha'])

   .pluck('id')
1 Like