Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] campaign participation id in ke snapshots [Pix-15753] #10844

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const updateWithSnapshot = async function (campaignParticipation) {
userId: campaignParticipation.userId,
snappedAt: campaignParticipation.sharedAt,
knowledgeElements,
campaignParticipationId: campaignParticipation.id,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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(
Expand Down
Loading