Skip to content

[TECH] arrêter d'utiliser les champs userId / snappedAt (Pix-17286) #11965

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

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ import { buildUser } from './build-user.js';

const buildKnowledgeElementSnapshot = function ({
id = databaseBuffer.getNextId(),
userId,
snappedAt = new Date('2020-01-01'),
snapshot,
campaignParticipationId,
userId,
} = {}) {
const dateMinusOneDay = new Date(snappedAt.getTime() - 1000 * 60 * 60 * 24 * 7);
userId = _.isUndefined(userId) ? buildUser().id : userId;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: on peut conserver le userId pour la construction des knowledgeElement

if (!snapshot) {
const knowledgeElements = [];
knowledgeElements.push(buildKnowledgeElement({ userId, createdAt: dateMinusOneDay }));
knowledgeElements.push(buildKnowledgeElement({ userId, createdAt: dateMinusOneDay }));
userId = _.isUndefined(userId) ? buildUser().id : userId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought : avons nous besoin de lodash pour des questions de lisibilité ou est ce que l'écriture userId === undefined pourrait répondre au besoin ?

knowledgeElements.push(buildKnowledgeElement({ userId, createdAt: new Date('2020-01-01') }));
knowledgeElements.push(buildKnowledgeElement({ userId, createdAt: new Date('2020-01-01') }));
snapshot = new KnowledgeElementCollection(knowledgeElements).toSnapshot();
}

const values = {
id,
userId,
snappedAt,
snapshot,
campaignParticipationId,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { databaseBuffer } from '../database-buffer.js';
import { buildKnowledgeElement } from './build-knowledge-element.js';

function buildSnapshot({ id, userId, snappedAt, knowledgeElementsAttributes, campaignParticipationId }) {
function buildSnapshot({ id, knowledgeElementsAttributes, campaignParticipationId }) {
const knowledgeElements = knowledgeElementsAttributes.map((attributes) => buildKnowledgeElement(attributes));

const values = {
id,
userId,
snappedAt,
snapshot: JSON.stringify(knowledgeElements),
campaignParticipationId,
};
Expand Down
4 changes: 0 additions & 4 deletions api/db/seeds/data/common/tooling/campaign-tooling.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ async function createAssessmentCampaign({

if (isShared) {
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: sharedAt,
snapshot: new KnowledgeElementCollection(keDataForSnapshot).toSnapshot(),
campaignParticipationId,
});
Expand Down Expand Up @@ -402,8 +400,6 @@ async function createProfilesCollectionCampaign({
);
}
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: sharedAt,
snapshot: new KnowledgeElementCollection(keDataForSnapshot).toSnapshot(),
campaignParticipationId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,17 @@ function _validateAndNormalizeArgs({ concurrency, maxSnapshotCount }) {

async function getEligibleCampaignParticipations(maxSnapshotCount) {
return knex('campaign-participations')
.select('campaign-participations.userId', 'campaign-participations.sharedAt', 'campaign-participations.id')
.leftJoin('knowledge-element-snapshots', function () {
this.on('knowledge-element-snapshots.userId', 'campaign-participations.userId').andOn(
'knowledge-element-snapshots.snappedAt',
'campaign-participations.sharedAt',
);
})
.select('campaign-participations.id', 'campaign-participations.userId', 'campaign-participations.sharedAt')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question Est ce qu'on a encore besoin de ce script (on pourrait le supprimer si il n'est plus utile)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je crois qu'on l'a laissé parce qu'il pourrait nous resservir

.leftJoin(
'knowledge-element-snapshots',
'knowledge-element-snapshots.campaignParticipationId',
'campaign-participations.id',
)
.whereNotNull('campaign-participations.sharedAt')
.where((qb) => {
qb.whereNull('knowledge-element-snapshots.snappedAt').orWhereRaw('?? != ??', [
'campaign-participations.sharedAt',
'knowledge-element-snapshots.snappedAt',
]);
qb.whereNull('knowledge-element-snapshots.campaignParticipationId');
})
.orderBy('campaign-participations.userId')
.orderBy('campaign-participations.id')
.limit(maxSnapshotCount);
}

Expand All @@ -79,8 +75,6 @@ async function generateKnowledgeElementSnapshots(
});
try {
await dependencies.knowledgeElementSnapshotRepository.save({
userId,
snappedAt: sharedAt,
snapshot: new KnowledgeElementCollection(knowledgeElements).toSnapshot(),
campaignParticipationId: id,
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ const updateWithSnapshot = async function (campaignParticipation) {
limitDate: campaignParticipation.sharedAt,
});
await knowledgeElementSnapshotRepository.save({
userId: campaignParticipation.userId,
snappedAt: campaignParticipation.sharedAt,
snapshot: new KnowledgeElementCollection(knowledgeElements).toSnapshot(),
campaignParticipationId: campaignParticipation.id,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { KnowledgeElementSnapshot } from './models/KnowledgeElementSnapshot.js';
*/
export async function save(knowledgeElementSnapshotPayload) {
await usecases.saveKnowledgeElementSnapshotForParticipation({
userId: knowledgeElementSnapshotPayload.userId,
snappedAt: new Date(),
knowledgeElementCollection: new KnowledgeElementCollection(knowledgeElementSnapshotPayload.knowledgeElements),
campaignParticipationId: knowledgeElementSnapshotPayload.campaignParticipationId,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { withTransaction } from '../../../../shared/domain/DomainTransaction.js';

export const saveKnowledgeElementSnapshotForParticipation = withTransaction(async function ({
userId,
snappedAt,
knowledgeElementCollection,
campaignParticipationId,
knowledgeElementSnapshotRepository,
}) {
await knowledgeElementSnapshotRepository.save({
userId,
snappedAt,
snapshot: knowledgeElementCollection.toSnapshot(),
campaignParticipationId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DomainTransaction } from '../../../../shared/domain/DomainTransaction.j
import { KnowledgeElement } from '../../../../shared/domain/models/KnowledgeElement.js';
import { CampaignParticipationKnowledgeElementSnapshots } from '../../../shared/domain/read-models/CampaignParticipationKnowledgeElementSnapshots.js';

export async function save({ userId, snappedAt, snapshot, campaignParticipationId }) {
export async function save({ snapshot, campaignParticipationId }) {
const knexConn = DomainTransaction.getConnection();
const existingSnapshot = await knexConn
.select('id')
Expand All @@ -12,15 +12,11 @@ export async function save({ userId, snappedAt, snapshot, campaignParticipationI
if (existingSnapshot) {
return await knexConn('knowledge-element-snapshots')
.update({
userId,
snappedAt,
snapshot,
})
.where('campaignParticipationId', campaignParticipationId);
} else {
return await knexConn('knowledge-element-snapshots').insert({
userId,
snappedAt,
snapshot,
campaignParticipationId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class AddMissingKnowledgeElementSnapshots extends Script {
limitDate: participation.sharedAt,
});
await knowledgeElementSnapshotRepository.save({
userId: participation.userId,
snappedAt: participation.sharedAt,
snapshot: new KnowledgeElementCollection(knowledgeElements).toSnapshot(),
campaignParticipationId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ describe('Evaluation | Integration | Domain | Use Cases | get-next-challenge-for
});
const knowledgeElementsBefore = new KnowledgeElementCollection([knowledgeElement]);
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: new Date('2019-01-01'),
campaignParticipationId,
snapshot: knowledgeElementsBefore.toSnapshot(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ describe('Integration | Usecase | Handle Badge Acquisition', function () {
});
const knowledgeElementsBefore = new KnowledgeElementCollection([ke1, ke2]);
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: new Date(),
campaignParticipationId,
snapshot: knowledgeElementsBefore.toSnapshot(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ describe('Evaluation | Integration | Usecase | Handle Stage Acquisition', functi
domainBuilder.buildKnowledgeElement({ userId, skillId: 'web4', status: 'invalidated' }),
];
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: new Date(),
snapshot: new KnowledgeElementCollection(knowledgeElements).toSnapshot(),
campaignParticipationId,
});
Expand Down Expand Up @@ -449,8 +447,6 @@ describe('Evaluation | Integration | Usecase | Handle Stage Acquisition', functi
domainBuilder.buildKnowledgeElement({ userId, skillId: 'web4', status: 'invalidated' }),
];
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: new Date(),
snapshot: new KnowledgeElementCollection(knowledgeElements).toSnapshot(),
campaignParticipationId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ describe('Evaluation | Integration | Usecase | Save and correct answer for campa
});
const knowledgeElementsBefore = new KnowledgeElementCollection([knowledgeElement]);
databaseBuilder.factory.buildKnowledgeElementSnapshot({
userId,
snappedAt: new Date('2019-01-01'),
campaignParticipationId,
snapshot: knowledgeElementsBefore.toSnapshot(),
});
Expand Down
Loading