Preços a partir do custo: regras de margem, arredondamento .99, alertas de margem e histórico de preços de 30 dias
Instalar
genpm add @core/pricingO que você recebe
- Código em src/lib/pricing/, 6 arquivos. (16,4 kB)
- Regras de IA em src/lib/pricing/AGENTS.md, mais arquivos de regras da IDE.
- Resolve @core/catalog, @core/contracts, @core/db, @core/money para você.
README
Este pacote não tem README.
Isto é exatamente o que sua IA lê quando trabalha em src/lib/pricing. Nada mais é adicionado ao contexto dela.
@core/pricing — rules for AI agents
Purpose
Turns costs into selling prices and keeps price changes honest. Rules (scope all / category / supplier / product,
cost range, markup in basis points, fixed add-on, charm rounding, minimum margin, priority) pick the most specific
match; repriceProduct applies them to variants with a cost. Every price change goes to price_history, which gives
the EU "prior price" (lowest of the 30 days before the current price) for any sale badge. Tables: price_rules, price_history.
Map
index.ts— public API:upsertPriceRule,priceFromCost,applyRule,repriceProduct,setVariantPrice,priorPrice,honestCompareAt,lowMarginVariants,priceRulesAdminResource.pricing.ts— logic.admin.ts— admin resource.schema.ts— tables.
Integration
- Migrations as in
src/lib/db/AGENTS.md. - Create rules in the admin (
priceRulesAdminResourceinsrc/genpm/admin.ts, permissionspricing:read|update), e.g.markupBps: 15000(+150 %), rounding.99. - Change prices only with
setVariantPrice(variantId, amount)(also from the admin and supplier syncs) so history is kept. - Product pages: show a crossed-out price only with
honestCompareAt(variantId); never with a typed "compare at". - Simulate before applying:
repriceProduct(productId, { apply: false })returns old/new price and margin per variant. - Verify: lower a price,
honestCompareAtreturns the lowest price of the previous 30 days.
Conventions
- Amounts in minor units; markup and margins in basis points (10000 = 100 %).
- Review
lowMarginVariants()after cost changes; items below their rule's minimum must not be sold silently.
Don't
- Don't update
priceAmountdirectly or invent "was" prices; that breaks the 30-day rule (Omnibus Directive). - Don't raise a price shortly before a sale to inflate the discount.
- Don't expose costs or margins in public pages or APIs.
# @core/pricing — rules for AI agents
## Purpose
Turns costs into selling prices and keeps price changes honest. Rules (scope all / category / supplier / product,
cost range, markup in basis points, fixed add-on, charm rounding, minimum margin, priority) pick the most specific
match; `repriceProduct` applies them to variants with a cost. Every price change goes to `price_history`, which gives
the EU "prior price" (lowest of the 30 days before the current price) for any sale badge. Tables: `price_rules`, `price_history`.
## Map
- `index.ts` — public API: `upsertPriceRule`, `priceFromCost`, `applyRule`, `repriceProduct`, `setVariantPrice`, `priorPrice`, `honestCompareAt`, `lowMarginVariants`, `priceRulesAdminResource`.
- `pricing.ts` — logic. `admin.ts` — admin resource. `schema.ts` — tables.
## Integration
1. Migrations as in `src/lib/db/AGENTS.md`.
2. Create rules in the admin (`priceRulesAdminResource` in `src/genpm/admin.ts`, permissions `pricing:read|update`), e.g. `markupBps: 15000` (+150 %), rounding `.99`.
3. Change prices only with `setVariantPrice(variantId, amount)` (also from the admin and supplier syncs) so history is kept.
4. Product pages: show a crossed-out price only with `honestCompareAt(variantId)`; never with a typed "compare at".
5. Simulate before applying: `repriceProduct(productId, { apply: false })` returns old/new price and margin per variant.
6. Verify: lower a price, `honestCompareAt` returns the lowest price of the previous 30 days.
## Conventions
- Amounts in minor units; markup and margins in basis points (10000 = 100 %).
- Review `lowMarginVariants()` after cost changes; items below their rule's minimum must not be sold silently.
## Don't
- Don't update `priceAmount` directly or invent "was" prices; that breaks the 30-day rule (Omnibus Directive).
- Don't raise a price shortly before a sale to inflate the discount.
- Don't expose costs or margins in public pages or APIs.
A árvore exata que será injetada, após o .genpmignore. Fixada em
// Precio de venta desde el coste con reglas, cambios de precio con historial, "precio anterior" honesto y márgenes.
import { and, asc, desc, eq, gt, isNull, lt, or } from 'drizzle-orm';
import { z } from 'zod';
import { categories, productCategories, productVariants, products } from '../catalog/index.ts';
import type { Money } from '../contracts/index.ts';
import { type Executor, getDb, withTransaction } from '../db/index.ts';
import { charmPrice, money } from '../money/index.ts';
import { type PriceRule, priceHistory, priceRules } from './schema.ts';
export const PriceRuleInput = z
.object({
name: z.string().min(1).max(80),
scope: z.enum(['all', 'category', 'supplier', 'product']).default('all'),
scopeValue: z.string().max(120).nullish(),
minCost: z.number().int().min(0).nullish(),
maxCost: z.number().int().min(0).nullish(),
markupBps: z.number().int().min(0).max(100_000),
fixedAdd: z.number().int().min(0).default(0),
rounding: z.enum(['none', '.99', '.95', '.90', '.00']).default('.99'),
minMarginBps: z.number().int().min(0).max(9_900).default(1000),
priority: z.number().int().default(0),
active: z.boolean().default(true),
})
.refine((r) => r.scope === 'all' || !!r.scopeValue, 'scopeValue is required for this scope');
export async function upsertPriceRule(input: z.input<typeof PriceRuleInput>, id?: string, db: Executor = getDb()): Promise<PriceRule> {
const r = PriceRuleInput.parse(input);
const values = { ...r, scopeValue: r.scopeValue ?? null, minCost: r.minCost ?? null, maxCost: r.maxCost ?? null };
const [row] = id ? await db.update(priceRules).set(values).where(eq(priceRules.id, id)).returning() : await db.insert(priceRules).values(values).returning();
return row!;
}
export type PricingContext = { productId?: string; categorySlugs?: string[]; supplierId?: string };
const SPECIFICITY = { product: 3, supplier: 2, category: 1, all: 0 } as const;
/** Regla aplicable: la más específica (producto > proveedor > categoría > todo) y, a igualdad, la de mayor prioridad. */
export async function ruleFor(cost: Money, ctx: PricingContext, db: Executor = getDb()): Promise<PriceRule | null> {
const rules = await db.select().from(priceRules).where(eq(priceRules.active, true));
const fits = rules.filter(
(r) =>
(r.minCost == null || cost.amount >= r.minCost) &&
(r.maxCost == null || cost.amount <= r.maxCost) &&
(r.scope === 'all' ||
(r.scope === 'product' && r.scopeValue === ctx.productId) ||
(r.scope === 'supplier' && r.scopeValue === ctx.supplierId) ||
(r.scope === 'category' && !!ctx.categorySlugs?.includes(r.scopeValue ?? ''))),
);
fits.sort((a, b) => SPECIFICITY[b.scope] - SPECIFICITY[a.scope] || b.priority - a.priority || a.id.localeCompare(b.id));
return fits[0] ?? null;
}
/** Precio = coste × (1 + margen) + fijo, redondeado. */
export function applyRule(cost: Money, rule: Pick<PriceRule, 'markupBps' | 'fixedAdd' | 'rounding'>): Money {
const raw = money(Math.round((cost.amount * (10_000 + rule.markupBps)) / 10_000) + rule.fixedAdd, cost.currency);
return rule.rounding === 'none' ? raw : charmPrice(raw, rule.rounding);
}
/** Margen sobre el precio en puntos básicos (negativo = pérdida). */
export const marginBps = (price: number, cost: number) => (price <= 0 ? -10_000 : Math.round(((price - cost) * 10_000) / price));
export type PriceQuote = { price: Money; rule: PriceRule; marginBps: number; belowMinimum: boolean };
export async function priceFromCost(cost: Money, ctx: PricingContext = {}, db: Executor = getDb()): Promise<PriceQuote | null> {
const rule = await ruleFor(cost, ctx, db);
if (!rule) return null;
const price = applyRule(cost, rule);
const m = marginBps(price.amount, cost.amount);
return { price, rule, marginBps: m, belowMinimum: m < rule.minMarginBps };
}
/**
* Cambia el precio de una variante cerrando el periodo anterior del historial. No hace nada si no cambia.
* Úsalo en lugar de actualizar `priceAmount` directamente.
*/
export async function setVariantPrice(variantId: string, amount: number, db = getDb(), at = new Date()): Promise<boolean> {
return withTransaction(async (tx) => {
const [v] = await tx.select().from(productVariants).where(eq(productVariants.id, variantId)).for('update');
if (!v) throw new Error(`variant ${variantId} not found`);
const [open] = await tx.select().from(priceHistory).where(and(eq(priceHistory.variantId, variantId), isNull(priceHistory.validTo)));
if (v.priceAmount === amount && open) return false;
if (open) await tx.update(priceHistory).set({ validTo: at }).where(eq(priceHistory.id, open.id));
else if (v.priceAmount !== amount) await tx.insert(priceHistory).values({ variantId, amount: v.priceAmount, currency: v.currency, validFrom: v.createdAt, validTo: at });
await tx.insert(priceHistory).values({ variantId, amount, currency: v.currency, validFrom: at });
if (v.priceAmount !== amount) await tx.update(productVariants).set({ priceAmount: amount }).where(eq(productVariants.id, variantId));
return true;
}, db);
}
/**
* Precio anterior a efectos de anunciar una rebaja: el más bajo de los 30 días previos al precio actual
* (Directiva Ómnibus, art. 6 bis). Null si no hay historial suficiente.
*/
export async function priorPrice(variantId: string, db: Executor = getDb()): Promise<Money | null> {
const [current] = await db.select().from(priceHistory).where(and(eq(priceHistory.variantId, variantId), isNull(priceHistory.validTo))).orderBy(desc(priceHistory.validFrom)).limit(1);
if (!current) return null;
const since = new Date(current.validFrom.getTime() - 30 * 86_400_000);
const rows = await db
.select()
.from(priceHistory)
.where(and(eq(priceHistory.variantId, variantId), lt(priceHistory.validFrom, current.validFrom), or(isNull(priceHistory.validTo), gt(priceHistory.validTo, since))))
.orderBy(asc(priceHistory.amount))
.limit(1);
return rows[0] ? money(rows[0].amount, rows[0].currency) : null;
}
/** "Antes X" que se puede mostrar: el precio anterior honesto si es mayor que el actual; si no, nada. */
export async function honestCompareAt(variantId: string, db: Executor = getDb()): Promise<Money | null> {
const [v] = await db.select().from(productVariants).where(eq(productVariants.id, variantId));
const prior = await priorPrice(variantId, db);
return v && prior && prior.amount > v.priceAmount ? prior : null;
}
/**
* Recalcula con las reglas el precio de las variantes con coste de un producto. Devuelve los cambios y las alertas
* de margen. Con `apply: false` solo simula.
*/
export async function repriceProduct(productId: string, opts: { apply?: boolean; supplierId?: string } = {}, db = getDb()) {
const cats = await db.select({ slug: categories.slug }).from(productCategories).innerJoin(categories, eq(categories.id, productCategories.categoryId)).where(eq(productCategories.productId, productId));
const [p] = await db.select({ id: products.id }).from(products).where(eq(products.id, productId));
if (!p) throw new Error(`product ${productId} not found`);
const variants = await db.select().from(productVariants).where(and(eq(productVariants.productId, productId), eq(productVariants.active, true)));
const changes: Array<{ variantId: string; from: number; to: number; marginBps: number; belowMinimum: boolean }> = [];
for (const v of variants) {
if (v.costAmount == null) continue;
const q = await priceFromCost(money(v.costAmount, v.currency), { productId, categorySlugs: cats.map((c) => c.slug), supplierId: opts.supplierId }, db);
if (!q) continue;
changes.push({ variantId: v.id, from: v.priceAmount, to: q.price.amount, marginBps: q.marginBps, belowMinimum: q.belowMinimum });
if (opts.apply !== false && q.price.amount !== v.priceAmount) await setVariantPrice(v.id, q.price.amount, db);
}
return changes;
}
/** Variantes activas que se venden con margen por debajo de `minBps` (o con pérdida). */
export async function lowMarginVariants(minBps = 0, db: Executor = getDb()) {
const rows = await db
.select({ id: productVariants.id, sku: productVariants.sku, price: productVariants.priceAmount, cost: productVariants.costAmount, product: products.name, status: products.status })
.from(productVariants)
.innerJoin(products, eq(products.id, productVariants.productId))
.where(eq(productVariants.active, true));
return rows.filter((r) => r.cost != null && marginBps(r.price, r.cost) < minBps).map((r) => ({ ...r, marginBps: marginBps(r.price, r.cost!) }));
}
Este pacote não declara servidores MCP.
| Versão | Commit | Publicado | Análise |
|---|---|---|---|
| 1.0.0 | 6aa4077 | há 3 horas | análise aprovada |
- npm
- zod ^4.0.0
- proposto
- O GenPM propõe o comando npm e só o executa se você disser sim.
- Usado por (2)
- @core/kit-dropshipping ^1.0.0@core/suppliers ^1.0.0
- análise
- análise aprovada · 0 achados
- commit
- v1.0.0 → 6aa40774c5407480451f9b9f74d4e4e0577c06df · verificado após o download
- scripts
- Nenhum. O GenPM nunca executa código de pacotes.
- licença
- MIT
- Qualidade
- 100/100
- Licença reconhecidacumprido
- AGENTS.md explica o propósitocumprido
- AGENTS.md tem passos de integraçãocumprido
- AGENTS.md lista convenções ou proibiçõescumprido
- Inclui testescumprido
- Escaneamento de segurança aprovadocumprido
- Publicado nos últimos 6 mesescumprido
- Publicador verificadocumprido
- Resumo e palavras-chavecumprido
- denúncia
- Viu algo errado?