@revenexx/cover 0.2.39 → 0.2.41
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/address/AccountAddressCard.vue +11 -3
- 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/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 +20 -0
- package/i18n/locales/en/account.json +95 -0
- package/i18n/locales/en/cart.json +20 -0
- package/package.json +1 -1
- package/server/api/account/profile.get.ts +37 -2
- 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
|
@@ -215,6 +215,26 @@
|
|
|
215
215
|
"delivery": "Ordered by 4 pm – delivered the next business day",
|
|
216
216
|
"invoice": "Buy on invoice for business customers",
|
|
217
217
|
"support": "Personal service team, weekdays 8 am–5 pm"
|
|
218
|
+
},
|
|
219
|
+
"quote": {
|
|
220
|
+
"action": "Request a quote",
|
|
221
|
+
"title": "Request a quote",
|
|
222
|
+
"description": "We price the carriage for your consignment and send you a quote.",
|
|
223
|
+
"shippingAddress": "Shipping address",
|
|
224
|
+
"shippingAddressHint": "The carriage is priced for this address",
|
|
225
|
+
"billingAddress": "Billing address",
|
|
226
|
+
"note": "Message to us (optional)",
|
|
227
|
+
"notePlaceholder": "e.g. preferred delivery date, tail-lift delivery …",
|
|
228
|
+
"steps": {
|
|
229
|
+
"price": "We price the carriage and make you a quote.",
|
|
230
|
+
"notify": "You receive an email as soon as the quote is ready.",
|
|
231
|
+
"order": "You order it with one click under “Quotes” in your account."
|
|
232
|
+
},
|
|
233
|
+
"cancel": "Cancel",
|
|
234
|
+
"submit": "Request quote",
|
|
235
|
+
"success": "Request {number} received",
|
|
236
|
+
"successDescription": "Your cart was moved into the request.",
|
|
237
|
+
"failed": "Request failed"
|
|
218
238
|
}
|
|
219
239
|
},
|
|
220
240
|
"calc": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenexx/cover",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.41",
|
|
4
4
|
"description": "Cover \u2014 revenexx design system for Nuxt. Distributed as a Nuxt layer: generic UI components, theming tokens and stores shared by the demo shop, custom storefronts and the Blokkli theme.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./nuxt.config.ts",
|
|
@@ -18,6 +18,24 @@ interface AccountProfileRaw {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/** The contact's own phone, or "" — a missing number is not an error. */
|
|
22
|
+
async function contactPhone(contactId: string | undefined): Promise<string> {
|
|
23
|
+
if (!contactId) {
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const contact = await useRevenexxSdk().call<{ phone?: string | null }>(
|
|
28
|
+
"GET",
|
|
29
|
+
`/v1/customers/contacts/${encodeURIComponent(contactId)}`,
|
|
30
|
+
);
|
|
31
|
+
return contact.phone ?? "";
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
getLogService().error("Profile: contact phone lookup failed", apiErrorContext(err));
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
21
39
|
export default defineEventHandler(async (event): Promise<AccountProfileResponse> => {
|
|
22
40
|
// Session guard — the profile endpoint always requires a login, regardless
|
|
23
41
|
// of which identity implementation resolves the user below.
|
|
@@ -42,8 +60,25 @@ export default defineEventHandler(async (event): Promise<AccountProfileResponse>
|
|
|
42
60
|
// resolves the real customer through the public API.
|
|
43
61
|
const user = await getAccountService(event).getUser(event);
|
|
44
62
|
|
|
45
|
-
//
|
|
46
|
-
//
|
|
63
|
+
// Live: the buyer's own phone from their contact, and the company from
|
|
64
|
+
// their organisation. Both used to come from the demo store whatever the
|
|
65
|
+
// mode, so every live buyer saw the demo profile's company and number.
|
|
66
|
+
const live = resolveServiceKey(event, { domain: "accountService", mockKey: "mock", liveKey: "api" }) === "api";
|
|
67
|
+
if (live) {
|
|
68
|
+
const [phone, organization] = await Promise.all([
|
|
69
|
+
contactPhone(session.contactId),
|
|
70
|
+
getOrganizationService(event).get(event).catch(() => null),
|
|
71
|
+
]);
|
|
72
|
+
return {
|
|
73
|
+
name: user.name,
|
|
74
|
+
email: user.email,
|
|
75
|
+
phone,
|
|
76
|
+
company: organization?.name ?? "",
|
|
77
|
+
initials: user.initials,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Mock: the phone and company of the demo profile.
|
|
47
82
|
const profileData = await readCoverMutableJson<AccountProfileRaw>("account/profile.json");
|
|
48
83
|
|
|
49
84
|
return {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** The buyer says no to a quote — every position still open is declined with it. */
|
|
2
|
+
export default defineEventHandler(async (event): Promise<{ ok: true }> => {
|
|
3
|
+
const id = getRouterParam(event, "id") ?? "";
|
|
4
|
+
const body = await readBody<{ reason?: string }>(event).catch(() => ({} as { reason?: string }));
|
|
5
|
+
const actor = await quoteActor(event);
|
|
6
|
+
if (!actor.user) {
|
|
7
|
+
throw createError({ statusCode: 401, message: "Unauthorized" });
|
|
8
|
+
}
|
|
9
|
+
const quote = await loadOwnQuote(event, id, actor);
|
|
10
|
+
if (!mapQuote(quote).canDecline) {
|
|
11
|
+
throw createError({ statusCode: 422, message: "This quote can no longer be declined" });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (resolveQuotesServiceKey(event) === "api") {
|
|
15
|
+
try {
|
|
16
|
+
await liveQuoteDecline(id, body?.reason?.trim() || undefined);
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
if (err instanceof RevenexxException && (err.code === 409 || err.code === 422)) {
|
|
20
|
+
throw createError({ statusCode: 422, message: err.message });
|
|
21
|
+
}
|
|
22
|
+
getLogService().error("Quote decline failed", apiErrorContext(err));
|
|
23
|
+
throw createError({ statusCode: 502, message: "Quote service unavailable" });
|
|
24
|
+
}
|
|
25
|
+
return { ok: true };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const quotes = await readMockQuotes();
|
|
29
|
+
const now = new Date().toISOString();
|
|
30
|
+
await writeMockQuotes(quotes.map(q => q.id !== id
|
|
31
|
+
? q
|
|
32
|
+
: {
|
|
33
|
+
...q,
|
|
34
|
+
status: "declined",
|
|
35
|
+
items: (q.items ?? []).map(i => (i.decision === "open" ? { ...i, decision: "declined" } : i)),
|
|
36
|
+
events: [...(q.events ?? []), { kind: "declined", actor: "buyer", body: body?.reason ?? null, visibility: "customer", created_at: now }],
|
|
37
|
+
}));
|
|
38
|
+
return { ok: true };
|
|
39
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { OrderPaymentStatus } from "@revenexx/sdk";
|
|
2
|
+
import type { Models } from "@revenexx/sdk";
|
|
3
|
+
|
|
4
|
+
import type { QuoteOrderPayload, QuoteOrderResult } from "../../../../../app/interfaces/account/quotes";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Order a quote at the quoted figures (RX-783).
|
|
8
|
+
*
|
|
9
|
+
* The quotes app answers acceptance with a draft; it never places the order
|
|
10
|
+
* itself, so this route does what the checkout does for a cart — with the
|
|
11
|
+
* difference that nothing is re-priced. The negotiated price arrives as each
|
|
12
|
+
* line's unit price and the quoted carriage as the shipping line, to the
|
|
13
|
+
* addresses the carriage was priced for. The order records which quote it came
|
|
14
|
+
* out of, and the quote records which order took it.
|
|
15
|
+
*
|
|
16
|
+
* No promotions are evaluated: a quote is already the negotiated price, and
|
|
17
|
+
* stacking a campaign on top of it is a decision the merchant makes in the
|
|
18
|
+
* price, not one the storefront makes afterwards.
|
|
19
|
+
*/
|
|
20
|
+
function toIsoCountry(value: unknown): string {
|
|
21
|
+
const raw = String(value ?? "").trim();
|
|
22
|
+
if (/^[A-Za-z]{2}$/.test(raw)) {
|
|
23
|
+
return raw.toUpperCase();
|
|
24
|
+
}
|
|
25
|
+
const names: Record<string, string> = { deutschland: "DE", germany: "DE", österreich: "AT", austria: "AT", schweiz: "CH", switzerland: "CH" };
|
|
26
|
+
return names[raw.toLowerCase()] ?? raw;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default defineEventHandler(async (event): Promise<QuoteOrderResult> => {
|
|
30
|
+
const id = getRouterParam(event, "id") ?? "";
|
|
31
|
+
const body = await readBody<QuoteOrderPayload>(event).catch(() => ({} as QuoteOrderPayload));
|
|
32
|
+
const actor = await quoteActor(event);
|
|
33
|
+
if (!actor.user) {
|
|
34
|
+
throw createError({ statusCode: 401, message: "Unauthorized" });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const quote = await loadOwnQuote(event, id, actor);
|
|
38
|
+
const view = mapQuote(quote);
|
|
39
|
+
if (!view.canOrder) {
|
|
40
|
+
throw createError({
|
|
41
|
+
statusCode: 422,
|
|
42
|
+
message: view.expired ? "This quote has expired" : "This quote cannot be ordered",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const config = useAppConfig() as { quotes?: { paymentMethod?: string } };
|
|
47
|
+
const paymentMethod = config.quotes?.paymentMethod ?? "invoice";
|
|
48
|
+
|
|
49
|
+
/* ---- demo ------------------------------------------------------------ */
|
|
50
|
+
if (resolveQuotesServiceKey(event) !== "api") {
|
|
51
|
+
const quotes = await readMockQuotes();
|
|
52
|
+
const now = new Date().toISOString();
|
|
53
|
+
const orderNumber = `ORD-${Date.now()}`;
|
|
54
|
+
await writeMockQuotes(quotes.map(q => q.id !== id
|
|
55
|
+
? q
|
|
56
|
+
: {
|
|
57
|
+
...q,
|
|
58
|
+
status: "accepted",
|
|
59
|
+
items: (q.items ?? []).map(i => (i.decision === "open" ? { ...i, decision: "accepted", order_id: orderNumber } : i)),
|
|
60
|
+
events: [...(q.events ?? []), { kind: "ordered", actor: "buyer", body: null, visibility: "customer", created_at: now }],
|
|
61
|
+
}));
|
|
62
|
+
return { orderId: orderNumber, orderNumber };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ---- live ------------------------------------------------------------ */
|
|
66
|
+
const sdk = useRevenexxSdk();
|
|
67
|
+
let draft;
|
|
68
|
+
try {
|
|
69
|
+
draft = (await liveQuoteAccept(id)).order_draft;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
if (err instanceof RevenexxException && [403, 409, 422].includes(err.code)) {
|
|
73
|
+
throw createError({ statusCode: 422, message: err.message });
|
|
74
|
+
}
|
|
75
|
+
getLogService().error("Quote acceptance failed", apiErrorContext(err));
|
|
76
|
+
throw createError({ statusCode: 502, message: "Quote service unavailable" });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let placed: { id: string; number: string; grand_total?: number };
|
|
80
|
+
try {
|
|
81
|
+
placed = await sdk.orders.ordersPlace({
|
|
82
|
+
contactId: draft.contact_id ?? actor.contactId,
|
|
83
|
+
organizationId: draft.organization_id ?? actor.organizationId,
|
|
84
|
+
currency: draft.currency,
|
|
85
|
+
...(body?.customerOrderNumber?.trim() ? { customerOrderNumber: body.customerOrderNumber.trim() } : {}),
|
|
86
|
+
buyer: (draft.buyer ?? { name: actor.user.name, email: actor.user.email }) as object,
|
|
87
|
+
billingAddress: (draft.billing_address ?? null) as object,
|
|
88
|
+
shippingAddress: (draft.shipping_address ?? null) as object,
|
|
89
|
+
payment: { method: paymentMethod },
|
|
90
|
+
// The carriage the merchant quoted — or none, when they quoted the
|
|
91
|
+
// goods alone. Taxed by order management at the quoted rate.
|
|
92
|
+
shipping: draft.shipping ?? { method: "quote", price: 0, tax_rate: 0 },
|
|
93
|
+
items: draft.items.map(line => ({
|
|
94
|
+
...(line.product_id ? { product_id: line.product_id } : {}),
|
|
95
|
+
...(line.sku ? { sku: line.sku } : {}),
|
|
96
|
+
name: line.name,
|
|
97
|
+
quantity: line.quantity,
|
|
98
|
+
unit_price: line.unit_price,
|
|
99
|
+
tax_rate: line.tax_rate ?? 0,
|
|
100
|
+
})) as unknown as Models.OrderItemCreateRequest[],
|
|
101
|
+
metadata: { quote_id: quote.id, quote_number: quote.number },
|
|
102
|
+
userData: {
|
|
103
|
+
quote_number: quote.number,
|
|
104
|
+
...(body?.orderNote?.trim() ? { order_note: body.orderNote.trim() } : {}),
|
|
105
|
+
},
|
|
106
|
+
}) as unknown as { id: string; number: string; grand_total?: number };
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
// The quote is accepted and no order exists. Say so in the log with
|
|
110
|
+
// the quote number: the merchant sees an accepted quote without an
|
|
111
|
+
// order and can place it by hand, which beats a buyer retrying into a
|
|
112
|
+
// second acceptance of a quote that has nothing left open.
|
|
113
|
+
getLogService().error(`Order placement for quote ${quote.number} failed after acceptance`, apiErrorContext(err));
|
|
114
|
+
if (err instanceof RevenexxException && (err.code === 400 || err.code === 422)) {
|
|
115
|
+
throw createError({ statusCode: 422, message: err.message });
|
|
116
|
+
}
|
|
117
|
+
throw createError({ statusCode: 502, message: "Order service unavailable" });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (resolvePaymentServiceKey(event) === "api") {
|
|
121
|
+
try {
|
|
122
|
+
const payment = await sdk.call<{ id: string; status: string }>("POST", "/v1/payments", {
|
|
123
|
+
body: {
|
|
124
|
+
method_code: paymentMethod,
|
|
125
|
+
amount: Math.max(0, Math.round((placed.grand_total ?? view.grandTotal) * 100) / 100),
|
|
126
|
+
currency: draft.currency,
|
|
127
|
+
country: toIsoCountry(draft.billing_address?.country ?? draft.shipping_address?.country),
|
|
128
|
+
order_ref: placed.number,
|
|
129
|
+
idempotency_key: `quote-${quote.id}-${placed.id}`,
|
|
130
|
+
metadata: { quote_number: quote.number },
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
const mapped = ["captured", "succeeded", "paid"].includes(payment.status)
|
|
134
|
+
? OrderPaymentStatus.Paid
|
|
135
|
+
: payment.status === "authorized" ? OrderPaymentStatus.Authorized : OrderPaymentStatus.Pending;
|
|
136
|
+
await sdk.orders.ordersPaymentStatusUpdate({ id: placed.id, status: mapped });
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
// An invoice order stands without a captured payment; the payment
|
|
140
|
+
// record is bookkeeping here, not the gate it is at checkout.
|
|
141
|
+
getLogService().error(`Payment for quote order ${placed.number} failed`, apiErrorContext(err));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
await liveQuoteOrdered(id, placed.id);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
getLogService().error(`Writing order ${placed.number} back to quote ${quote.number} failed`, apiErrorContext(err));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return { orderId: placed.id, orderNumber: placed.number };
|
|
153
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AccountQuote } from "../../../../app/interfaces/account/quotes";
|
|
2
|
+
|
|
3
|
+
/** One quote as its buyer reads it: positions, carriage, addresses, the customer's trail. */
|
|
4
|
+
export default defineEventHandler(async (event): Promise<AccountQuote> => {
|
|
5
|
+
const id = getRouterParam(event, "id") ?? "";
|
|
6
|
+
const actor = await quoteActor(event);
|
|
7
|
+
if (!actor.user) {
|
|
8
|
+
throw createError({ statusCode: 401, message: "Unauthorized" });
|
|
9
|
+
}
|
|
10
|
+
return mapQuote(await loadOwnQuote(event, id, actor));
|
|
11
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AccountQuoteListResponse } from "../../../app/interfaces/account/quotes";
|
|
2
|
+
|
|
3
|
+
/** The buyer's quotes, newest first — their organisation's, as the quotes app accepts by organisation. */
|
|
4
|
+
export default defineEventHandler(async (event): Promise<AccountQuoteListResponse> => {
|
|
5
|
+
const actor = await quoteActor(event);
|
|
6
|
+
if (!actor.user) {
|
|
7
|
+
throw createError({ statusCode: 401, message: "Unauthorized" });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (resolveQuotesServiceKey(event) === "api") {
|
|
11
|
+
try {
|
|
12
|
+
const rows = await liveQuoteList(actor.organizationId, actor.contactId);
|
|
13
|
+
// Belt and braces: the app filtered by organisation, and nothing
|
|
14
|
+
// that is not the buyer's may reach the page even if it did not.
|
|
15
|
+
const quotes = rows.filter(row => ownsQuote(row, actor)).map(mapQuote);
|
|
16
|
+
return { quotes, total: quotes.length };
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
getLogService().error("Live quote list failed", apiErrorContext(err));
|
|
20
|
+
throw createError({ statusCode: 502, message: "Quote service unavailable" });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const quotes = (await readMockQuotes()).filter(row => ownsQuote(row, actor) || (!row.organization_id && !row.contact_id)).map(mapQuote);
|
|
25
|
+
return { quotes, total: quotes.length };
|
|
26
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { Models } from "@revenexx/sdk";
|
|
2
|
+
|
|
3
|
+
import type { QuoteRequestPayload, QuoteRequestResult } from "../../../app/interfaces/account/quotes";
|
|
4
|
+
import type { LiveQuote } from "../../utils/quotesService";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Turn the active cart into a quotation request (RX-783).
|
|
8
|
+
*
|
|
9
|
+
* For a cart the shop cannot order as it is — a consignment over the parcel
|
|
10
|
+
* limit is the case that brought this in. The buyer picks the delivery and the
|
|
11
|
+
* billing address from their own address book (selection only; the ERP owns
|
|
12
|
+
* the addresses), because the carriage is priced for where the goods go, and
|
|
13
|
+
* the order the quote becomes must go to the same address with the same ids
|
|
14
|
+
* the ERP export reads.
|
|
15
|
+
*
|
|
16
|
+
* The positions are copied onto the quote at the prices the buyer was shown.
|
|
17
|
+
* The cart is then handed over to the quote the way an order takes it — frozen
|
|
18
|
+
* under the quote number — so the buyer starts clean and cannot order the same
|
|
19
|
+
* goods a second time while the merchant is still pricing them.
|
|
20
|
+
*/
|
|
21
|
+
export default defineEventHandler(async (event): Promise<QuoteRequestResult> => {
|
|
22
|
+
const body = await readBody<QuoteRequestPayload>(event);
|
|
23
|
+
if (!body?.items?.length) {
|
|
24
|
+
throw createError({ statusCode: 400, message: "Cart is empty" });
|
|
25
|
+
}
|
|
26
|
+
if (!body.shippingAddressId || !body.billingAddressId) {
|
|
27
|
+
throw createError({ statusCode: 422, message: "A delivery and a billing address are required" });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const actor = await quoteActor(event);
|
|
31
|
+
if (!actor.user) {
|
|
32
|
+
throw createError({ statusCode: 401, message: "Sign in to ask for a quote" });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const profile = await getCheckoutProfileService().getProfile(event, actor.user.$id);
|
|
36
|
+
const shippingAddress = profile.addresses.find(a => a.id === body.shippingAddressId);
|
|
37
|
+
const billingAddress = profile.addresses.find(a => a.id === body.billingAddressId);
|
|
38
|
+
if (!shippingAddress || !billingAddress) {
|
|
39
|
+
throw createError({ statusCode: 422, message: "The chosen address is not in your address book" });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const currency = resolveSelectedCurrency(event) ?? "EUR";
|
|
43
|
+
const live = resolveQuotesServiceKey(event) === "api";
|
|
44
|
+
|
|
45
|
+
// The list price per line, resolved the same way the order would be — a
|
|
46
|
+
// quote asked at a price the buyer never saw is no basis for a decision.
|
|
47
|
+
const listPrice = new Map<string, number>();
|
|
48
|
+
const taxRate = new Map<string, number>();
|
|
49
|
+
if (live) {
|
|
50
|
+
try {
|
|
51
|
+
const { prices } = await useRevenexxSdk().prices.pricesResolve({
|
|
52
|
+
items: body.items.map(it => ({ product_id: String(it.id), sku: it.sku || undefined, quantity: Number(it.quantity) || 1 })) as Models.PriceResolveItem[],
|
|
53
|
+
...(actor.contactId ? { contactId: actor.contactId } : {}),
|
|
54
|
+
...(actor.organizationId ? { organizationId: actor.organizationId } : {}),
|
|
55
|
+
currency,
|
|
56
|
+
}) as unknown as { prices: Array<{ product_id: string | null; sku: string | null; tax_rate: number | null; on_request: boolean; unit_price: number | null }> };
|
|
57
|
+
for (const p of prices) {
|
|
58
|
+
const keys = [p.product_id, p.sku].filter((k): k is string => Boolean(k));
|
|
59
|
+
for (const key of keys) {
|
|
60
|
+
if (!p.on_request && p.unit_price != null) listPrice.set(key, p.unit_price);
|
|
61
|
+
if (p.tax_rate != null) taxRate.set(key, p.tax_rate);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
getLogService().error("Quote request price resolution failed", apiErrorContext(err));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const items = body.items.map((it, index) => {
|
|
71
|
+
const key = String(it.id);
|
|
72
|
+
const price = listPrice.get(key) ?? listPrice.get(String(it.sku ?? "")) ?? (Number.isFinite(Number(it.price)) ? Number(it.price) : null);
|
|
73
|
+
const rate = taxRate.get(key) ?? taxRate.get(String(it.sku ?? ""));
|
|
74
|
+
return {
|
|
75
|
+
position: (index + 1) * 10,
|
|
76
|
+
product_id: key,
|
|
77
|
+
...(it.sku ? { sku: String(it.sku) } : {}),
|
|
78
|
+
name: String(it.name ?? it.sku ?? it.id),
|
|
79
|
+
quantity: Number(it.quantity),
|
|
80
|
+
...(price !== null ? { list_price: price } : {}),
|
|
81
|
+
...(rate !== undefined ? { tax_rate: rate } : {}),
|
|
82
|
+
...(it.image ? { metadata: { image: String(it.image) } } : {}),
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const buyer = {
|
|
87
|
+
name: actor.user.name,
|
|
88
|
+
email: actor.user.email,
|
|
89
|
+
company: shippingAddress.companyName || billingAddress.companyName || undefined,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
let cartId: string | null = null;
|
|
93
|
+
const manager = getCartManager(event);
|
|
94
|
+
try {
|
|
95
|
+
cartId = (await manager?.listCarts(event))?.activeCartId ?? null;
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
getLogService().error("Active cart lookup failed", apiErrorContext(err));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let created: { id: string; number: string };
|
|
102
|
+
if (live) {
|
|
103
|
+
try {
|
|
104
|
+
const quote = await liveQuoteRequest({
|
|
105
|
+
currency,
|
|
106
|
+
items,
|
|
107
|
+
...(cartId ? { cart_id: cartId } : {}),
|
|
108
|
+
...(actor.contactId ? { contact_id: actor.contactId } : {}),
|
|
109
|
+
...(actor.organizationId ? { organization_id: actor.organizationId } : {}),
|
|
110
|
+
buyer,
|
|
111
|
+
shipping_address: shippingAddress,
|
|
112
|
+
billing_address: billingAddress,
|
|
113
|
+
...(body.note?.trim() ? { buyer_note: body.note.trim() } : {}),
|
|
114
|
+
...(body.reason ? { reason: body.reason } : {}),
|
|
115
|
+
});
|
|
116
|
+
created = { id: quote.id, number: quote.number };
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
if (err instanceof RevenexxException && (err.code === 400 || err.code === 422)) {
|
|
120
|
+
throw createError({ statusCode: 422, message: err.message });
|
|
121
|
+
}
|
|
122
|
+
getLogService().error("Quote request failed", apiErrorContext(err));
|
|
123
|
+
throw createError({ statusCode: 502, message: "Quote service unavailable" });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const quotes = await readMockQuotes();
|
|
128
|
+
const number = `ANG-${String(quotes.length + 1).padStart(6, "0")}`;
|
|
129
|
+
const now = new Date().toISOString();
|
|
130
|
+
const record: LiveQuote = {
|
|
131
|
+
id: crypto.randomUUID(),
|
|
132
|
+
number,
|
|
133
|
+
status: "requested",
|
|
134
|
+
currency,
|
|
135
|
+
contact_id: actor.contactId ?? null,
|
|
136
|
+
organization_id: actor.organizationId ?? null,
|
|
137
|
+
reason: body.reason ?? null,
|
|
138
|
+
buyer_note: body.note?.trim() || null,
|
|
139
|
+
list_total: items.reduce((sum, it) => sum + (it.list_price ?? 0) * it.quantity, 0),
|
|
140
|
+
quoted_total: items.reduce((sum, it) => sum + (it.list_price ?? 0) * it.quantity, 0),
|
|
141
|
+
grand_total: items.reduce((sum, it) => sum + (it.list_price ?? 0) * it.quantity, 0),
|
|
142
|
+
requested_at: now,
|
|
143
|
+
created_at: now,
|
|
144
|
+
buyer,
|
|
145
|
+
shipping_address: shippingAddress as unknown as Record<string, unknown>,
|
|
146
|
+
billing_address: billingAddress as unknown as Record<string, unknown>,
|
|
147
|
+
items: items.map(it => ({
|
|
148
|
+
id: crypto.randomUUID(),
|
|
149
|
+
product_id: it.product_id,
|
|
150
|
+
sku: it.sku ?? null,
|
|
151
|
+
name: it.name,
|
|
152
|
+
quantity: it.quantity,
|
|
153
|
+
list_price: it.list_price ?? null,
|
|
154
|
+
tax_rate: it.tax_rate ?? null,
|
|
155
|
+
line_total: (it.list_price ?? 0) * it.quantity,
|
|
156
|
+
decision: "open",
|
|
157
|
+
})),
|
|
158
|
+
events: [{ kind: "requested", actor: "buyer", body: body.note?.trim() || null, visibility: "customer", created_at: now }],
|
|
159
|
+
};
|
|
160
|
+
await writeMockQuotes([record, ...quotes]);
|
|
161
|
+
created = { id: record.id, number };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The cart goes to the quote. Best-effort, like the order hand-over: a
|
|
165
|
+
// cart that stays active is an inconvenience, a lost request is not.
|
|
166
|
+
if (manager) {
|
|
167
|
+
try {
|
|
168
|
+
await manager.orderActiveCart(event, created.number);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
getLogService().error("Cart hand-over to quote failed", toErrorContext(err));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return created;
|
|
176
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"quotes": [
|
|
3
|
+
{
|
|
4
|
+
"id": "5b8f9a1e-0000-4000-8000-000000000001",
|
|
5
|
+
"number": "ANG-000001",
|
|
6
|
+
"status": "quoted",
|
|
7
|
+
"currency": "EUR",
|
|
8
|
+
"contact_id": null,
|
|
9
|
+
"organization_id": null,
|
|
10
|
+
"reason": "shipping-on-request",
|
|
11
|
+
"buyer_note": "Bitte Anlieferung mit Hebebühne.",
|
|
12
|
+
"seller_note": "Speditionsversand auf Einwegpalette, Lieferung in 5 Werktagen nach Bestellung.",
|
|
13
|
+
"list_total": 2176,
|
|
14
|
+
"quoted_total": 2176,
|
|
15
|
+
"shipping_amount": 185,
|
|
16
|
+
"shipping_tax_rate": 19,
|
|
17
|
+
"grand_total": 2361,
|
|
18
|
+
"valid_until": "2099-12-31T00:00:00.000Z",
|
|
19
|
+
"requested_at": "2026-09-22T08:00:00.000Z",
|
|
20
|
+
"quoted_at": "2026-09-22T09:30:00.000Z",
|
|
21
|
+
"created_at": "2026-09-22T08:00:00.000Z",
|
|
22
|
+
"buyer": { "name": "Max Mustermann", "email": "max@example.com", "company": "Musterfirma GmbH" },
|
|
23
|
+
"shipping_address": { "id": "addr-1", "companyName": "Musterfirma GmbH", "contactName": "Max Mustermann", "street": "Industriestraße 12", "postalCode": "68753", "city": "Waghäusel", "country": "DE" },
|
|
24
|
+
"billing_address": { "id": "addr-1", "companyName": "Musterfirma GmbH", "contactName": "Max Mustermann", "street": "Industriestraße 12", "postalCode": "68753", "city": "Waghäusel", "country": "DE" },
|
|
25
|
+
"items": [
|
|
26
|
+
{ "id": "5b8f9a1e-0000-4000-8000-0000000000a1", "sku": "D22020005", "name": "Umkehrkammer 4102", "quantity": 2, "list_price": 1088, "quoted_price": null, "tax_rate": 19, "line_total": 2176, "decision": "open" }
|
|
27
|
+
],
|
|
28
|
+
"events": [
|
|
29
|
+
{ "kind": "requested", "actor": "buyer", "body": "Bitte Anlieferung mit Hebebühne.", "visibility": "customer", "created_at": "2026-09-22T08:00:00.000Z" },
|
|
30
|
+
{ "kind": "quoted", "actor": "seller", "body": "Speditionsversand auf Einwegpalette, Lieferung in 5 Werktagen nach Bestellung.", "visibility": "customer", "created_at": "2026-09-22T09:30:00.000Z" }
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|