Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
intrn(query): make query pipe to filter messages by kind(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 15, 2022
1 parent b849af8 commit accdd75
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions database/queries/chat_message/sift_by_kinds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { ChatMessageKindFilter } from "$/types/query"
import type { FindOptions } from "%/types/dependent"

import Log from "$!/singletons/log"

import Condition from "%/managers/helpers/condition"
import isUndefined from "$/type_guards/is_undefined"

/**
* Sift chat message model by kinds
*/
export default function<T>(
currentState: FindOptions<T>,
constraints: ChatMessageKindFilter|Partial<ChatMessageKindFilter>
): FindOptions<T> {
const newState = { ...currentState }

if (
isUndefined(constraints.filter)
|| isUndefined(constraints.filter.chatMessageKinds)
|| constraints.filter.chatMessageKinds === "*"
) {
return currentState
}

const condition = new Condition()
condition.or(
...constraints.filter.chatMessageKinds.map(
kind => new Condition().equal("kind", kind)
)
)

if (isUndefined(newState.where)) {
newState.where = {}
}

newState.where = new Condition().and(
new Condition(newState.where),
condition
).build()

Log.trace("query pipe", "sift by kinds")

return newState
}

0 comments on commit accdd75

Please sign in to comment.