rapida-partner 8.14.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 +1 -1
- package/src/interfaces/cash.interface.ts +31 -0
- package/src/interfaces/project.interface.ts +4 -0
- package/src/interfaces/store-order.interface.ts +20 -0
- package/src/interfaces/store-settings.interface.ts +9 -0
- package/src/schemas/module.ref.json +7 -0
- package/src/schemas/store-settings.ref.json +11 -0
- package/src/utils/cash.test.ts +98 -0
- package/src/utils/cash.ts +81 -0
- package/src/utils/store-payment.test.ts +72 -0
- package/src/utils/store-payment.ts +49 -0
package/package.json
CHANGED
|
@@ -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[];
|
|
@@ -13,6 +13,22 @@ export type TStoreDeliveryMethod = "delivery" | "pickup";
|
|
|
13
13
|
/** Como o cliente paga: online (Mercado Pago) ou na entrega/retirada. */
|
|
14
14
|
export type TStorePaymentMethod = "online" | "onDelivery";
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Forma de pagamento detalhada escolhida no pedido (balcão, mesa ou checkout).
|
|
18
|
+
* Só aparecem as formas que o cliente ligou em IStoreSettings.paymentChoices.
|
|
19
|
+
* - "debitCard" e "creditCard": na maquininha.
|
|
20
|
+
* - "pixTerminal": Pix na maquininha (com taxa e prazo D+0).
|
|
21
|
+
* - "pixDirect": Pix direto para a chave da loja (IStoreSettings.pixKey), sem taxa.
|
|
22
|
+
* - "cash": dinheiro, com troco em changeFor.
|
|
23
|
+
*/
|
|
24
|
+
export type TStorePaymentChoice = "debitCard" | "creditCard" | "pixTerminal" | "pixDirect" | "cash";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Destino do pedido na mesa: comer no local ou levar para casa. Obrigatório
|
|
28
|
+
* no checkout da mesa; sem ele o pedido não é confirmado.
|
|
29
|
+
*/
|
|
30
|
+
export type TStoreConsumptionMode = "dineIn" | "takeaway";
|
|
31
|
+
|
|
16
32
|
/**
|
|
17
33
|
* Meio do pagamento online, informado pelo Mercado Pago (Fase 8.1), para o
|
|
18
34
|
* vendedor ver "Pago no Pix" ou "Pago no cartão". Veja storeOnlinePaymentTypeOf.
|
|
@@ -129,6 +145,10 @@ export interface IStoreOrder {
|
|
|
129
145
|
// pagamento: changeFor é o valor para o troco, e o vendedor marca
|
|
130
146
|
// paymentReceived quando receber.
|
|
131
147
|
paymentMethod?: TStorePaymentMethod;
|
|
148
|
+
// Forma detalhada escolhida (Pagamentos por pedido, fase 1); vazio em pedidos anteriores.
|
|
149
|
+
paymentChoice?: TStorePaymentChoice;
|
|
150
|
+
// Destino na mesa (fase 1): vazio fora da mesa.
|
|
151
|
+
consumptionMode?: TStoreConsumptionMode;
|
|
132
152
|
changeFor?: number;
|
|
133
153
|
paymentReceived?: boolean;
|
|
134
154
|
// Meio do pagamento online (Fase 8.1); vazio no pagamento na entrega.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { TStorePaymentChoice } from "./store-order.interface";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Funcionalidades opcionais da loja (`skeleton` "ecommerce" ou "marketplace").
|
|
3
5
|
* Cada uma tem duas camadas: `IProjectStore.features` decide se o código dela é
|
|
@@ -155,6 +157,13 @@ export interface IStoreSettings extends Partial<Record<TStoreFeatureToggle, bool
|
|
|
155
157
|
// a loja deixa de oferecer o pagamento online. Só vale com payOnDeliveryEnabled ligado,
|
|
156
158
|
// para a loja nunca ficar sem forma de pagamento.
|
|
157
159
|
payOnDeliveryOnly?: boolean;
|
|
160
|
+
// Formas de pagamento detalhadas que o cliente liga para o balcão, a mesa e o checkout
|
|
161
|
+
// (Pagamentos por pedido, fase 1). Sem a lista, nenhuma forma detalhada aparece.
|
|
162
|
+
paymentChoices?: TStorePaymentChoice[];
|
|
163
|
+
// Formas da entrega que a loja liga (maquininha e dinheiro). Obrigatória quando o pagamento na entrega está ligado.
|
|
164
|
+
deliveryPaymentChoices?: TStorePaymentChoice[];
|
|
165
|
+
// Chave Pix da loja, usada pela forma "pixDirect" (sem taxa, com confirmação manual pelo vendedor).
|
|
166
|
+
pixKey?: string;
|
|
158
167
|
// Funcionalidade "stockReservation": por quantos minutos o pedido aguardando
|
|
159
168
|
// pagamento segura o estoque (de 5 a 60). Vencido o prazo, o pedido é
|
|
160
169
|
// cancelado, o link de pagamento expira e a quantidade volta. Sem o campo,
|
|
@@ -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": {
|
|
@@ -80,6 +80,17 @@
|
|
|
80
80
|
},
|
|
81
81
|
"minimumOrderValue": { "type": "number", "minimum": 0 },
|
|
82
82
|
"payOnDeliveryOnly": { "type": "boolean" },
|
|
83
|
+
"paymentChoices": {
|
|
84
|
+
"type": "array",
|
|
85
|
+
"uniqueItems": true,
|
|
86
|
+
"items": { "enum": ["debitCard", "creditCard", "pixTerminal", "pixDirect", "cash"] }
|
|
87
|
+
},
|
|
88
|
+
"deliveryPaymentChoices": {
|
|
89
|
+
"type": "array",
|
|
90
|
+
"uniqueItems": true,
|
|
91
|
+
"items": { "enum": ["debitCard", "creditCard", "pixTerminal", "cash"] }
|
|
92
|
+
},
|
|
93
|
+
"pixKey": { "type": "string" },
|
|
83
94
|
"reservationMinutes": { "type": "integer", "minimum": 5, "maximum": 60 },
|
|
84
95
|
"ordersPaused": { "type": "boolean" },
|
|
85
96
|
"aboutText": { "type": "string" },
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import settingsSchema from "../schemas/store-settings.ref.json";
|
|
3
|
+
import {
|
|
4
|
+
isStorePaymentChoiceAllowed,
|
|
5
|
+
STORE_DELIVERY_PAYMENT_CHOICES,
|
|
6
|
+
STORE_PAYMENT_CHOICES,
|
|
7
|
+
storeDeliveryPaymentChoicesOf,
|
|
8
|
+
storePaymentChoicesOf,
|
|
9
|
+
} from "./store-payment";
|
|
10
|
+
|
|
11
|
+
describe("storePaymentChoicesOf", () => {
|
|
12
|
+
test("mostra só as formas ligadas, na ordem do balcão", () => {
|
|
13
|
+
expect(storePaymentChoicesOf({ paymentChoices: ["creditCard", "cash"], pixKey: "chave" })).toEqual(["cash", "creditCard"]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("sem lista, nenhuma forma detalhada aparece", () => {
|
|
17
|
+
expect(storePaymentChoicesOf({})).toEqual([]);
|
|
18
|
+
expect(storePaymentChoicesOf({ paymentChoices: [] })).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("Pix direto some sem chave da loja, mesmo ligado", () => {
|
|
22
|
+
expect(storePaymentChoicesOf({ paymentChoices: ["pixDirect", "debitCard"] })).toEqual(["debitCard"]);
|
|
23
|
+
expect(storePaymentChoicesOf({ paymentChoices: ["pixDirect"], pixKey: " " })).toEqual([]);
|
|
24
|
+
expect(storePaymentChoicesOf({ paymentChoices: ["pixDirect"], pixKey: "11999999999" })).toEqual(["pixDirect"]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("repetição na lista não duplica a forma", () => {
|
|
28
|
+
expect(storePaymentChoicesOf({ paymentChoices: ["cash", "cash"] })).toEqual(["cash"]);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("storeDeliveryPaymentChoicesOf", () => {
|
|
33
|
+
test("só as formas da entrega, na ordem do checkout; Pix direto nunca entra", () => {
|
|
34
|
+
expect(storeDeliveryPaymentChoicesOf({ deliveryPaymentChoices: ["cash", "debitCard"] })).toEqual(["debitCard", "cash"]);
|
|
35
|
+
expect(storeDeliveryPaymentChoicesOf({ deliveryPaymentChoices: ["pixDirect" as never] })).toEqual([]);
|
|
36
|
+
expect(storeDeliveryPaymentChoicesOf({})).toEqual([]);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("isStorePaymentChoiceAllowed", () => {
|
|
41
|
+
test("no balcão e na mesa, aceita só forma ligada e com destino", () => {
|
|
42
|
+
const settings = { paymentChoices: ["pixTerminal" as const, "pixDirect" as const] };
|
|
43
|
+
expect(isStorePaymentChoiceAllowed("pixTerminal", settings, "counter")).toBe(true);
|
|
44
|
+
expect(isStorePaymentChoiceAllowed("pixDirect", settings, "counter")).toBe(false);
|
|
45
|
+
expect(isStorePaymentChoiceAllowed("cash", settings, "counter")).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("na entrega, vale deliveryPaymentChoices, não paymentChoices", () => {
|
|
49
|
+
const settings = { paymentChoices: ["debitCard" as const], deliveryPaymentChoices: ["cash" as const] };
|
|
50
|
+
expect(isStorePaymentChoiceAllowed("cash", settings, "delivery")).toBe(true);
|
|
51
|
+
expect(isStorePaymentChoiceAllowed("cash", settings, "counter")).toBe(false);
|
|
52
|
+
expect(isStorePaymentChoiceAllowed("debitCard", settings, "delivery")).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("na entrega, Pix direto nunca vale, mesmo com chave da loja", () => {
|
|
56
|
+
const settings = { deliveryPaymentChoices: ["pixDirect" as never], pixKey: "11999999999" };
|
|
57
|
+
expect(isStorePaymentChoiceAllowed("pixDirect", settings, "delivery")).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("store-settings.ref.json", () => {
|
|
62
|
+
const enumOf = (property: "paymentChoices" | "deliveryPaymentChoices"): string[] =>
|
|
63
|
+
[...settingsSchema.properties[property].items.enum].sort();
|
|
64
|
+
|
|
65
|
+
test("o enum de paymentChoices tem as mesmas formas de STORE_PAYMENT_CHOICES", () => {
|
|
66
|
+
expect(enumOf("paymentChoices")).toEqual([...STORE_PAYMENT_CHOICES].sort());
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("o enum de deliveryPaymentChoices tem as mesmas formas de STORE_DELIVERY_PAYMENT_CHOICES", () => {
|
|
70
|
+
expect(enumOf("deliveryPaymentChoices")).toEqual([...STORE_DELIVERY_PAYMENT_CHOICES].sort());
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { TStoreConsumptionMode, TStorePaymentChoice } from "../interfaces/store-order.interface";
|
|
2
|
+
import type { IStoreSettings } from "../interfaces/store-settings.interface";
|
|
3
|
+
|
|
4
|
+
/** Formas detalhadas de pagamento, na ordem em que aparecem no balcão e no checkout. */
|
|
5
|
+
export const STORE_PAYMENT_CHOICES: TStorePaymentChoice[] = ["cash", "debitCard", "creditCard", "pixTerminal", "pixDirect"];
|
|
6
|
+
|
|
7
|
+
/** Formas que existem na entrega (maquininha e dinheiro), na ordem do checkout. Pix direto não entra. */
|
|
8
|
+
export const STORE_DELIVERY_PAYMENT_CHOICES: TStorePaymentChoice[] = ["debitCard", "creditCard", "pixTerminal", "cash"];
|
|
9
|
+
|
|
10
|
+
/** Destinos do pedido na mesa, na ordem do checkout. */
|
|
11
|
+
export const STORE_CONSUMPTION_MODES: TStoreConsumptionMode[] = ["dineIn", "takeaway"];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Formas que a loja aceita de fato: só as ligadas em paymentChoices, sem repetição
|
|
15
|
+
* e na ordem de STORE_PAYMENT_CHOICES. "pixDirect" só entra com pixKey preenchida,
|
|
16
|
+
* para a loja nunca mostrar um Pix sem destino.
|
|
17
|
+
*/
|
|
18
|
+
export const storePaymentChoicesOf = (settings: Pick<IStoreSettings, "paymentChoices" | "pixKey">): TStorePaymentChoice[] => {
|
|
19
|
+
const enabled = new Set(settings.paymentChoices ?? []);
|
|
20
|
+
const hasPixKey = (settings.pixKey ?? "").trim().length > 0;
|
|
21
|
+
return STORE_PAYMENT_CHOICES.filter((choice) => enabled.has(choice) && (choice !== "pixDirect" || hasPixKey));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Formas da entrega que a loja liga (deliveryPaymentChoices), sem repetição e na ordem do checkout.
|
|
26
|
+
* Pix direto nunca entra: a entrega só aceita maquininha e dinheiro.
|
|
27
|
+
*/
|
|
28
|
+
export const storeDeliveryPaymentChoicesOf = (settings: Pick<IStoreSettings, "deliveryPaymentChoices">): TStorePaymentChoice[] => {
|
|
29
|
+
const enabled = new Set(settings.deliveryPaymentChoices ?? []);
|
|
30
|
+
return STORE_DELIVERY_PAYMENT_CHOICES.filter((choice) => enabled.has(choice));
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Onde o pedido é pago: "counter" no balcão e na mesa (paymentChoices) e
|
|
35
|
+
* "delivery" na entrega (deliveryPaymentChoices).
|
|
36
|
+
*/
|
|
37
|
+
export type TStorePaymentContext = "counter" | "delivery";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Vale gravar a forma no pedido: ela precisa estar entre as formas que a loja aceita
|
|
41
|
+
* onde o pedido é pago. No balcão e na mesa valem as de paymentChoices; na entrega,
|
|
42
|
+
* as de deliveryPaymentChoices.
|
|
43
|
+
*/
|
|
44
|
+
export const isStorePaymentChoiceAllowed = (
|
|
45
|
+
choice: TStorePaymentChoice,
|
|
46
|
+
settings: Pick<IStoreSettings, "paymentChoices" | "deliveryPaymentChoices" | "pixKey">,
|
|
47
|
+
context: TStorePaymentContext,
|
|
48
|
+
): boolean =>
|
|
49
|
+
(context === "delivery" ? storeDeliveryPaymentChoicesOf(settings) : storePaymentChoicesOf(settings)).includes(choice);
|