Skip to content

Commit

Permalink
Merge pull request #2660 from MTES-MCT/fix/dashboard-v2-filters
Browse files Browse the repository at this point in the history
[TRA-11226] Fix/dashboard v2 filters
  • Loading branch information
kornifex authored Aug 28, 2023
2 parents ed9ee4f + ba70c50 commit 40c5b5f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
28 changes: 28 additions & 0 deletions front/src/Apps/Dashboard/dashboardServices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
isEcoOrgSign,
isEmetteurSign,
isSignTransportAndCanSkipEmission,
getOperationCodesFromSearchString,
} from "./dashboardServices";
import {
BsdType,
Expand Down Expand Up @@ -1354,4 +1355,31 @@ describe("dashboardServices", () => {
expect(result).toBe(false);
});
});

describe("getOperationCodesFromSearchString", () => {
it("returns operation codes from search string", () => {
const searchString = "r15 R 13 d13 d 15 peoropzier 23326783 d23 D 15";
const operationCodes = getOperationCodesFromSearchString(searchString);

expect(operationCodes).toStrictEqual([
"R 13",
"R13",
"D 15",
"D15",
"D 15",
"D15",
"R15",
"R 15",
"D13",
"D 13",
]);
});

it("returns empty array on bad formatted string", () => {
const searchString = "peoropzier 23326783";
const operationCodes = getOperationCodesFromSearchString(searchString);

expect(operationCodes).toStrictEqual([]);
});
});
});
16 changes: 15 additions & 1 deletion front/src/Apps/Dashboard/dashboardServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import {
completer_bsd_suite,
} from "../common/wordings/dashboard/wordingsDashboard";
import { BsdCurrentTab } from "Apps/common/types/commonTypes";
import { User } from "@sentry/browser";

export const getBsdView = (bsd): BsdDisplay | null => {
const bsdView = formatBsd(bsd);
Expand Down Expand Up @@ -1091,3 +1090,18 @@ export const canEditCustomInfoOrTransporterNumberPlate = (

return false;
};

export const getOperationCodesFromSearchString = (value: any): string[] => {
let searchCodes: string[] = [];

value.match(/[rRdD]{1}( )(12|13|14|15)/g)?.forEach(code => {
searchCodes.push(code.toUpperCase());
searchCodes.push(code.replace(" ", "").toUpperCase());
});

value.match(/[rRdD]{1}(12|13|14|15)/g)?.forEach(code => {
searchCodes.push(code.toUpperCase());
searchCodes.push(code.replace("1", " 1").toUpperCase());
});
return searchCodes;
};
35 changes: 26 additions & 9 deletions front/src/Apps/Dashboard/dashboardUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
IconBSDDThin as IconBSDD,
IconBSDasriThin as IconBSDasri,
} from "Apps/common/Components/Icons/Icons";
import { getOperationCodesFromSearchString } from "./dashboardServices";

export const MAX_FILTER = 5;

Expand Down Expand Up @@ -377,16 +378,32 @@ export const filterPredicates: {
},
{
filterName: FilterName.operationCode,
where: value => ({
destination: { operation: { code: { _contains: value } } },
}),
where: value => {
const operationCodes = getOperationCodesFromSearchString(value);

if (operationCodes.length > 1) {
return {
destination: {
operation: {
code: {
_in: operationCodes,
},
},
},
};
}

return {
destination: { operation: { code: { _contains: value } } },
};
},
order: "code",
},
{
filterName: FilterName.emitterSignDate,
where: value => ({
emitter: {
emission: { date: { _lte: value.startDate, _gte: value.endDate } },
emission: { date: { _lte: value.endDate, _gte: value.startDate } },
},
}),
order: "date",
Expand All @@ -395,7 +412,7 @@ export const filterPredicates: {
filterName: FilterName.workerSignDate,
where: value => ({
worker: {
work: { date: { _lte: value.startDate, _gte: value.endDate } },
work: { date: { _lte: value.endDate, _gte: value.startDate } },
},
}),
order: "date",
Expand All @@ -405,7 +422,7 @@ export const filterPredicates: {
where: value => ({
transporter: {
transport: {
takenOverAt: { _lte: value.startDate, _gte: value.endDate },
takenOverAt: { _lte: value.endDate, _gte: value.startDate },
},
},
}),
Expand All @@ -415,7 +432,7 @@ export const filterPredicates: {
filterName: FilterName.destinationReceptionDate,
where: value => ({
destination: {
reception: { date: { _lte: value.startDate, _gte: value.endDate } },
reception: { date: { _lte: value.endDate, _gte: value.startDate } },
},
}),
order: "date",
Expand All @@ -424,7 +441,7 @@ export const filterPredicates: {
filterName: FilterName.destinationAcceptationDate,
where: value => ({
destination: {
acceptation: { date: { _lte: value.startDate, _gte: value.endDate } },
acceptation: { date: { _lte: value.endDate, _gte: value.startDate } },
},
}),
order: "date",
Expand All @@ -433,7 +450,7 @@ export const filterPredicates: {
filterName: FilterName.destinationOperationSignDate,
where: value => ({
destination: {
operation: { date: { _lte: value.startDate, _gte: value.endDate } },
operation: { date: { _lte: value.endDate, _gte: value.startDate } },
},
}),
order: "date",
Expand Down

0 comments on commit 40c5b5f

Please sign in to comment.