Skip to content

Commit

Permalink
fix advert count in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Petter Andersson committed Jan 17, 2025
1 parent ef0a886 commit 450aabc
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/adverts/repository/mongo/mongo-db-adverts-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { MongoConnection } from '../../../mongodb-utils/types'
import { toMap } from '../../../lib'
import { convertObjectStream } from '../../../lib/streams'
import { createValidatingAdvertsRepository } from '../validation'
import { combineAnd, combineOr } from './filters/filter-utils'

export const createMongoAdvertsRepository = (
{ getCollection }: MongoConnection<MongoAdvert>,
Expand Down Expand Up @@ -131,13 +132,21 @@ export const createMongoAdvertsRepository = (
) => {
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#use-in--group-stage
const collection = await getCollection()

const filter = combineAnd(
excludeArchived ? { 'meta.archived': { $ne: true } } : null,
combineOr(
{ 'meta.unreservedCount': { $gt: 0 } },
{ 'advert.lendingPeriod': { $gt: 0 } }
)
)
const cursor = collection.aggregate<{ _id: string; c: number }>(
[
excludeArchived
? {
$match: { 'meta.archived': { $ne: true } },
}
: null,
{
$match: {
...filter,
},
},
{
$group: {
_id: `$advert.${by}`,
Expand Down

0 comments on commit 450aabc

Please sign in to comment.