Skip to content

Commit

Permalink
chore?(api): refacto repository to use a with clause
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBazin committed Nov 13, 2024
1 parent a8b6c88 commit 45fe735
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ async function _getParticipations(qb, campaignId, filters) {
.whereNull('campaign-participations.deletedAt')
.orderBy('sharedAt', 'desc');
})
.with('participationsCount', (qb) => {
qb.select('organizationLearnerId')
.count('organizationLearnerId AS sharedProfileCount')
.from('campaign-participations')
.groupBy('organizationLearnerId')
.where('campaignId', campaignId)
.whereNotNull('campaign-participations.sharedAt')
.whereNull('campaign-participations.deletedAt');
})
.select(
'campaign-participations.id AS campaignParticipationId',
'campaign-participations.userId AS userId',
Expand All @@ -66,13 +75,7 @@ async function _getParticipations(qb, campaignId, filters) {
'campaign-participations.pixScore AS pixScore',
'previousParticipationsInfos.previousPixScore',
'previousParticipationsInfos.previousSharedAt',
knex('campaign-participations')
.as('sharedProfileCount')
.count()
.whereRaw('"campaign-participations"."organizationLearnerId" = "view-active-organization-learners".id')
.whereNotNull('campaign-participations.sharedAt')
.whereNull('campaign-participations.deletedAt')
.where('campaign-participations.campaignId', campaignId),
'participationsCount.sharedProfileCount',
)
.distinctOn('campaign-participations.organizationLearnerId')
.from('campaign-participations')
Expand All @@ -87,6 +90,11 @@ async function _getParticipations(qb, campaignId, filters) {
'campaign-participations.organizationLearnerId',
).andOn('campaign-participations.id', '!=', 'previousParticipationsInfos.id');
})
.join(
'participationsCount',
'participationsCount.organizationLearnerId',
'campaign-participations.organizationLearnerId',
)
.where('campaign-participations.campaignId', campaignId)
.whereNull('campaign-participations.deletedAt')
.whereNotNull('campaign-participations.sharedAt')
Expand Down

0 comments on commit 45fe735

Please sign in to comment.