Skip to content

Commit 122ca52

Browse files
tech(api): make campaign participation repository domain transaction compliant
1 parent b2bd187 commit 122ca52

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

api/src/prescription/campaign-participation/infrastructure/repositories/campaign-participation-repository.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import lodash from 'lodash';
22

3-
import { knex } from '../../../../../db/knex-database-connection.js';
43
import { constants } from '../../../../shared/domain/constants.js';
54
import { DomainTransaction } from '../../../../shared/domain/DomainTransaction.js';
65
import { NotFoundError } from '../../../../shared/domain/errors.js';
@@ -119,7 +118,8 @@ const getAllCampaignParticipationsInCampaignForASameLearner = async function ({
119118
};
120119

121120
const getCampaignParticipationsForOrganizationLearner = async function ({ organizationLearnerId, campaignId }) {
122-
const campaignParticipations = await knex('campaign-participations')
121+
const knexConn = DomainTransaction.getConnection();
122+
const campaignParticipations = await knexConn('campaign-participations')
123123
.where({
124124
campaignId,
125125
organizationLearnerId,
@@ -139,7 +139,8 @@ const remove = async function ({ id, deletedAt, deletedBy }) {
139139
};
140140

141141
const findProfilesCollectionResultDataByCampaignId = async function (campaignId) {
142-
const results = await knex('campaign-participations')
142+
const knexConn = DomainTransaction.getConnection();
143+
const results = await knexConn('campaign-participations')
143144
.select([
144145
'campaign-participations.*',
145146
'view-active-organization-learners.studentNumber',
@@ -163,11 +164,12 @@ const findProfilesCollectionResultDataByCampaignId = async function (campaignId)
163164
};
164165

165166
const findOneByCampaignIdAndUserId = async function ({ campaignId, userId }) {
166-
const campaignParticipation = await knex('campaign-participations')
167+
const knexConn = DomainTransaction.getConnection();
168+
const campaignParticipation = await knexConn('campaign-participations')
167169
.where({ userId, isImproved: false, campaignId })
168170
.first();
169171
if (!campaignParticipation) return null;
170-
const assessments = await knex('assessments').where({ campaignParticipationId: campaignParticipation.id });
172+
const assessments = await knexConn('assessments').where({ campaignParticipationId: campaignParticipation.id });
171173
const campaign = await campaignRepository.get(campaignId);
172174
return new CampaignParticipation({
173175
...campaignParticipation,
@@ -182,7 +184,8 @@ const findOneByCampaignIdAndUserId = async function ({ campaignId, userId }) {
182184
};
183185

184186
const hasAssessmentParticipations = async function (userId) {
185-
const { count } = await knex('assessments')
187+
const knexConn = DomainTransaction.getConnection();
188+
const { count } = await knexConn('assessments')
186189
.count('assessments.id')
187190
.leftJoin('campaign-participations', 'campaignParticipationId', 'campaign-participations.id')
188191
.leftJoin('campaigns', 'campaigns.id', 'campaignId')
@@ -223,13 +226,14 @@ const getCodeOfLastParticipationToProfilesCollectionCampaignForUser = async func
223226
};
224227

225228
const isRetrying = async function ({ campaignParticipationId }) {
226-
const { id: campaignId, userId } = await knex('campaigns')
229+
const knexConn = DomainTransaction.getConnection();
230+
const { id: campaignId, userId } = await knexConn('campaigns')
227231
.select('campaigns.id', 'userId')
228232
.join('campaign-participations', 'campaigns.id', 'campaignId')
229233
.where({ 'campaign-participations.id': campaignParticipationId })
230234
.first();
231235

232-
const campaignParticipations = await knex('campaign-participations')
236+
const campaignParticipations = await knexConn('campaign-participations')
233237
.select('sharedAt', 'isImproved')
234238
.where({ campaignId, userId });
235239

0 commit comments

Comments
 (0)