Skip to content

Commit

Permalink
fix(policy): add la rochelle 2024 (#2694)
Browse files Browse the repository at this point in the history
* feat(policy): add la rochelle 2024

* chore(policy): add unit test for exclusion

* fix min distance

* fix(policy): unit test

* fix description import path

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore(fraudcheck): remove unused colun

* fix description

---------

Co-authored-by: Jonathan Fallon <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Fallon <[email protected]>
  • Loading branch information
4 people authored Nov 26, 2024
1 parent 59cc70b commit a4f0940
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE FRAUDCHECK.LABELS DROP COLUMN geo_code;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const description = `<div _ngcontent-moy-c231="" id="summary" class="campaignSummaryText-content-text">
<p>
Campagne d’incitation au covoiturage du <b>01 janvier 2024 au 31 décembre 2024</b>
</p>
<p>
Cette campagne est limitée à <b>433 740,19</b> €
</p>
<p>
Les <b>conducteurs</b> effectuant un trajet de plus de 5 km
sont incités selon les règles suivantes :
</p>
<ul>
<li><b>De 5 à 10 km : 1,00 € par trajet par passager transporté </b></li>
<li><b>De 10 km à 20 km : 0,10 € par km par passager </b></li>
</ul>
<p>Les restrictions suivantes seront appliquées :</p>
<ul>
<li><b>6 trajets maximum pour le conducteur par jour.</b></li>
<li><b>80,00 € maximum pour le conducteur par mois.</b></li>
</ul>
<p>
La campagne est limitée à l'opérateur <b>Blablacar Daily</b>
pour les trajets de classe <b>B ou C</b>.
</p>
</div>`;
111 changes: 111 additions & 0 deletions api/src/pdc/services/policy/engine/policies/20240101_LaRochelle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { getOperatorsAt, TimestampedOperators } from "@/pdc/services/policy/engine/helpers/getOperatorsAt.ts";
import { isOperatorClassOrThrow } from "@/pdc/services/policy/engine/helpers/isOperatorClassOrThrow.ts";
import { isOperatorOrThrow } from "@/pdc/services/policy/engine/helpers/isOperatorOrThrow.ts";
import {
LimitTargetEnum,
watchForGlobalMaxAmount,
watchForPersonMaxAmountByMonth,
watchForPersonMaxTripByDay,
} from "@/pdc/services/policy/engine/helpers/limits.ts";
import { onDistanceRange, onDistanceRangeOrThrow } from "@/pdc/services/policy/engine/helpers/onDistanceRange.ts";
import { perKm, perSeat } from "@/pdc/services/policy/engine/helpers/per.ts";
import { AbstractPolicyHandler } from "@/pdc/services/policy/engine/policies/AbstractPolicyHandler.ts";
import { RunnableSlices } from "@/pdc/services/policy/interfaces/engine/PolicyInterface.ts";
import {
OperatorsEnum,
PolicyHandlerInterface,
PolicyHandlerParamsInterface,
PolicyHandlerStaticInterface,
StatelessContextInterface,
} from "@/pdc/services/policy/interfaces/index.ts";
import { description } from "./20240101_LaRochelle.html.ts";

export const LaRochelle2024: PolicyHandlerStaticInterface = class extends AbstractPolicyHandler
implements PolicyHandlerInterface {
static readonly id = "larochelle_2024";

protected operators: TimestampedOperators = [
{
date: new Date("2024-01-01T00:00:00+0100"),
operators: [OperatorsEnum.BLABLACAR_DAILY],
},
];

protected slices: RunnableSlices = [
{
start: 5_000,
end: 10_000,
fn: (ctx: StatelessContextInterface) => perSeat(ctx, 100),
},
{
start: 10_000,
end: 20_000,
fn: (ctx: StatelessContextInterface) => perSeat(ctx, perKm(ctx, { amount: 10, offset: 10_000, limit: 20_000 })),
},
{
start: 20_000,
fn: () => 0,
},
];

constructor(public max_amount: number) {
super();
this.limits = [
[
"99911EAF-89AB-C346-DDD5-BD2C7704F935",
max_amount,
watchForGlobalMaxAmount,
],
[
"70CE7566-6FD5-F850-C039-D76AF6F8CEB5",
6,
watchForPersonMaxTripByDay,
LimitTargetEnum.Driver,
],
[
"ECDE3CD4-96FF-C9D2-BA88-45754205A798",
80_00,
watchForPersonMaxAmountByMonth,
LimitTargetEnum.Driver,
],
];
}

public processStateless(ctx: StatelessContextInterface): void {
this.processExclusion(ctx);
super.processStateless(ctx);

// Calcul des incitations par tranche
let amount = 0;
for (
const { start, fn } of this.slices
) {
if (onDistanceRange(ctx, { min: start })) {
amount += fn(ctx);
}
}

ctx.incentive.set(amount);
}

public params(): PolicyHandlerParamsInterface {
return {
tz: "Europe/Paris",
slices: this.slices,
operators: getOperatorsAt(this.operators),
limits: {
glob: this.max_amount,
},
};
}

public describe(): string {
return description;
}

protected processExclusion(ctx: StatelessContextInterface) {
isOperatorOrThrow(ctx, getOperatorsAt(this.operators, ctx.carpool.datetime));
onDistanceRangeOrThrow(ctx, { min: 5_000 });
isOperatorClassOrThrow(ctx, ["B", "C"]);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { it } from "@/dev_deps.ts";
import { v4 as uuidV4 } from "@/lib/uuid/index.ts";
import { OperatorsEnum } from "../../interfaces/index.ts";
import { makeProcessHelper } from "../tests/macro.ts";
import { LaRochelle2024 as Handler } from "./20240101_LaRochelle.ts";

const defaultPosition = {
arr: "73031",
com: "73031",
aom: "200069110",
epci: "200069110",
dep: "73",
reg: "84",
country: "XXXXX",
reseau: "76",
};
const defaultLat = 48.72565703413325;
const defaultLon = 2.261827843187402;

const defaultCarpool = {
_id: 1,
operator_trip_id: uuidV4(),
passenger_identity_key: uuidV4(),
driver_identity_key: "driver_id_one",
operator_uuid: OperatorsEnum.BLABLACAR_DAILY,
operator_class: "C",
passenger_is_over_18: true,
passenger_has_travel_pass: true,
driver_has_travel_pass: true,
datetime: new Date("2024-01-02"),
seats: 1,
distance: 6_000,
operator_journey_id: uuidV4(),
operator_id: 9,
driver_revenue: 20,
passenger_contribution: 20,
start: { ...defaultPosition },
end: { ...defaultPosition },
start_lat: defaultLat,
start_lon: defaultLon,
end_lat: defaultLat,
end_lon: defaultLon,
};

const process = makeProcessHelper(defaultCarpool);

it("should work with exclusions", async () =>
await process(
{
policy: { handler: Handler.id },
carpool: [
{ operator_uuid: OperatorsEnum.KLAXIT },
{ distance: 100 },
{ distance: 4_000 },
{ operator_class: "A" },
],
meta: [],
},
{
incentive: [0, 0, 0, 0],
meta: [],
},
));

it("should work basic", async () =>
await process(
{
policy: { handler: Handler.id },
carpool: [
{ distance: 2_000 },
{ distance: 6_000 },
{ distance: 6_000, seats: 2 },
{ distance: 80_000 },
{ distance: 80_000, seats: 2 },
],
meta: [],
},
{
incentive: [0, 100, 200, 200, 400],
meta: [
{
key: "max_amount_restriction.global.campaign.global",
value: 900,
},
{
key: "max_amount_restriction.0-driver_id_one.month.0-2024",
value: 900,
},
],
},
));

it("should work with driver month limits of 80 €", async () =>
await process(
{
policy: { handler: Handler.id },
carpool: [
{ distance: 6_000 },
{ distance: 6_000 },
],
meta: [
{
key: "max_amount_restriction.0-driver_id_one.month.0-2024",
value: 79_00,
},
{
key: "max_amount_restriction.global.campaign.global",
value: 79_00,
},
],
},
{
incentive: [100, 0],
meta: [
{
key: "max_amount_restriction.global.campaign.global",
value: 80_00,
},
{
key: "max_amount_restriction.0-driver_id_one.month.0-2024",
value: 80_00,
},
],
},
));
2 changes: 2 additions & 0 deletions api/src/pdc/services/policy/engine/policies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { PolicyTemplateOne } from "@/pdc/services/policy/engine/policies/unbound
import { PolicyTemplateThree } from "@/pdc/services/policy/engine/policies/unbound/PolicyTemplateThree.ts";
import { PolicyTemplateTwo } from "@/pdc/services/policy/engine/policies/unbound/PolicyTemplateTwo.ts";
import { PolicyHandlerStaticInterface } from "@/pdc/services/policy/interfaces/index.ts";
import { LaRochelle2024 } from "./20240101_LaRochelle.ts";
import { CCVMM202405 } from "./20240805_CCVMM_2024_05.ts";

export const policies: Map<string, PolicyHandlerStaticInterface> = new Map(
Expand Down Expand Up @@ -78,5 +79,6 @@ export const policies: Map<string, PolicyHandlerStaticInterface> = new Map(
SMTC2024Passenger,
TerresTouloises2024,
Vitre2023,
LaRochelle2024,
].map((h) => [h.id, h]),
);

0 comments on commit a4f0940

Please sign in to comment.