There are two tables sim_card and observation 1 to 1. It is necessary to select all SIM cards with the status Active (true) and which are not used in the observation table. (That is, an observation card is created and a free and active SIM card is issued to it.)
model observation {
id Int @id @unique @default(autoincrement()) @map("observation_id")
number Int
contract String?
sim_card sim_card? @relation(fields: [sim_cardId], references: [id], map: "ObservationId_sim_card_key")
sim_cardId Int? @unique(map: "ObservationId_sim_card_key")
firmId Int? @unique(map: "Observation_firmId_key")
client client? @relation(fields: [firmId], references: [id], map: "Observation_firmId_key")
}
model sim_card {
id Int @id @default(autoincrement())
number String @unique(map: "Sim_card_number_key")
operator String @default("kyivstar")
active Boolean @default(true)
busy Boolean @default(false)
observationId Int?
}
As I imagine, (but it doesn't work)
findActive(): Promise<CreateSimCardDto[]> {
return Promise
.resolve(this.prismaService.sim_card
.findMany({
where: {
active: true
},
include: {
observation: {
select: {
sim_card: false
}
}
}
}))
.catch((error) => {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
})
}