Skip to content

Commit 0aa62e7

Browse files
tech(api): make answer repository domain transaction compliant
1 parent 42e6d7d commit 0aa62e7

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

api/src/shared/infrastructure/repositories/answer-repository.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import jsYaml from 'js-yaml';
22
import _ from 'lodash';
33

4-
import { knex } from '../../../../db/knex-database-connection.js';
54
import { Answer } from '../../../evaluation/domain/models/Answer.js';
65
import { DomainTransaction } from '../../domain/DomainTransaction.js';
76
import { ChallengeAlreadyAnsweredError, NotFoundError } from '../../domain/errors.js';
@@ -38,7 +37,8 @@ const COLUMNS = Object.freeze([
3837
]);
3938

4039
const get = async function (id) {
41-
const answerDTO = await knex.select(COLUMNS).from('answers').where({ id }).first();
40+
const knexConn = DomainTransaction.getConnection();
41+
const answerDTO = await knexConn.select(COLUMNS).from('answers').where({ id }).first();
4242

4343
if (!answerDTO) {
4444
throw new NotFoundError(`Not found answer for ID ${id}`);
@@ -48,7 +48,8 @@ const get = async function (id) {
4848
};
4949

5050
const findByChallengeAndAssessment = async function ({ challengeId, assessmentId }) {
51-
const answerDTO = await knex
51+
const knexConn = DomainTransaction.getConnection();
52+
const answerDTO = await knexConn
5253
.select(COLUMNS)
5354
.from('answers')
5455
.where({ challengeId, assessmentId })
@@ -63,14 +64,16 @@ const findByChallengeAndAssessment = async function ({ challengeId, assessmentId
6364
};
6465

6566
const findByAssessment = async function (assessmentId) {
66-
const answerDTOs = await knex.select(COLUMNS).from('answers').where({ assessmentId }).orderBy('createdAt');
67+
const knexConn = DomainTransaction.getConnection();
68+
const answerDTOs = await knexConn.select(COLUMNS).from('answers').where({ assessmentId }).orderBy('createdAt');
6769
const answerDTOsWithoutDuplicate = _.uniqBy(answerDTOs, 'challengeId');
6870

6971
return _toDomainArray(answerDTOsWithoutDuplicate);
7072
};
7173

7274
const findByAssessmentExcludingChallengeIds = async function ({ assessmentId, excludedChallengeIds = [] }) {
73-
const answerDTOs = await knex
75+
const knexConn = DomainTransaction.getConnection();
76+
const answerDTOs = await knexConn
7477
.with('all-first-answers', (qb) => {
7578
qb.select('*')
7679
.distinctOn('challengeId', 'assessmentId')
@@ -86,7 +89,8 @@ const findByAssessmentExcludingChallengeIds = async function ({ assessmentId, ex
8689
};
8790

8891
const findChallengeIdsFromAnswerIds = async function (ids) {
89-
return knex.distinct().pluck('challengeId').from('answers').whereInArray('id', ids);
92+
const knexConn = DomainTransaction.getConnection();
93+
return knexConn.distinct().pluck('challengeId').from('answers').whereInArray('id', ids);
9094
};
9195

9296
const save = async function ({ answer }) {

0 commit comments

Comments
 (0)