rapida-partner 8.15.0 → 8.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rapida-partner",
3
- "version": "8.15.0",
3
+ "version": "8.16.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "index.ts",
6
6
  "files": [
@@ -0,0 +1,31 @@
1
+ /** Suprimento (entra dinheiro no caixa) ou sangria (sai dinheiro do caixa). */
2
+ export type TCashMovementKind = "supply" | "withdrawal";
3
+
4
+ /**
5
+ * Movimento do caixa durante o turno (Fase 2 do PDV). approvedBy só existe quando o
6
+ * valor passou do limite da loja (IStoreSettings.cashApprovalLimit) e o gerente aprovou.
7
+ */
8
+ export interface ICashMovement {
9
+ kind: TCashMovementKind;
10
+ amount: number;
11
+ reason: string;
12
+ createdBy: string;
13
+ approvedBy?: string;
14
+ createdAt: string;
15
+ }
16
+
17
+ /**
18
+ * Turno de um caixa. Um caixa só pode ter um turno aberto. cashSales soma as vendas em
19
+ * dinheiro do turno; countedAmount e justification existem depois do fechamento.
20
+ */
21
+ export interface ICashShift {
22
+ registerName: string;
23
+ openedBy: string;
24
+ openedAt: string;
25
+ openingFloat: number;
26
+ cashSales: number;
27
+ movements: ICashMovement[];
28
+ closedAt?: string;
29
+ countedAmount?: number;
30
+ justification?: string;
31
+ }
@@ -253,6 +253,10 @@ export interface IModule {
253
253
  // Quadro de pedidos do vendedor (Fase 8): no módulo de pedidos da loja
254
254
  // (getStoreOrdersModule), o gerador cria as rotas e a tela do quadro.
255
255
  storeOrdersBoard?: IStoreOrdersBoard;
256
+ // Caixa e turno do balcão (Fase 2 do PDV): o gerador cria as rotas (abrir caixa, venda, suprimento e sangria,
257
+ // fechamento) no backend do vendedor. Exige flows.authentication e flows.permission. approvalLimit: acima deste
258
+ // valor, suprimento e sangria pedem login e senha de quem tem papel 'owner' ou 'manager' (sem valor, sempre pedem).
259
+ cashShift?: { approvalLimit?: number };
256
260
  components: IComponent[];
257
261
  businessRules?: IBusinessRule[];
258
262
  todo?: ITodo[];
@@ -30,6 +30,13 @@
30
30
  "required": ["segment"],
31
31
  "additionalProperties": false
32
32
  },
33
+ "cashShift": {
34
+ "type": "object",
35
+ "properties": {
36
+ "approvalLimit": { "type": "number", "minimum": 0 }
37
+ },
38
+ "additionalProperties": false
39
+ },
33
40
  "components": {
34
41
  "type": "array",
35
42
  "items": {
@@ -0,0 +1,98 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { ICashShift } from "../interfaces/cash.interface";
3
+ import { canOpenShift, closeCashShift, cashDifferenceOf, expectedCashOf, recordCashMovement, withCashSale } from "./cash";
4
+
5
+ const openShift = (openingFloat: number): ICashShift => ({
6
+ registerName: "Caixa 1",
7
+ openedBy: "ana",
8
+ openedAt: "2026-11-28T08:00:00.000Z",
9
+ openingFloat,
10
+ cashSales: 0,
11
+ movements: [],
12
+ });
13
+
14
+ const at = "2026-11-28T10:00:00.000Z";
15
+
16
+ describe("abrir caixa", () => {
17
+ test("um turno aberto por caixa: não abre se o caixa já tem turno", () => {
18
+ expect(canOpenShift(false)).toBe(true);
19
+ expect(canOpenShift(true)).toBe(false);
20
+ });
21
+ });
22
+
23
+ describe("venda em dinheiro entra no turno", () => {
24
+ test("soma no turno aberto e recusa com o turno fechado", () => {
25
+ const sold = withCashSale(openShift(100), 24);
26
+ expect(sold.ok && sold.value.cashSales).toBe(24);
27
+ const closed = closeCashShift(openShift(100), 100, at);
28
+ if (!closed.ok) throw new Error("fechamento deveria passar");
29
+ expect(withCashSale(closed.value, 10)).toEqual({ ok: false, reason: "shiftClosed" });
30
+ });
31
+ });
32
+
33
+ describe("suprimento e sangria: limite da loja", () => {
34
+ test("abaixo do limite, a equipe registra sem gerente e fica quem fez", () => {
35
+ const result = recordCashMovement(openShift(100), { kind: "withdrawal", amount: 100, reason: "troco", createdBy: "ana", createdAt: at, approvalLimit: 300 });
36
+ expect(result.ok).toBe(true);
37
+ if (!result.ok) return;
38
+ expect(result.value.movements[0]).toEqual({ kind: "withdrawal", amount: 100, reason: "troco", createdBy: "ana", createdAt: at });
39
+ });
40
+
41
+ test("acima do limite, sem gerente, bloqueia", () => {
42
+ const result = recordCashMovement(openShift(100), { kind: "withdrawal", amount: 500, reason: "pagamento", createdBy: "ana", createdAt: at, approvalLimit: 300 });
43
+ expect(result).toEqual({ ok: false, reason: "managerApprovalRequired" });
44
+ });
45
+
46
+ test("acima do limite, com gerente, registra quem aprovou", () => {
47
+ const result = recordCashMovement(openShift(100), { kind: "withdrawal", amount: 500, reason: "pagamento", createdBy: "ana", approvedBy: "carlos", createdAt: at, approvalLimit: 300 });
48
+ expect(result.ok && result.value.movements[0]?.approvedBy).toBe("carlos");
49
+ });
50
+
51
+ test("sem limite configurado, sempre pede gerente; motivo e valor são obrigatórios", () => {
52
+ expect(recordCashMovement(openShift(100), { kind: "supply", amount: 10, reason: "x", createdBy: "ana", createdAt: at })).toEqual({ ok: false, reason: "managerApprovalRequired" });
53
+ expect(recordCashMovement(openShift(100), { kind: "supply", amount: 10, reason: " ", createdBy: "ana", createdAt: at, approvedBy: "carlos" })).toEqual({ ok: false, reason: "reasonRequired" });
54
+ expect(recordCashMovement(openShift(100), { kind: "supply", amount: 0, reason: "x", createdBy: "ana", createdAt: at, approvedBy: "carlos" })).toEqual({ ok: false, reason: "invalidAmount" });
55
+ });
56
+ });
57
+
58
+ describe("dinheiro esperado e fechamento", () => {
59
+ test("esperado = fundo + vendas em dinheiro + suprimentos − sangrias", () => {
60
+ let shift = openShift(100);
61
+ const sold = withCashSale(shift, 80);
62
+ if (!sold.ok) throw new Error("venda deveria passar");
63
+ shift = sold.value;
64
+ const supply = recordCashMovement(shift, { kind: "supply", amount: 20, reason: "reforço", createdBy: "ana", createdAt: at, approvalLimit: 300 });
65
+ if (!supply.ok) throw new Error("suprimento deveria passar");
66
+ const withdrawal = recordCashMovement(supply.value, { kind: "withdrawal", amount: 30, reason: "troco", createdBy: "ana", createdAt: at, approvalLimit: 300 });
67
+ if (!withdrawal.ok) throw new Error("sangria deveria passar");
68
+ expect(expectedCashOf(withdrawal.value)).toBe(170);
69
+ expect(cashDifferenceOf(withdrawal.value, 165)).toBe(-5);
70
+ });
71
+
72
+ test("diferença de R$ 5,00 sem justificativa não fecha", () => {
73
+ const shift = withCashSale(openShift(100), 80);
74
+ if (!shift.ok) throw new Error("venda deveria passar");
75
+ expect(closeCashShift(shift.value, 175, at)).toEqual({ ok: false, reason: "justificationRequired" });
76
+ expect(closeCashShift(shift.value, 175, at, " ")).toEqual({ ok: false, reason: "justificationRequired" });
77
+ });
78
+
79
+ test("diferença com justificativa fecha e guarda o contado e a justificativa", () => {
80
+ const shift = withCashSale(openShift(100), 80);
81
+ if (!shift.ok) throw new Error("venda deveria passar");
82
+ const closed = closeCashShift(shift.value, 175, at, "troco errado");
83
+ expect(closed.ok && closed.value.countedAmount).toBe(175);
84
+ expect(closed.ok && closed.value.justification).toBe("troco errado");
85
+ });
86
+
87
+ test("sem diferença, fecha sem justificativa", () => {
88
+ const closed = closeCashShift(openShift(100), 100, at);
89
+ expect(closed.ok).toBe(true);
90
+ expect(closed.ok && closed.value.justification).toBeUndefined();
91
+ });
92
+
93
+ test("turno fechado não aceita mais movimento", () => {
94
+ const closed = closeCashShift(openShift(100), 100, at);
95
+ if (!closed.ok) throw new Error("fechamento deveria passar");
96
+ expect(recordCashMovement(closed.value, { kind: "supply", amount: 10, reason: "x", createdBy: "ana", createdAt: at, approvedBy: "carlos" })).toEqual({ ok: false, reason: "shiftClosed" });
97
+ });
98
+ });
@@ -0,0 +1,81 @@
1
+ import type { ICashMovement, ICashShift, TCashMovementKind } from "../interfaces/cash.interface";
2
+
3
+ export type TCashResult<T> = { ok: true; value: T } | { ok: false; reason: TCashRefusal };
4
+
5
+ export type TCashRefusal = "shiftClosed" | "invalidAmount" | "reasonRequired" | "managerApprovalRequired" | "justificationRequired";
6
+
7
+ const cents = (value: number): number => Math.round(value * 100) / 100;
8
+
9
+ /** Sem limite configurado, qualquer movimento pede aprovação do gerente. */
10
+ export const cashMovementNeedsApproval = (amount: number, approvalLimit: number | undefined): boolean =>
11
+ approvalLimit === undefined || amount > approvalLimit;
12
+
13
+ /** Dinheiro esperado na gaveta: fundo + vendas em dinheiro + suprimentos − sangrias. */
14
+ export const expectedCashOf = (shift: ICashShift): number => {
15
+ const movementTotal = (kind: TCashMovementKind): number =>
16
+ shift.movements.filter((movement: ICashMovement) => movement.kind === kind).reduce((sum, movement) => sum + movement.amount, 0);
17
+ return cents(shift.openingFloat + shift.cashSales + movementTotal("supply") - movementTotal("withdrawal"));
18
+ };
19
+
20
+ /** Diferença do fechamento: contado − esperado (negativa = falta dinheiro). */
21
+ export const cashDifferenceOf = (shift: ICashShift, counted: number): number => cents(counted - expectedCashOf(shift));
22
+
23
+ /** Um turno aberto por caixa: abrir é permitido só se o caixa não tem turno aberto. */
24
+ export const canOpenShift = (registerHasOpenShift: boolean): boolean => !registerHasOpenShift;
25
+
26
+ /** Registra uma venda em dinheiro no turno aberto. */
27
+ export const withCashSale = (shift: ICashShift, amount: number): TCashResult<ICashShift> => {
28
+ if (shift.closedAt) return { ok: false, reason: "shiftClosed" };
29
+ if (!(amount > 0)) return { ok: false, reason: "invalidAmount" };
30
+ return { ok: true, value: { ...shift, cashSales: cents(shift.cashSales + amount) } };
31
+ };
32
+
33
+ export interface ICashMovementInput {
34
+ kind: TCashMovementKind;
35
+ amount: number;
36
+ reason: string;
37
+ createdBy: string;
38
+ createdAt: string;
39
+ approvedBy?: string;
40
+ approvalLimit?: number;
41
+ }
42
+
43
+ /**
44
+ * Suprimento ou sangria. Acima do limite da loja, exige o gerente (approvedBy); abaixo,
45
+ * a própria equipe registra. Quem fez e, se houver, quem aprovou ficam no movimento.
46
+ */
47
+ export const recordCashMovement = (shift: ICashShift, input: ICashMovementInput): TCashResult<ICashShift> => {
48
+ if (shift.closedAt) return { ok: false, reason: "shiftClosed" };
49
+ if (!(input.amount > 0)) return { ok: false, reason: "invalidAmount" };
50
+ if (!input.reason.trim()) return { ok: false, reason: "reasonRequired" };
51
+ const needsApproval = cashMovementNeedsApproval(input.amount, input.approvalLimit);
52
+ if (needsApproval && !input.approvedBy) return { ok: false, reason: "managerApprovalRequired" };
53
+ const movement: ICashMovement = {
54
+ kind: input.kind,
55
+ amount: cents(input.amount),
56
+ reason: input.reason.trim(),
57
+ createdBy: input.createdBy,
58
+ createdAt: input.createdAt,
59
+ ...(needsApproval && input.approvedBy ? { approvedBy: input.approvedBy } : {}),
60
+ };
61
+ return { ok: true, value: { ...shift, movements: [...shift.movements, movement] } };
62
+ };
63
+
64
+ /**
65
+ * Fechamento: compara contado com esperado. Diferença diferente de zero exige justificativa;
66
+ * depois de fechado, o turno não aceita mais movimento.
67
+ */
68
+ export const closeCashShift = (shift: ICashShift, counted: number, closedAt: string, justification?: string): TCashResult<ICashShift> => {
69
+ if (shift.closedAt) return { ok: false, reason: "shiftClosed" };
70
+ const difference = cashDifferenceOf(shift, counted);
71
+ if (difference !== 0 && !(justification ?? "").trim()) return { ok: false, reason: "justificationRequired" };
72
+ return {
73
+ ok: true,
74
+ value: {
75
+ ...shift,
76
+ closedAt,
77
+ countedAmount: cents(counted),
78
+ ...(difference !== 0 ? { justification: (justification ?? "").trim() } : {}),
79
+ },
80
+ };
81
+ };