Skip to content

Commit

Permalink
Feat/pre assignation (#101)
Browse files Browse the repository at this point in the history
* create model and repositories to preAssign decision

* add preAssignation service tests

* create pre assignation api endpoints

* fix import in controller

* create assignation when there is a preAssignation after nlp annotation

* add delete preassignation route on api

* first step for frontend

* create delete oprtion

* get userName instead of userId

* refetch on delete

* add pre-assignation creation button

* add pre assignation creation

* lint

* adapt clear db

* fix tests

* fix to run script without docker

* change behavior to accept appealNumbers

* fix docker start

* reset path to storage

* fix paths to execute scripts

* changes from review

---------

Co-authored-by: Antoine Jeanneney <antoine.jeanneney@justice.fr>
  • Loading branch information
ajeanneney and Antoine Jeanneney committed Jun 3, 2024
1 parent ef1b5c3 commit 8e39746
Show file tree
Hide file tree
Showing 40 changed files with 984 additions and 75 deletions.
1 change: 1 addition & 0 deletions docs/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
- statistics
- treatments
- users
- preAssignations
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"init:db": "scripts/initializeTestDb.sh",
"lint": "lerna run lint",
"migration:create": "scripts/createMigrationFile.sh",
"start:backend": "RUN_MODE=LOCAL lerna run --scope @label/cour-de-cassation start --stream",
"start:backend:dev": "RUN_MODE=LOCAL nodemon",
"start:client:dev": "yarn compile:client && cd packages/generic/client && yarn start",
"test": "lerna run test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { nlpApiType, nlpResponseType } from './nlpApiType';

export { buildNlpLocalApi };

const pathToNlpAnnotations = 'packages/courDeCassation/storage/annotations/';
const pathToNlpAnnotations = './storage/annotations/';

function buildNlpLocalApi(): nlpApiType {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,10 @@ function extractRouteForJurica({
(decEndCaseCode >= 11.01 && decEndCaseCode <= 22.24) ||
(decEndCaseCode >= 33.01 && decEndCaseCode <= 33.03)
) {
let endCaseCodeCsv;
if (process.env.RUN_MODE === 'LOCAL') {
endCaseCodeCsv = fs.readFileSync(
'packages/courDeCassation/static/endCaseRoutes.csv',
{
encoding: 'utf8',
},
) as string;
} else {
endCaseCodeCsv = fs.readFileSync('./static/endCaseRoutes.csv', {
encoding: 'utf8',
}) as string;
}
const endCaseCodeCsv = fs.readFileSync('./static/endCaseRoutes.csv', {
encoding: 'utf8',
}) as string;

const endCaseCodeData = {} as Array<any>;

endCaseCodeCsv.split('\n').forEach((line) => {
Expand All @@ -57,19 +48,10 @@ function extractRouteForJurica({
}
}

let NACCodeCsv;
if (process.env.RUN_MODE === 'LOCAL') {
NACCodeCsv = fs.readFileSync(
'packages/courDeCassation/static/NACCodeRoutes.csv',
{
encoding: 'utf8',
},
) as string;
} else {
NACCodeCsv = fs.readFileSync('./static/NACCodeRoutes.csv', {
encoding: 'utf8',
}) as string;
}
const NACCodeCsv = fs.readFileSync('./static/NACCodeRoutes.csv', {
encoding: 'utf8',
}) as string;

const NACCodeData = {} as Array<any>;

NACCodeCsv.split('\n').forEach((line) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/courDeCassation/src/sderApi/sderLocalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sderApiType } from './sderApiType';

export { sderLocalApi };

const pathToCourtDecisions = 'packages/courDeCassation/storage/documents/';
const pathToCourtDecisions = './storage/documents/';

async function fetchApi({
method,
Expand Down
26 changes: 25 additions & 1 deletion packages/generic/backend/src/api/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { userService } from '../modules/user';
import { buildAuthenticatedController } from './buildAuthenticatedController';
import { controllersFromSchemaType } from './controllerType';
import { annotationReportService } from '../modules/annotationReport';
import { preAssignationService } from '../modules/preAssignation';
import { replacementTermType } from '@label/core';

export { controllers };
Expand Down Expand Up @@ -191,6 +192,12 @@ const controllers: controllersFromSchemaType<typeof apiSchema> = {
permissions: ['admin', 'scrutator'],
controllerWithUser: async () => userService.fetchWorkingUsers(),
}),

preAssignations: buildAuthenticatedController({
permissions: ['admin'],
controllerWithUser: async () =>
preAssignationService.fetchAllPreAssignation(),
}),
},

post: {
Expand Down Expand Up @@ -238,7 +245,13 @@ const controllers: controllersFromSchemaType<typeof apiSchema> = {
idModule.lib.buildId(problemReportId),
),
}),

deletePreAssignation: buildAuthenticatedController({
permissions: ['admin'],
controllerWithUser: async (_, { args: { preAssignationId } }) =>
preAssignationService.deletePreAssignation(
idModule.lib.buildId(preAssignationId),
),
}),
deleteHumanTreatmentsForDocument: buildAuthenticatedController({
permissions: ['admin'],
controllerWithUser: async (_, { args: { documentId } }) => {
Expand Down Expand Up @@ -468,5 +481,16 @@ const controllers: controllersFromSchemaType<typeof apiSchema> = {
);
},
}),

createPreAssignation: buildAuthenticatedController({
permissions: ['admin'],
controllerWithUser: async (_, { args: { userId, source, number } }) => {
preAssignationService.createPreAssignation({
userId: idModule.lib.buildId(userId),
source,
number,
});
},
}),
},
};
8 changes: 8 additions & 0 deletions packages/generic/backend/src/app/scripts/clearDb.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildPreAssignationRepository } from '../../modules/preAssignation';
import { buildAnnotationReportRepository } from '../../modules/annotationReport';
import { buildAssignationRepository } from '../../modules/assignation';
import { buildDocumentRepository } from '../../modules/document';
Expand All @@ -19,6 +20,7 @@ async function clearDb({
statistic = true,
treatment = true,
user = true,
preAssignations = true,
}: {
annotation?: boolean;
assignation?: boolean;
Expand All @@ -28,6 +30,7 @@ async function clearDb({
statistic?: boolean;
treatment?: boolean;
user?: boolean;
preAssignations?: boolean;
}) {
logger.log({
operationName: 'clearDb',
Expand All @@ -41,6 +44,7 @@ async function clearDb({
statistic,
treatment,
user,
preAssignations,
},
});

Expand Down Expand Up @@ -77,6 +81,10 @@ async function clearDb({
shouldClear: user,
buildRepository: buildUserRepository,
},
{
shouldClear: preAssignations,
buildRepository: buildPreAssignationRepository,
},
]
.filter(({ shouldClear }) => shouldClear)
.map(({ buildRepository }) => buildRepository());
Expand Down
5 changes: 5 additions & 0 deletions packages/generic/backend/src/app/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { settingsType } from '@label/core';
import { settingsLoader } from '../lib/settingsLoader';
import { logger, mongo } from '../utils';
import { setIndexesOnAllCollections } from './scripts';
import * as dotenv from 'dotenv';

export { setup, setupMongo };

if (process.env.RUN_MODE === 'LOCAL') {
dotenv.config();
}

async function setup(settings: settingsType) {
setupSettings(settings);
await setupMongo();
Expand Down
54 changes: 50 additions & 4 deletions packages/generic/backend/src/lib/annotator/buildAnnotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
settingsType,
treatmentModule,
annotationModule,
preAssignationType,
} from '@label/core';
import { buildAnnotationReportRepository } from '../../modules/annotationReport';
import { documentService } from '../../modules/document';
import { treatmentService } from '../../modules/treatment';
import { logger } from '../../utils';
import { annotatorConfigType } from './annotatorConfigType';
import { preAssignationService } from '../../modules/preAssignation';
import { assignationService } from '../../modules/assignation';

export { buildAnnotator };

Expand Down Expand Up @@ -312,16 +315,59 @@ function buildAnnotator(
publicationCategory: document.publicationCategory,
route: document.route,
});
await documentService.updateDocumentStatus(
document._id,
nextDocumentStatus,
);

// Check first pre-assignation by documentNumber and then by appelNumber
const preAssignationForDocument =
(await preAssignationService.fetchPreAssignationBySourceAndNumber(
document.documentNumber.toString(),
document.source,
)) ||
(await preAssignationService.fetchPreAssignationBySourceAndNumber(
document.decisionMetadata.appealNumber,
document.source,
));

if (
nextDocumentStatus === 'free' &&
preAssignationForDocument != undefined
) {
logger.log({
operationName: 'annotateDocument',
msg: `Pre-assignation found for document ${formatDocumentInfos(
document,
)}. Matching pre-assignation number : ${
preAssignationForDocument.number
}. Creating assignation...`,
});
await createAssignation(preAssignationForDocument, document);
} else {
await documentService.updateDocumentStatus(
document._id,
nextDocumentStatus,
);
}

logger.log({
operationName: 'annotateDocument',
msg: `Annotation done for document ${formatDocumentInfos(document)}`,
});
}

async function createAssignation(
preAssignation: preAssignationType,
document: documentType,
) {
await assignationService.createAssignation({
documentId: idModule.lib.buildId(document._id),
userId: idModule.lib.buildId(preAssignation.userId),
});
await preAssignationService.deletePreAssignation(preAssignation._id);
await documentService.updateDocumentStatus(
idModule.lib.buildId(document._id),
'saved',
);
}

async function createAnnotatorTreatment({
annotations,
documentId,
Expand Down
4 changes: 4 additions & 0 deletions packages/generic/backend/src/modules/preAssignation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { buildPreAssignationRepository } from './repository';
import { preAssignationService } from './service';

export { buildPreAssignationRepository, preAssignationService };
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { preAssignationType } from '@label/core';
import { buildFakeRepositoryBuilder } from '../../../repository';
import { customPreAssignationRepositoryType } from './customPreAssignationRepositoryType';

export { buildFakePreAssignationRepository };

const buildFakePreAssignationRepository = buildFakeRepositoryBuilder<
preAssignationType,
customPreAssignationRepositoryType
>({
collectionName: 'preAssignations',
buildCustomFakeRepository: (collection) => ({
async findOneByNumberAndSource({ number, source }) {
return collection.find(
(preAssignation) =>
preAssignation.source === source && preAssignation.number === number,
);
},
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { preAssignationType } from '@label/core';
import { buildRepositoryBuilder } from '../../../repository';
import { customPreAssignationRepositoryType } from './customPreAssignationRepositoryType';

export { buildPreAssignationRepository };

const buildPreAssignationRepository = buildRepositoryBuilder<
preAssignationType,
customPreAssignationRepositoryType
>({
collectionName: 'preAssignations',
indexes: [
{
index: { source: 1, number: 1 },
mustBeUnique: true,
} as const,
],
buildCustomRepository: (collection) => ({
async findOneByNumberAndSource({ number, source }) {
const preAssignation = await collection.findOne({
number,
source,
});
return preAssignation || undefined;
},
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { preAssignationType } from '@label/core';

export type { customPreAssignationRepositoryType };

type customPreAssignationRepositoryType = {
findOneByNumberAndSource: ({
number,
source,
}: {
number: string;
source: string;
}) => Promise<preAssignationType | undefined>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { dependencyManager } from '../../../utils';
import { buildPreAssignationRepository } from './buildPreAssignationRepository';
import { buildFakePreAssignationRepository } from './buildFakePreAssignationRepository';

export { buildRepository as buildPreAssignationRepository };

const buildRepository = dependencyManager.inject({
forLocal: buildPreAssignationRepository,
forProd: buildPreAssignationRepository,
forTest: buildFakePreAssignationRepository,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { preAssignationModule, idType } from '@label/core';
import { buildPreAssignationRepository } from '..';

export { createPreAssignation };

async function createPreAssignation({
userId,
source,
number,
}: {
userId: idType;
source: string;
number: string;
}) {
const preAssignationRepository = buildPreAssignationRepository();

const preAssignation = preAssignationModule.lib.buildPreAssignation({
userId,
source,
number,
creationDate: new Date().getTime(),
});

await preAssignationRepository.insert(preAssignation);

return preAssignation;
}
Loading

0 comments on commit 8e39746

Please sign in to comment.