1
+ import lodash from 'lodash' ;
2
+
1
3
import { knex } from '../../../../../db/knex-database-connection.js' ;
2
4
import { NotFoundError } from '../../../../../lib/domain/errors.js' ;
3
5
import { Campaign } from '../../../../../lib/domain/models/Campaign.js' ;
@@ -9,6 +11,21 @@ import { ApplicationTransaction } from '../../../shared/infrastructure/Applicati
9
11
import { CampaignParticipation } from '../../domain/models/CampaignParticipation.js' ;
10
12
import { AvailableCampaignParticipation } from '../../domain/read-models/AvailableCampaignParticipation.js' ;
11
13
14
+ const { pick } = lodash ;
15
+
16
+ import { CampaignParticipationStatuses } from '../../../shared/domain/constants.js' ;
17
+
18
+ const CAMPAIGN_PARTICIPATION_ATTRIBUTES = [
19
+ 'participantExternalId' ,
20
+ 'sharedAt' ,
21
+ 'status' ,
22
+ 'campaignId' ,
23
+ 'userId' ,
24
+ 'organizationLearnerId' ,
25
+ 'deletedAt' ,
26
+ 'deletedBy' ,
27
+ ] ;
28
+
12
29
const updateWithSnapshot = async function ( campaignParticipation ) {
13
30
const domainTransaction = ApplicationTransaction . getTransactionAsDomainTransaction ( ) ;
14
31
await this . update ( campaignParticipation ) ;
@@ -29,16 +46,13 @@ const updateWithSnapshot = async function (campaignParticipation) {
29
46
const update = async function ( campaignParticipation , domainTransaction ) {
30
47
const knexConn = ApplicationTransaction . getConnection ( domainTransaction ) ;
31
48
32
- const attributes = {
33
- participantExternalId : campaignParticipation . participantExternalId ,
34
- sharedAt : campaignParticipation . sharedAt ,
35
- status : campaignParticipation . status ,
36
- campaignId : campaignParticipation . campaignId ,
37
- userId : campaignParticipation . userId ,
38
- organizationLearnerId : campaignParticipation . organizationLearnerId ,
39
- } ;
49
+ await knexConn ( 'campaign-participations' )
50
+ . where ( { id : campaignParticipation . id } )
51
+ . update ( pick ( campaignParticipation , CAMPAIGN_PARTICIPATION_ATTRIBUTES ) ) ;
52
+ } ;
40
53
41
- await knexConn ( 'campaign-participations' ) . where ( { id : campaignParticipation . id } ) . update ( attributes ) ;
54
+ const batchUpdate = async function ( campaignParticipations ) {
55
+ return Promise . all ( campaignParticipations . map ( ( campaignParticipation ) => update ( campaignParticipation ) ) ) ;
42
56
} ;
43
57
44
58
const get = async function ( id , domainTransaction ) {
@@ -55,6 +69,14 @@ const get = async function (id, domainTransaction) {
55
69
} ) ;
56
70
} ;
57
71
72
+ const getByCampaignIds = async function ( campaignIds ) {
73
+ const knexConn = ApplicationTransaction . getConnection ( ) ;
74
+ const campaignParticipations = await knexConn ( 'campaign-participations' )
75
+ . whereNull ( 'deletedAt' )
76
+ . whereIn ( 'campaignId' , campaignIds ) ;
77
+ return campaignParticipations . map ( ( campaignParticipation ) => new CampaignParticipation ( campaignParticipation ) ) ;
78
+ } ;
79
+
58
80
const getAllCampaignParticipationsInCampaignForASameLearner = async function ( { campaignId, campaignParticipationId } ) {
59
81
const knexConn = DomainTransaction . getConnection ( ) ;
60
82
const result = await knexConn ( 'campaign-participations' )
@@ -99,9 +121,53 @@ const remove = async function ({ id, deletedAt, deletedBy }) {
99
121
return await knexConn ( 'campaign-participations' ) . where ( { id } ) . update ( { deletedAt, deletedBy } ) ;
100
122
} ;
101
123
124
+ const findProfilesCollectionResultDataByCampaignId = async function ( campaignId ) {
125
+ const results = await knex ( 'campaign-participations' )
126
+ . select ( [
127
+ 'campaign-participations.*' ,
128
+ 'view-active-organization-learners.studentNumber' ,
129
+ 'view-active-organization-learners.division' ,
130
+ 'view-active-organization-learners.group' ,
131
+ 'view-active-organization-learners.firstName' ,
132
+ 'view-active-organization-learners.lastName' ,
133
+ ] )
134
+ . join (
135
+ 'view-active-organization-learners' ,
136
+ 'view-active-organization-learners.id' ,
137
+ 'campaign-participations.organizationLearnerId' ,
138
+ )
139
+ . where ( { campaignId, 'campaign-participations.deletedAt' : null } )
140
+ . orderBy ( 'lastName' , 'ASC' )
141
+ . orderBy ( 'firstName' , 'ASC' )
142
+ . orderBy ( 'createdAt' , 'DESC' ) ;
143
+
144
+ return results . map ( _rowToResult ) ;
145
+ } ;
146
+
147
+ function _rowToResult ( row ) {
148
+ return {
149
+ id : row . id ,
150
+ createdAt : new Date ( row . createdAt ) ,
151
+ isShared : row . status === CampaignParticipationStatuses . SHARED ,
152
+ sharedAt : row . sharedAt ? new Date ( row . sharedAt ) : null ,
153
+ participantExternalId : row . participantExternalId ,
154
+ userId : row . userId ,
155
+ isCompleted : row . state === 'completed' ,
156
+ studentNumber : row . studentNumber ,
157
+ participantFirstName : row . firstName ,
158
+ participantLastName : row . lastName ,
159
+ division : row . division ,
160
+ pixScore : row . pixScore ,
161
+ group : row . group ,
162
+ } ;
163
+ }
164
+
102
165
export {
166
+ batchUpdate ,
167
+ findProfilesCollectionResultDataByCampaignId ,
103
168
get ,
104
169
getAllCampaignParticipationsInCampaignForASameLearner ,
170
+ getByCampaignIds ,
105
171
getCampaignParticipationsForOrganizationLearner ,
106
172
remove ,
107
173
update ,
0 commit comments