Skip to content

Commit

Permalink
added Category.unarchivedAdvertCount for graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed Jul 4, 2024
1 parent 1e04c64 commit 4a1ede7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
13 changes: 11 additions & 2 deletions src/adverts/repository/fs/fs-adverts-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,19 @@ export const createFsAdvertsRepository = (
return null
}

const countBy: AdvertsRepository['countBy'] = async (user, by) =>
const countBy: AdvertsRepository['countBy'] = async (
user,
by,
excludeArchived
) =>
list(user).then(advertList =>
mapValues(
toLookup(advertList.adverts, advert => advert[by]),
toLookup(
advertList.adverts.filter(a =>
excludeArchived ? !a.archivedAt : true
),
advert => advert[by]
),
l => l.length
)
)
Expand Down
5 changes: 4 additions & 1 deletion src/adverts/repository/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export const createInMemoryAdvertsRepository = (
}
return null
},
countBy: async (user, by) =>
countBy: async (user, by, excludeArchived) =>
Object.values(db).reduce<Record<string, number>>((s, advert) => {
const v = (advert[by] || '').toString()
if (excludeArchived && advert.archivedAt) {
return s
}
// eslint-disable-next-line no-param-reassign
s[v] = (s[v] || 0) + 1
return s
Expand Down
35 changes: 24 additions & 11 deletions src/adverts/repository/mongo/mongo-db-adverts-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,34 @@ export const createMongoAdvertsRepository = (
return result.modifiedCount > 0 ? advert : null
}

const countBy: AdvertsRepository['countBy'] = async (user, by) => {
const countBy: AdvertsRepository['countBy'] = async (
user,
by,
excludeArchived
) => {
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#use-in--group-stage
const collection = await getCollection()
const cursor = collection.aggregate<{ _id: string; c: number }>([
{
$group: {
_id: `$advert.${by}`,
c: {
// NOTE: $count operator doesnt work on Mongo 4.*. $sum: 1 i equivalent
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#mongodb-group-grp.-count
$sum: 1,
const cursor = collection.aggregate<{ _id: string; c: number }>(
[
excludeArchived
? {
$match: { 'meta.archived': { $ne: true } },
}
: null,
{
$group: {
_id: `$advert.${by}`,
c: {
// NOTE: $count operator doesnt work on Mongo 4.*. $sum: 1 i equivalent
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#mongodb-group-grp.-count
$sum: 1,
},
},
},
},
])
]
.filter(v => v)
.map(v => v!)
)
const rows = await cursor.toArray()
return toMap(
rows,
Expand Down
3 changes: 2 additions & 1 deletion src/adverts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ export interface AdvertsRepository {
// return count of adverts per value of selected property
countBy: (
user: HaffaUser,
by: keyof Only<Advert, string>
by: keyof Only<Advert, string>,
excludeArchived?: boolean
// by: keyof Extract<Advert, string>
) => Promise<Record<string, number>>
getAdvertsByClaimStatus: (filter: AdvertsClaimFilter) => Promise<string[]>
Expand Down
7 changes: 7 additions & 0 deletions src/categories/categories-gql-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export const createCategoriesGqlModule = ({
)
return summary[source.id] || 0
},
unarchivedAdvertCount: async ({ source, ctx, cache }) => {
const summary = await cache.getOrCreateCachedValue(
'unarchived-adverts-category-summary',
() => adverts.countBy(ctx.user, 'category', true)
)
return summary[source.id] || 0
},
},
Query: {
// https://www.graphql-tools.com/docs/resolvers
Expand Down
1 change: 1 addition & 0 deletions src/categories/categories.gql.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const categoriesGqlSchema = /* GraphQL */ `
co2kg: Int
valueByUnit: Int
advertCount: Int!
unarchivedAdvertCount: Int!
}
input CategoryInput {
Expand Down

0 comments on commit 4a1ede7

Please sign in to comment.