diff --git a/api/db/database-builder/factory/build-knowledge-element-snapshot.js b/api/db/database-builder/factory/build-knowledge-element-snapshot.js index 854e6ef788f..1a3888501e8 100644 --- a/api/db/database-builder/factory/build-knowledge-element-snapshot.js +++ b/api/db/database-builder/factory/build-knowledge-element-snapshot.js @@ -9,6 +9,7 @@ const buildKnowledgeElementSnapshot = function ({ userId, snappedAt = new Date('2020-01-01'), snapshot, + campaignParticipationId, } = {}) { const dateMinusOneDay = new Date(snappedAt.getTime() - 1000 * 60 * 60 * 24 * 7); userId = _.isUndefined(userId) ? buildUser().id : userId; @@ -24,6 +25,7 @@ const buildKnowledgeElementSnapshot = function ({ userId, snappedAt, snapshot, + campaignParticipationId, }; return databaseBuffer.pushInsertable({ diff --git a/api/db/database-builder/factory/knowledge-elements-snapshot-factory.js b/api/db/database-builder/factory/knowledge-elements-snapshot-factory.js index d173d79eb95..d6b6c896cf7 100644 --- a/api/db/database-builder/factory/knowledge-elements-snapshot-factory.js +++ b/api/db/database-builder/factory/knowledge-elements-snapshot-factory.js @@ -1,7 +1,7 @@ import { databaseBuffer } from '../database-buffer.js'; import { buildKnowledgeElement } from './build-knowledge-element.js'; -function buildSnapshot({ id, userId, snappedAt, knowledgeElementsAttributes }) { +function buildSnapshot({ id, userId, snappedAt, knowledgeElementsAttributes, campaignParticipationId }) { const knowledgeElements = knowledgeElementsAttributes.map((attributes) => buildKnowledgeElement(attributes)); const values = { @@ -9,6 +9,7 @@ function buildSnapshot({ id, userId, snappedAt, knowledgeElementsAttributes }) { userId, snappedAt, snapshot: JSON.stringify(knowledgeElements), + campaignParticipationId, }; return databaseBuffer.pushInsertable({ diff --git a/api/db/migrations/20241217162805_create-campaign-id-column-on-ke-snapshots.js b/api/db/migrations/20241217162805_create-campaign-id-column-on-ke-snapshots.js new file mode 100644 index 00000000000..f90ed92441a --- /dev/null +++ b/api/db/migrations/20241217162805_create-campaign-id-column-on-ke-snapshots.js @@ -0,0 +1,19 @@ +const TABLE_NAME = 'knowledge-element-snapshots'; +const COLUMN_NAME = 'campaignParticipationId'; + +const up = async function (knex) { + await knex.schema.alterTable(TABLE_NAME, function (table) { + table.integer(COLUMN_NAME).unsigned().nullable(); + + table.foreign(COLUMN_NAME).references('id').inTable('campaign-participations'); + }); +}; + +const down = async function (knex) { + await knex.schema.alterTable(TABLE_NAME, function (table) { + table.dropForeign(COLUMN_NAME); + table.dropColumn(COLUMN_NAME); + }); +}; + +export { down, up }; diff --git a/api/lib/infrastructure/repositories/knowledge-element-snapshot-repository.js b/api/lib/infrastructure/repositories/knowledge-element-snapshot-repository.js index a314a63ca43..2654088d41d 100644 --- a/api/lib/infrastructure/repositories/knowledge-element-snapshot-repository.js +++ b/api/lib/infrastructure/repositories/knowledge-element-snapshot-repository.js @@ -18,13 +18,14 @@ function _toKnowledgeElementCollection({ snapshot } = {}) { ); } -const save = async function ({ userId, snappedAt, knowledgeElements }) { +const save = async function ({ userId, snappedAt, knowledgeElements, campaignParticipationId }) { try { const knexConn = DomainTransaction.getConnection(); return await knexConn('knowledge-element-snapshots').insert({ userId, snappedAt, snapshot: JSON.stringify(knowledgeElements), + campaignParticipationId, }); } catch (error) { if (knexUtils.isUniqConstraintViolated(error)) { diff --git a/api/src/prescription/campaign-participation/infrastructure/repositories/campaign-participation-repository.js b/api/src/prescription/campaign-participation/infrastructure/repositories/campaign-participation-repository.js index 84d4dbe7bbf..0d4cea31655 100644 --- a/api/src/prescription/campaign-participation/infrastructure/repositories/campaign-participation-repository.js +++ b/api/src/prescription/campaign-participation/infrastructure/repositories/campaign-participation-repository.js @@ -37,6 +37,7 @@ const updateWithSnapshot = async function (campaignParticipation) { userId: campaignParticipation.userId, snappedAt: campaignParticipation.sharedAt, knowledgeElements, + campaignParticipationId: campaignParticipation.id, }); }; diff --git a/api/tests/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js b/api/tests/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js index d686036b840..b55c9db5c5c 100644 --- a/api/tests/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js +++ b/api/tests/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js @@ -10,6 +10,7 @@ describe('Integration | Repository | KnowledgeElementSnapshotRepository', functi // given const snappedAt = new Date('2019-04-01'); const userId = databaseBuilder.factory.buildUser().id; + const campaignParticipationId = databaseBuilder.factory.buildCampaignParticipation().id; const knowledgeElement1 = databaseBuilder.factory.buildKnowledgeElement({ userId, createdAt: new Date('2019-03-01'), @@ -22,12 +23,13 @@ describe('Integration | Repository | KnowledgeElementSnapshotRepository', functi await databaseBuilder.commit(); // when - await knowledgeElementSnapshotRepository.save({ userId, snappedAt, knowledgeElements }); + await knowledgeElementSnapshotRepository.save({ userId, snappedAt, knowledgeElements, campaignParticipationId }); // then const actualUserSnapshot = await knex.select('*').from('knowledge-element-snapshots').first(); expect(actualUserSnapshot.userId).to.deep.equal(userId); expect(actualUserSnapshot.snappedAt).to.deep.equal(snappedAt); + expect(actualUserSnapshot.campaignParticipationId).to.deep.equal(campaignParticipationId); const actualKnowledgeElements = []; for (const knowledgeElementData of actualUserSnapshot.snapshot) { actualKnowledgeElements.push(