Codes promo et remises automatiques : pourcentage, montant, livraison offerte, par produit/catégorie, limites d'usage
Installer
genpm add @core/discountsCe que vous obtenez
- Source dans src/lib/discounts/, 6 fichiers. (21,5 ko)
- Règles IA dans src/lib/discounts/AGENTS.md, plus les fichiers de règles de l’IDE.
- Résout @core/cart, @core/catalog, @core/db, @core/money, @core/orders pour vous.
README
Ce paquet n’a pas de README.
Voici exactement ce que lit votre IA quand elle travaille dans src/lib/discounts. Rien d’autre n’est ajouté à son contexte.
@core/discounts — rules for AI agents
Purpose
Discount codes and automatic discounts for @core/cart: percent, fixed amount or free shipping; optional scope by
product or category slugs, minimum order, start/end dates, total and per-customer usage limits, first-order-only and
combinability (non-combinable ones compete: the biggest wins). Registers the discounts (100) and free-shipping
(250) totals steps and records redemptions inside the transaction that creates/pays the order (onOrderPaidTx of
@core/orders), with the discount row locked. Limits count recorded redemptions plus open payments carrying the code
(pendingCodeUses of @core/cart, fed by @core/checkout). If a limit was exceeded anyway (the customer already paid),
the order is kept, the redemption recorded and a discount_over_limit event is added to the order timeline for review.
Tables: discounts, discount_redemptions.
Map
index.ts— public API:upsertDiscount,DiscountInput,discountsStep,freeShippingStep,recordRedemptions,discountsAdminResource.discounts.ts— validation, application and the redemption handler (registered on import).admin.ts— admin resource.
Integration
- Migrations as in
src/lib/db/AGENTS.md. Import this module at startup (it registers its steps and handler). - Cart page: a code field calling
PUT /api/cart/codes {codes: [code]}(@core/cart); showtotals.discountsand anyinvalid_codeissues. - Pass the customer email to
computeTotals(cart, { email })once known so per-customer and first-order limits apply. - Add
discountsAdminResourcetosrc/genpm/admin.ts(permissionsdiscounts:read|update). - Verify: create
TEN(10 %), apply it, the total drops and each line shows its share.
Conventions
- Codes are stored uppercase (
A–Z 0–9 _ -); values: percent 1–100, fixed in minor units. - Deleting a discount deactivates it (redemption history stays).
- Free-shipping adjustments carry their
code(amount 0), so they count towards usage limits. - Check the order timeline (
discount_over_limit) when a usage-limited campaign ends. - Prefer automatic discounts for store-wide sales; codes for campaigns.
Don't
- Don't show a "was" price or a percentage off that isn't real: in the EU a price reduction must refer to the lowest price of the previous 30 days (Omnibus Directive). Use @core/pricing's price history for compare-at prices.
- Don't apply discounts on the client or trust discount amounts sent by the browser.
- Don't create fake urgency (countdowns that reset, "only today" when it isn't).
# @core/discounts — rules for AI agents
## Purpose
Discount codes and automatic discounts for @core/cart: percent, fixed amount or free shipping; optional scope by
product or category slugs, minimum order, start/end dates, total and per-customer usage limits, first-order-only and
combinability (non-combinable ones compete: the biggest wins). Registers the `discounts` (100) and `free-shipping`
(250) totals steps and records redemptions inside the transaction that creates/pays the order (`onOrderPaidTx` of
@core/orders), with the discount row locked. Limits count recorded redemptions plus open payments carrying the code
(`pendingCodeUses` of @core/cart, fed by @core/checkout). If a limit was exceeded anyway (the customer already paid),
the order is kept, the redemption recorded and a `discount_over_limit` event is added to the order timeline for review.
Tables: `discounts`, `discount_redemptions`.
## Map
- `index.ts` — public API: `upsertDiscount`, `DiscountInput`, `discountsStep`, `freeShippingStep`, `recordRedemptions`, `discountsAdminResource`.
- `discounts.ts` — validation, application and the redemption handler (registered on import). `admin.ts` — admin resource.
## Integration
1. Migrations as in `src/lib/db/AGENTS.md`. Import this module at startup (it registers its steps and handler).
2. Cart page: a code field calling `PUT /api/cart/codes {codes: [code]}` (@core/cart); show `totals.discounts` and any `invalid_code` issues.
3. Pass the customer email to `computeTotals(cart, { email })` once known so per-customer and first-order limits apply.
4. Add `discountsAdminResource` to `src/genpm/admin.ts` (permissions `discounts:read|update`).
5. Verify: create `TEN` (10 %), apply it, the total drops and each line shows its share.
## Conventions
- Codes are stored uppercase (`A–Z 0–9 _ -`); values: percent 1–100, fixed in minor units.
- Deleting a discount deactivates it (redemption history stays).
- Free-shipping adjustments carry their `code` (amount 0), so they count towards usage limits.
- Check the order timeline (`discount_over_limit`) when a usage-limited campaign ends.
- Prefer automatic discounts for store-wide sales; codes for campaigns.
## Don't
- Don't show a "was" price or a percentage off that isn't real: in the EU a price reduction must refer to the lowest
price of the previous 30 days (Omnibus Directive). Use @core/pricing's price history for compare-at prices.
- Don't apply discounts on the client or trust discount amounts sent by the browser.
- Don't create fake urgency (countdowns that reset, "only today" when it isn't).
L’arborescence exacte qui sera injectée, après .genpmignore. Épinglée à
Ce paquet ne déclare aucun serveur MCP.
| Version | Commit | Publié | Analyse |
|---|---|---|---|
| 1.0.1 | 6b23d34 | il y a 3 heures | analyse réussie |
- npm
- zod ^4.0.0
- proposé
- GenPM propose la commande npm et ne l’exécute que si vous acceptez.
- Utilisé par (1)
- @core/kit-store ^1.0.0
- analyse
- analyse réussie · 0 problème
- commit
- v1.0.1 → 6b23d346ddd5be7a9154a7106859687008386baa · vérifié après téléchargement
- scripts
- Aucun. GenPM n’exécute jamais le code des paquets.
- licence
- MIT
- Qualité
- 100/100
- Licence reconnuevalidé
- AGENTS.md explique son objectifvalidé
- AGENTS.md donne les étapes d’intégrationvalidé
- AGENTS.md liste conventions ou interditsvalidé
- Contient des testsvalidé
- Analyse de sécurité réussievalidé
- Publié au cours des 6 derniers moisvalidé
- Éditeur vérifiévalidé
- Résumé et mots-clésvalidé
- signalement
- Vous avez repéré un problème ?