-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(policy): Haute Correze 2025 (#2757)
- Loading branch information
1 parent
f011f5f
commit f5b122d
Showing
4 changed files
with
333 additions
and
30 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
api/src/pdc/services/policy/engine/policies/20240902_haute_correze_2025.html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export const description = ` | ||
<div _ngcontent-pmm-c231="" id="summary" class="campaignSummaryText-content-text"> | ||
<p> | ||
Campagne d'incitation au covoiturage du <b> 02 septembre 2024 au 01 septembre 2025</b>, | ||
toute la semaine | ||
</p> | ||
<p>Cette campagne est limitée à <b>10 000,00 €</b>.</p> | ||
<p> | ||
Les <b> conducteurs </b> effectuant un trajet en covoiturage d'au moins 2 km | ||
au départ ou à l'arrivée de la CC Haute-Corrèze Communauté | ||
sont incités selon les règles suivantes : | ||
</p> | ||
<ul> | ||
<li><b>De 2 à 15 km : 1,50 € par trajet et par passager transporté</b></li> | ||
<li><b>De 15 à 30 km : 1,50 € + 0,10 € par km supplémentaire par trajet et par passager transporté</b></li> | ||
<li><b>De 30 à 80 km : 3,00 € par trajet et par passager transporté</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>150,00 € maximum pour le conducteur par mois.</b></li> | ||
</ul> | ||
<p> | ||
La campagne est limitée à l'opérateur Blablacar Daily proposant des preuves de classe <b>B</b> ou <b>C</b>. | ||
</p> | ||
</div>`; |
113 changes: 113 additions & 0 deletions
113
api/src/pdc/services/policy/engine/policies/20240902_haute_correze_2025.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
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 { startsOrEndsAtOrThrow } from "@/pdc/services/policy/engine/helpers/startsOrEndsAtOrThrow.ts"; | ||
import { description } from "@/pdc/services/policy/engine/policies/20240902_haute_correze_2025.html.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"; | ||
|
||
/* eslint-disable-next-line */ | ||
export const HauteCorreze2025: PolicyHandlerStaticInterface = class extends AbstractPolicyHandler | ||
implements PolicyHandlerInterface { | ||
static readonly id = "haute_correze_2025"; | ||
|
||
protected operators: TimestampedOperators = [ | ||
{ | ||
date: new Date("2024-09-02T00:00:00+0200"), | ||
operators: [OperatorsEnum.BLABLACAR_DAILY], | ||
}, | ||
]; | ||
|
||
// 7 cts per km per passenger | ||
protected slices: RunnableSlices = [ | ||
{ | ||
start: 2_000, | ||
end: 15_000, | ||
fn: (ctx: StatelessContextInterface) => perSeat(ctx, 1_50), | ||
}, | ||
{ | ||
start: 15_000, | ||
end: 30_000, | ||
fn: (ctx: StatelessContextInterface) => perSeat(ctx, perKm(ctx, { amount: 10, offset: 15_000, limit: 30_000 })), | ||
}, | ||
{ | ||
start: 30_000, | ||
end: 80_001, | ||
fn: (_ctx: StatelessContextInterface) => 0, | ||
}, | ||
]; | ||
|
||
constructor(public max_amount: number) { | ||
super(); | ||
this.limits = [ | ||
[ | ||
"17a34876-97dc-409e-a45a-eb1c1a7f9924", | ||
max_amount, | ||
watchForGlobalMaxAmount, | ||
], | ||
[ | ||
"23ad0e0c-6805-41ae-915d-bcc566486bb6", | ||
6, | ||
watchForPersonMaxTripByDay, | ||
LimitTargetEnum.Driver, | ||
], | ||
[ | ||
"4437e7aa-efe6-4eb8-b844-3139819f8fcf", | ||
150_00, | ||
watchForPersonMaxAmountByMonth, | ||
LimitTargetEnum.Driver, | ||
], | ||
]; | ||
} | ||
|
||
protected processExclusion(ctx: StatelessContextInterface) { | ||
isOperatorOrThrow(ctx, getOperatorsAt(this.operators, ctx.carpool.datetime)); | ||
onDistanceRangeOrThrow(ctx, { min: 2_000, max: 80_001 }); | ||
isOperatorClassOrThrow(ctx, ["B", "C"]); | ||
startsOrEndsAtOrThrow(ctx, { aom: ["200066744"] }); | ||
} | ||
|
||
processStateless(ctx: StatelessContextInterface): void { | ||
this.processExclusion(ctx); | ||
super.processStateless(ctx); | ||
|
||
let amount = 0; | ||
for (const { start, fn } of this.slices) { | ||
if (onDistanceRange(ctx, { min: start })) { | ||
amount += fn(ctx); | ||
} | ||
} | ||
|
||
ctx.incentive.set(amount); | ||
} | ||
|
||
params(): PolicyHandlerParamsInterface { | ||
return { | ||
tz: "Europe/Paris", | ||
slices: this.slices, | ||
operators: getOperatorsAt(this.operators), | ||
limits: { | ||
glob: this.max_amount, | ||
}, | ||
}; | ||
} | ||
|
||
describe(): string { | ||
return description; | ||
} | ||
}; |
154 changes: 154 additions & 0 deletions
154
api/src/pdc/services/policy/engine/policies/20240902_haute_correze_2025.unit.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { it } from "@/dev_deps.ts"; | ||
import { v4 as uuidV4 } from "@/lib/uuid/index.ts"; | ||
import { HauteCorreze2025 as Handler } from "@/pdc/services/policy/engine/policies/20240902_haute_correze_2025.ts"; | ||
import { makeProcessHelper } from "@/pdc/services/policy/engine/tests/macro.ts"; | ||
import { OperatorsEnum } from "@/pdc/services/policy/interfaces/index.ts"; | ||
|
||
const defaultPosition = { | ||
arr: "19139", | ||
com: "19139", | ||
aom: "200066744", | ||
epci: "200066744", | ||
dep: "19", | ||
reg: "75", | ||
country: "XXXXX", | ||
reseau: "73", | ||
}; | ||
const defaultLat = 45.54858380899173; | ||
const defaultLon = 2.3146017323921195; | ||
|
||
const defaultCarpool = { | ||
_id: 1, | ||
operator_trip_id: uuidV4(), | ||
passenger_identity_key: uuidV4(), | ||
driver_identity_key: uuidV4(), | ||
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-11-01"), | ||
seats: 1, | ||
distance: 5_000, | ||
operator_journey_id: uuidV4(), | ||
operator_id: 1, | ||
driver_revenue: 20, | ||
passenger_contribution: 20, | ||
start: { ...defaultPosition }, | ||
end: { ...defaultPosition }, | ||
start_lat: defaultLat, | ||
start_lon: defaultLon, | ||
end_lat: 45.53725299731382, | ||
end_lon: 2.3059201226861297, | ||
}; | ||
|
||
const process = makeProcessHelper(defaultCarpool); | ||
|
||
it("should work with exclusions", async () => | ||
await process( | ||
{ | ||
policy: { handler: Handler.id }, | ||
carpool: [ | ||
{ operator_uuid: "not in list" }, | ||
{ operator_uuid: OperatorsEnum.KLAXIT }, | ||
{ distance: 100 }, | ||
{ distance: 90_000 }, | ||
{ operator_class: "A" }, | ||
], | ||
meta: [], | ||
}, | ||
{ incentive: [0, 0, 0, 0, 0], meta: [] }, | ||
)); | ||
|
||
it("should work basic with start/end inside aom", async () => | ||
await process( | ||
{ | ||
policy: { handler: Handler.id }, | ||
carpool: [ | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, seats: 2, driver_identity_key: "one" }, | ||
{ distance: 25_000, driver_identity_key: "two" }, | ||
{ distance: 25_000, seats: 2, driver_identity_key: "two" }, | ||
{ distance: 80_000, driver_identity_key: "marcel" }, | ||
], | ||
meta: [], | ||
}, | ||
{ | ||
incentive: [150, 300, 250, 500, 300], | ||
meta: [ | ||
{ | ||
key: "max_amount_restriction.global.campaign.global", | ||
value: 1500, | ||
}, | ||
{ | ||
key: "max_amount_restriction.0-one.month.10-2024", | ||
value: 450, | ||
}, | ||
{ | ||
key: "max_amount_restriction.0-two.month.10-2024", | ||
value: 750, | ||
}, | ||
{ | ||
key: "max_amount_restriction.0-marcel.month.10-2024", | ||
value: 300, | ||
}, | ||
], | ||
}, | ||
)); | ||
|
||
it("should work with global limits", async () => | ||
await process( | ||
{ | ||
policy: { handler: Handler.id, max_amount: 15_000_00 }, | ||
carpool: [{ distance: 79_000, driver_identity_key: "one" }], | ||
meta: [ | ||
{ | ||
key: "max_amount_restriction.global.campaign.global", | ||
value: 14_999_99, | ||
}, | ||
], | ||
}, | ||
{ | ||
incentive: [1], // <-- should be 250. capped to 1 | ||
meta: [ | ||
{ | ||
key: "max_amount_restriction.global.campaign.global", | ||
value: 15_000_00, | ||
}, | ||
{ | ||
key: "max_amount_restriction.0-one.month.10-2024", | ||
value: 300, | ||
}, | ||
], | ||
}, | ||
)); | ||
|
||
it("should work with 6 trips per day limit", async () => | ||
await process( | ||
{ | ||
policy: { handler: Handler.id }, | ||
carpool: [ | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, driver_identity_key: "one" }, | ||
{ distance: 5_000, driver_identity_key: "one" }, // too many | ||
], | ||
meta: [], | ||
}, | ||
{ | ||
incentive: [150, 150, 150, 150, 150, 150, 0], | ||
meta: [ | ||
{ | ||
key: "max_amount_restriction.global.campaign.global", | ||
value: 900, | ||
}, | ||
{ | ||
key: "max_amount_restriction.0-one.month.10-2024", | ||
value: 900, | ||
}, | ||
], | ||
}, | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters