@revenexx/cover 0.2.40 → 0.2.42
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/app/api/account.ts +6 -0
- package/app/app.config.ts +9 -0
- package/app/components/account/quotes/AccountQuoteDetail.vue +428 -0
- package/app/components/account/quotes/AccountQuotesTable.vue +157 -0
- package/app/components/cart/quote/CartQuoteRequestModal.vue +146 -0
- package/app/components/cart/view/CartSummaryPanel.vue +27 -0
- package/app/components/cart/view/CartView.vue +28 -1
- package/app/composables/useAccountQuotes.ts +75 -0
- package/app/config/navigation.ts +1 -1
- package/app/config/quickActions.ts +1 -0
- package/app/interfaces/account/quotes.ts +107 -0
- package/app/interfaces/cart-calculation.ts +6 -0
- package/i18n/locales/de/account.json +95 -0
- package/i18n/locales/de/cart.json +21 -0
- package/i18n/locales/en/account.json +95 -0
- package/i18n/locales/en/cart.json +21 -0
- package/package.json +1 -1
- package/server/api/account/quotes/[id]/decline.post.ts +39 -0
- package/server/api/account/quotes/[id]/order.post.ts +153 -0
- package/server/api/account/quotes/[id].get.ts +11 -0
- package/server/api/account/quotes.get.ts +26 -0
- package/server/api/quotes/request.post.ts +176 -0
- package/server/data/account/quotes.json +34 -0
- package/server/utils/quotesService.ts +274 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
|
|
3
|
+
import type { AccountQuote, AccountQuoteLine, AccountQuoteStatus } from "../../app/interfaces/account/quotes";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Quotes — the storefront's side of the quotes app (RX-783).
|
|
7
|
+
*
|
|
8
|
+
* A buyer whose cart cannot be ordered as it is (a consignment over the parcel
|
|
9
|
+
* limit, a price on request) sends it in as a quotation request; the merchant
|
|
10
|
+
* prices it in the Cockpit; the buyer orders it from the account at the quoted
|
|
11
|
+
* figures. The app owns the record and its money; this file only talks to it.
|
|
12
|
+
*
|
|
13
|
+
* Registry domain `quotesService`: `api` calls the gateway (`/v1/quotes/…`),
|
|
14
|
+
* `mock` keeps the same records in the demo data store, so both modes share one
|
|
15
|
+
* shape and one mapping onto the account contract.
|
|
16
|
+
*
|
|
17
|
+
* Whose quotes: the storefront does not yet send the gateway a principal
|
|
18
|
+
* assertion, so the app cannot narrow a call to the buyer by itself. This file
|
|
19
|
+
* does it — every list is filtered by the buyer's organisation, every read and
|
|
20
|
+
* answer checks ownership first, and details are read as the customer sees
|
|
21
|
+
* them (`audience=customer`), so an internal note never reaches the page.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export function resolveQuotesServiceKey(event?: H3Event): string {
|
|
25
|
+
return resolveServiceKey(event, { domain: "quotesService", mockKey: "mock", liveKey: "api" });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A quote record as the quotes app answers it (detail aggregate). */
|
|
29
|
+
export interface LiveQuote {
|
|
30
|
+
id: string;
|
|
31
|
+
number: string;
|
|
32
|
+
status: AccountQuoteStatus;
|
|
33
|
+
origin?: string;
|
|
34
|
+
cart_id?: string | null;
|
|
35
|
+
contact_id?: string | null;
|
|
36
|
+
organization_id?: string | null;
|
|
37
|
+
currency: string;
|
|
38
|
+
reason?: string | null;
|
|
39
|
+
buyer_note?: string | null;
|
|
40
|
+
seller_note?: string | null;
|
|
41
|
+
list_total?: number | string | null;
|
|
42
|
+
quoted_total?: number | string | null;
|
|
43
|
+
shipping_amount?: number | string | null;
|
|
44
|
+
shipping_tax_rate?: number | string | null;
|
|
45
|
+
grand_total?: number | string | null;
|
|
46
|
+
valid_until?: string | null;
|
|
47
|
+
requested_at?: string | null;
|
|
48
|
+
quoted_at?: string | null;
|
|
49
|
+
created_at: string;
|
|
50
|
+
buyer?: Record<string, unknown> | null;
|
|
51
|
+
billing_address?: Record<string, unknown> | null;
|
|
52
|
+
shipping_address?: Record<string, unknown> | null;
|
|
53
|
+
items?: LiveQuoteItem[];
|
|
54
|
+
events?: Array<{ kind: string; actor?: string | null; body?: string | null; visibility?: string; created_at: string }>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface LiveQuoteItem {
|
|
58
|
+
id: string;
|
|
59
|
+
product_id?: string | null;
|
|
60
|
+
sku?: string | null;
|
|
61
|
+
name: string;
|
|
62
|
+
quantity: number | string;
|
|
63
|
+
unit?: string | null;
|
|
64
|
+
list_price?: number | string | null;
|
|
65
|
+
quoted_price?: number | string | null;
|
|
66
|
+
tax_rate?: number | string | null;
|
|
67
|
+
line_total?: number | string | null;
|
|
68
|
+
decision?: "open" | "accepted" | "declined";
|
|
69
|
+
order_id?: string | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** What `accept` hands over, shaped the way `orders/place` takes it. */
|
|
73
|
+
export interface LiveOrderDraft {
|
|
74
|
+
currency: string;
|
|
75
|
+
contact_id?: string;
|
|
76
|
+
organization_id?: string;
|
|
77
|
+
cart_id?: string;
|
|
78
|
+
buyer?: Record<string, unknown>;
|
|
79
|
+
billing_address?: Record<string, unknown>;
|
|
80
|
+
shipping_address?: Record<string, unknown>;
|
|
81
|
+
shipping?: { method: string; price: number; tax_rate: number };
|
|
82
|
+
items: Array<{ product_id?: string; sku?: string; name: string; quantity: number; unit_price: number; tax_rate?: number }>;
|
|
83
|
+
metadata?: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const num = (value: unknown): number | null => {
|
|
87
|
+
if (value === null || value === undefined || value === "") {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const n = Number(value);
|
|
91
|
+
return Number.isFinite(n) ? n : null;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** The acting buyer: session cookie first, the resolved user as fallback. */
|
|
95
|
+
export async function quoteActor(event: H3Event): Promise<{
|
|
96
|
+
contactId?: string;
|
|
97
|
+
organizationId?: string;
|
|
98
|
+
user: Awaited<ReturnType<ReturnType<typeof getAuthService>["me"]>>;
|
|
99
|
+
}> {
|
|
100
|
+
const refs = sessionOrderRefs(event);
|
|
101
|
+
const user = await getAuthService(event).me(event);
|
|
102
|
+
return {
|
|
103
|
+
contactId: refs.contact_id ?? user?.contactId ?? undefined,
|
|
104
|
+
organizationId: refs.organization_id ?? user?.organizationId ?? undefined,
|
|
105
|
+
user,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Whether the quote belongs to this buyer. The organisation decides, because
|
|
111
|
+
* the quotes app accepts by organisation unless the merchant says otherwise:
|
|
112
|
+
* a colleague's request is theirs to order too.
|
|
113
|
+
*/
|
|
114
|
+
export function ownsQuote(quote: LiveQuote, actor: { contactId?: string; organizationId?: string }): boolean {
|
|
115
|
+
if (quote.organization_id && actor.organizationId) {
|
|
116
|
+
return quote.organization_id === actor.organizationId;
|
|
117
|
+
}
|
|
118
|
+
if (quote.contact_id && actor.contactId) {
|
|
119
|
+
return quote.contact_id === actor.contactId;
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isExpired(quote: LiveQuote): boolean {
|
|
125
|
+
if (quote.status === "expired") {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
return Boolean(quote.valid_until) && new Date(String(quote.valid_until)).getTime() < Date.now();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The quotes app's record, in the account's words. */
|
|
132
|
+
export function mapQuote(quote: LiveQuote): AccountQuote {
|
|
133
|
+
const lines: AccountQuoteLine[] = (quote.items ?? []).map(item => ({
|
|
134
|
+
id: item.id,
|
|
135
|
+
productId: item.product_id ?? null,
|
|
136
|
+
sku: item.sku ?? "",
|
|
137
|
+
name: item.name,
|
|
138
|
+
quantity: Number(item.quantity),
|
|
139
|
+
unit: item.unit ?? null,
|
|
140
|
+
listPrice: num(item.list_price),
|
|
141
|
+
quotedPrice: num(item.quoted_price),
|
|
142
|
+
unitPrice: num(item.quoted_price) ?? num(item.list_price),
|
|
143
|
+
lineTotal: num(item.line_total) ?? 0,
|
|
144
|
+
taxRate: num(item.tax_rate),
|
|
145
|
+
decision: item.decision ?? "open",
|
|
146
|
+
orderId: item.order_id ?? null,
|
|
147
|
+
}));
|
|
148
|
+
const expired = isExpired(quote);
|
|
149
|
+
// A list row carries no positions; there "open" is what the status says.
|
|
150
|
+
const open = quote.items ? lines.some(line => line.decision === "open") : quote.status !== "accepted";
|
|
151
|
+
const priced = quote.status === "quoted" || quote.status === "partially_accepted";
|
|
152
|
+
const goodsTotal = num(quote.quoted_total) ?? 0;
|
|
153
|
+
const shippingAmount = num(quote.shipping_amount);
|
|
154
|
+
return {
|
|
155
|
+
id: quote.id,
|
|
156
|
+
number: quote.number,
|
|
157
|
+
status: expired && priced ? "expired" : quote.status,
|
|
158
|
+
createdAt: quote.requested_at ?? quote.created_at,
|
|
159
|
+
quotedAt: quote.quoted_at ?? null,
|
|
160
|
+
validUntil: quote.valid_until ?? null,
|
|
161
|
+
currency: quote.currency,
|
|
162
|
+
listTotal: num(quote.list_total) ?? 0,
|
|
163
|
+
goodsTotal,
|
|
164
|
+
shippingAmount,
|
|
165
|
+
shippingTaxRate: num(quote.shipping_tax_rate),
|
|
166
|
+
grandTotal: num(quote.grand_total) ?? goodsTotal + (shippingAmount ?? 0),
|
|
167
|
+
reason: quote.reason ?? null,
|
|
168
|
+
buyerNote: quote.buyer_note ?? null,
|
|
169
|
+
sellerNote: quote.seller_note ?? null,
|
|
170
|
+
shippingAddress: (quote.shipping_address ?? null) as AccountQuote["shippingAddress"],
|
|
171
|
+
billingAddress: (quote.billing_address ?? null) as AccountQuote["billingAddress"],
|
|
172
|
+
lines,
|
|
173
|
+
trail: (quote.events ?? [])
|
|
174
|
+
.filter(entry => (entry.visibility ?? "customer") === "customer")
|
|
175
|
+
.map(entry => ({ kind: entry.kind, actor: entry.actor ?? null, body: entry.body ?? null, at: entry.created_at })),
|
|
176
|
+
orderIds: [...new Set(lines.map(line => line.orderId).filter((id): id is string => Boolean(id)))],
|
|
177
|
+
canOrder: priced && open && !expired,
|
|
178
|
+
// The quotes app takes a "no" to an offer, not to a request still on the
|
|
179
|
+
// merchant's desk — there is nothing to decline until there is a price.
|
|
180
|
+
canDecline: priced && open && !expired,
|
|
181
|
+
expired,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/* ---- live -------------------------------------------------------------- */
|
|
186
|
+
|
|
187
|
+
export async function liveQuoteList(organizationId: string | undefined, contactId: string | undefined): Promise<LiveQuote[]> {
|
|
188
|
+
const query: Record<string, string | number> = { limit: 100, order: "created_at.desc" };
|
|
189
|
+
if (organizationId) {
|
|
190
|
+
query.organization_id = organizationId;
|
|
191
|
+
}
|
|
192
|
+
else if (contactId) {
|
|
193
|
+
query.contact_id = contactId;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
return [];
|
|
197
|
+
}
|
|
198
|
+
const { items } = await useRevenexxSdk().call<{ items: LiveQuote[] }>("GET", "/v1/quotes/quotes", { query });
|
|
199
|
+
return items ?? [];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export async function liveQuoteDetail(id: string): Promise<LiveQuote> {
|
|
203
|
+
return await useRevenexxSdk().call<LiveQuote>("GET", `/v1/quotes/quotes/${encodeURIComponent(id)}/detail`, {
|
|
204
|
+
query: { audience: "customer" },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export async function liveQuoteRequest(body: Record<string, unknown>): Promise<LiveQuote> {
|
|
209
|
+
return await useRevenexxSdk().call<LiveQuote>("POST", "/v1/quotes/request", { body });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export async function liveQuoteAccept(id: string): Promise<LiveQuote & { order_draft: LiveOrderDraft }> {
|
|
213
|
+
return await useRevenexxSdk().call<LiveQuote & { order_draft: LiveOrderDraft }>(
|
|
214
|
+
"POST", `/v1/quotes/quotes/${encodeURIComponent(id)}/accept`, { body: {} },
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export async function liveQuoteOrdered(id: string, orderId: string): Promise<void> {
|
|
219
|
+
await useRevenexxSdk().call("POST", `/v1/quotes/quotes/${encodeURIComponent(id)}/ordered`, {
|
|
220
|
+
body: { order_id: orderId },
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export async function liveQuoteDecline(id: string, reason?: string): Promise<void> {
|
|
225
|
+
await useRevenexxSdk().call("POST", `/v1/quotes/quotes/${encodeURIComponent(id)}/decline`, {
|
|
226
|
+
body: reason ? { reason } : {},
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The quote this buyer asked about, or a 404 — another organisation's quote
|
|
232
|
+
* does not exist for them, and saying "not yours" would confirm it does.
|
|
233
|
+
*/
|
|
234
|
+
export async function loadOwnQuote(
|
|
235
|
+
event: H3Event,
|
|
236
|
+
id: string,
|
|
237
|
+
actor: { contactId?: string; organizationId?: string },
|
|
238
|
+
): Promise<LiveQuote> {
|
|
239
|
+
let quote: LiveQuote | undefined;
|
|
240
|
+
if (resolveQuotesServiceKey(event) === "api") {
|
|
241
|
+
try {
|
|
242
|
+
quote = await liveQuoteDetail(id);
|
|
243
|
+
}
|
|
244
|
+
catch (err) {
|
|
245
|
+
if (err instanceof RevenexxException && err.code === 404) {
|
|
246
|
+
throw createError({ statusCode: 404, message: "Quote not found" });
|
|
247
|
+
}
|
|
248
|
+
getLogService().error("Live quote read failed", apiErrorContext(err));
|
|
249
|
+
throw createError({ statusCode: 502, message: "Quote service unavailable" });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
quote = (await readMockQuotes()).find(q => q.id === id);
|
|
254
|
+
if (quote && !quote.organization_id && !quote.contact_id) {
|
|
255
|
+
return quote;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (!quote || !ownsQuote(quote, actor)) {
|
|
259
|
+
throw createError({ statusCode: 404, message: "Quote not found" });
|
|
260
|
+
}
|
|
261
|
+
return quote;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/* ---- mock -------------------------------------------------------------- */
|
|
265
|
+
|
|
266
|
+
const MOCK_KEY = "account/quotes.json";
|
|
267
|
+
|
|
268
|
+
export async function readMockQuotes(): Promise<LiveQuote[]> {
|
|
269
|
+
return (await readCoverMutableJson<{ quotes: LiveQuote[] }>(MOCK_KEY)).quotes;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export async function writeMockQuotes(quotes: LiveQuote[]): Promise<void> {
|
|
273
|
+
await writeCoverMutableJson(MOCK_KEY, { quotes });
|
|
274
|
+
}
|