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

Feat/keep stats but remove user #105

Merged
merged 5 commits into from
Jun 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/generic/backend/src/api/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const controllers: controllersFromSchemaType<typeof apiSchema> = {
documentStatistics: buildAuthenticatedController({
permissions: ['admin', 'scrutator'],
controllerWithUser: async (_, { args: { documentNumber } }) => {
return statisticService.fetchDocumentStatistics(documentNumber);
return await statisticService.fetchDocumentStatistics(documentNumber);
},
}),

Expand Down
2 changes: 1 addition & 1 deletion packages/generic/backend/src/app/scripts/purgeDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { statisticService } from '../../modules/statistic';
export { purgeDb };

async function purgeDb({ months }: { months: number }) {
await statisticService.deleteStaticticsBefore({
await statisticService.deleteTreatmentsSummaryBefore({
since: months,
unit: 'MONTHS',
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { statisticType } from '@label/core';
import { buildFakeRepositoryBuilder } from '../../../repository';
import {
buildFakeRepositoryBuilder,
updateFakeCollection,
} from '../../../repository';
import { buildFakeRessourceFilterRequest } from '../../ressourceFilter';
import { customStatisticRepositoryType } from './customStatisticRepositoryType';

Expand Down Expand Up @@ -42,5 +45,21 @@ const buildFakeStatisticRepository = buildFakeRepositoryBuilder<
maxDate: sortedItems[sortedItems.length - 1]?.treatmentDate,
};
},

async deleteTreatmentsSummaryByIds(ids) {
let modifiedCount = 0;
updateFakeCollection(
collection,
collection.map((treatment) => {
if (ids.includes(treatment._id)) {
modifiedCount++;
return { ...treatment, treatmentsSummary: [] };
} else {
return treatment;
}
}),
);
return modifiedCount;
},
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,17 @@ const buildStatisticRepository = buildRepositoryBuilder<
maxDate: maxDateStatistics[0]?.treatmentDate,
};
},

async deleteTreatmentsSummaryByIds(ids) {
const result = await collection.updateMany(
{
_id: { $in: ids },
},
{
$unset: { 'treatmentsSummary.$[].userId': '' },
},
);
return result.modifiedCount;
},
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type customStatisticRepositoryType = {
findExtremumTreatmentDateBySources: (
sources: statisticType['source'][],
) => Promise<{ minDate: number | undefined; maxDate: number | undefined }>;
deleteTreatmentsSummaryByIds: (ids: idType[]) => Promise<number>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { dateBuilder, statisticModule } from '@label/core';
import { buildStatisticRepository } from '../repository';
import { deleteTreatmentsSummaryBefore } from './deleteTreatmentsSummaryBefore';

describe('deleteTreatmentsSummaryBefore', () => {
const statisticRepository = buildStatisticRepository();

it('should delete all the treatments summary from statistics before the given time', async () => {
const statistics = [
{
treatmentDate: dateBuilder.monthsAgo(3),
},
{
treatmentDate: dateBuilder.monthsAgo(1),
},
{
treatmentDate: dateBuilder.monthsAgo(2),
},
].map(statisticModule.generator.generate);
await Promise.all(statistics.map(statisticRepository.insert));

await deleteTreatmentsSummaryBefore({ since: 2, unit: 'MONTHS' });

const statisticsAfterDeletion = await statisticRepository.findAll();

expect(statisticsAfterDeletion[0].treatmentsSummary).toEqual([]);
expect(statisticsAfterDeletion[1].treatmentsSummary).toEqual(
statistics[1].treatmentsSummary,
);
expect(statisticsAfterDeletion[2].treatmentsSummary).toEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { dateBuilder } from '@label/core';
import { logger } from '../../../utils';
import { buildStatisticRepository } from '../repository';

export { deleteTreatmentsSummaryBefore };

async function deleteTreatmentsSummaryBefore({
since,
unit,
}: {
since: number;
unit: 'MONTHS';
}) {
const statisticRepository = buildStatisticRepository();

const statisticsExpirationDate = computeExpirationDate();
const treatmentsSummaryToDeleteIds = await statisticRepository.findAllIdsBefore(
statisticsExpirationDate,
);

const count = await statisticRepository.deleteTreatmentsSummaryByIds(
treatmentsSummaryToDeleteIds,
);

logger.log({
operationName: 'deleteTreatmentsSummaryBefore',
msg: `START: ${since} ${unit}: ${count} treatments sumary deleted`,
});

function computeExpirationDate() {
switch (unit) {
case 'MONTHS':
return dateBuilder.monthsAgo(since);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ async function getUsersAndDuration(
userTreatments: { userId: ObjectId; treatmentDuration: number }[],
statId: ObjectId,
) {
const userIds = userTreatments.map((userTreatment) => userTreatment.userId);
const userIds = userTreatments
.filter((userTreatment) => userTreatment.userId != undefined)
.map((userTreatment) => userTreatment.userId);
const users = await userService.fetchUsersByIds(userIds);
return userTreatments.map((userTreatment) => {
const userIdString = idModule.lib.convertToString(userTreatment.userId);
const { email, name, _id } = users[userIdString];
return {
email: email,
name: name,
id: _id,
statId: statId,
treatmentDuration: userTreatment.treatmentDuration,
};
if (userTreatment.userId != undefined) {
const userIdString = idModule.lib.convertToString(userTreatment.userId);
const { name, _id } = users[userIdString];
return {
name: name,
id: _id,
statId: statId,
treatmentDuration: userTreatment.treatmentDuration,
};
} else {
return {
name: undefined,
id: undefined,
statId: statId,
treatmentDuration: userTreatment.treatmentDuration,
};
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { fetchPersonalStatistics } from './fetchPersonalStatistics';
import { fetchSummary } from './fetchSummary';
import { saveStatisticsOfDocument } from './saveStatisticsOfDocument';
import { fetchDocumentStatistics } from './fetchDocumentStatistics';
import { deleteTreatmentsSummaryBefore } from './deleteTreatmentsSummaryBefore';

export { statisticService };

Expand All @@ -18,4 +19,5 @@ const statisticService = {
fetchSummary,
fetchDocumentStatistics,
saveStatisticsOfDocument,
deleteTreatmentsSummaryBefore,
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { wordings } from '../../../../wordings';
export { DocumentStatisticsBox };

type treatmentsSummaryType = {
email: string;
id: string;
name: string;
statId: string;
Expand Down Expand Up @@ -147,7 +146,7 @@ function DocumentStatisticsBox(props: { documentStatistic: documentStatsType; wi
.map((treatment) => timeOperator.convertDurationToReadableDuration(treatment.treatmentDuration))
.join(', ');

const agent = uniqueTreatments.map((treatment) => treatment.name).join(', ');
const agent = uniqueTreatments.map((treatment) => treatment.name || 'N/A').join(', ');

return [
{
Expand Down
8 changes: 4 additions & 4 deletions packages/generic/client/src/wordings/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ const fr = {
subtitle: 'Statistiques',
},
treatedDecisions: 'Décisions traitées',
agregatedStatisticsHintMessage: 'Veuillez sélectionner un filtre pour afficher les statistiques',
specificStatisticsHintMessage: 'Veuillez chercher par numéro de décision pour afficher les statistiques',
specificStatisticsNotFound:
"Il n'y a pas de statistiques pour ce numéro de document. Les statistiques sont conservées 6 mois.",
agregatedStatisticsHintMessage: 'Veuillez sélectionner un filtre pour afficher les statistiques.',
specificStatisticsHintMessage:
"Veuillez chercher par numéro de décision pour afficher les statistiques. Le nom de l'agent ayant traité la décision est conservé 6 mois.",
specificStatisticsNotFound: "Il n'y a pas de statistiques pour ce numéro de document.",
box: {
computation: {
total: 'Total',
Expand Down
23 changes: 17 additions & 6 deletions packages/generic/core/src/api/apiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,28 @@ const apiSchema = {
content: {
kind: 'object',
content: {
id: buildModel({
kind: 'custom',
content: 'id',
} as const),
id: {
kind: 'or',
content: [
buildModel({
kind: 'custom',
content: 'id',
} as const),
{ kind: 'primitive', content: 'undefined' },
],
},
statId: buildModel({
kind: 'custom',
content: 'id',
} as const),
treatmentDuration: { kind: 'primitive', content: 'number' },
name: { kind: 'primitive', content: 'string' },
email: { kind: 'primitive', content: 'string' },
name: {
kind: 'or',
content: [
{ kind: 'primitive', content: 'string' },
{ kind: 'primitive', content: 'undefined' },
],
},
},
},
},
Expand Down
Loading