@revenexx/cover 0.19.0 → 0.20.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/app/api/marketing.ts +24 -0
- package/app/app.config.ts +22 -0
- package/app/components/account/loyalty/AccountLoyalty.vue +205 -0
- package/app/components/cart/view/CartSummaryPanel.vue +4 -0
- package/app/components/checkout/onepage/CheckoutSummarySection.vue +4 -0
- package/app/components/loyalty/hint/LoyaltyPerkHint.vue +28 -0
- package/app/components/newsletter/confirm/NewsletterConfirmation.vue +54 -0
- package/app/components/newsletter/preferences/NewsletterPreferenceCentre.vue +183 -0
- package/app/components/newsletter/state/NewsletterLinkRefusal.vue +45 -0
- package/app/components/review/form/ReviewForm.vue +211 -0
- package/app/composables/useCampaignAttribution.ts +120 -0
- package/app/composables/useCheckoutOnePage.ts +9 -0
- package/app/composables/useLoyaltyAccount.ts +79 -0
- package/app/composables/useMarketingConsent.ts +49 -0
- package/app/config/navigation.ts +1 -1
- package/app/config/quickActions.ts +1 -0
- package/app/pages/account/points.vue +26 -0
- package/app/pages/newsletter/confirm/[token].vue +23 -0
- package/app/pages/newsletter/preferences/[token].vue +24 -0
- package/app/pages/review/[token].vue +23 -0
- package/app/plugins/campaign-attribution.client.ts +47 -0
- package/app/shared/campaignAttribution.ts +281 -0
- package/app/shared/personalisation.ts +107 -0
- package/i18n/locales/de/account.json +3 -2
- package/i18n/locales/de/marketing.json +71 -0
- package/i18n/locales/en/account.json +3 -2
- package/i18n/locales/en/marketing.json +71 -0
- package/package.json +1 -1
- package/server/api/campaigns/touches.post.ts +42 -0
- package/server/api/cart/attribution.put.ts +31 -0
- package/server/api/loyalty/account.get.ts +27 -0
- package/server/api/newsletter/confirm/[token].post.ts +21 -0
- package/server/api/newsletter/preferences/[token].get.ts +14 -0
- package/server/api/newsletter/preferences/[token].post.ts +36 -0
- package/server/api/orders/index.post.ts +10 -1
- package/server/api/reviews/requests/[token].get.ts +14 -0
- package/server/api/reviews/submit/[token].post.ts +31 -0
- package/server/interfaces/cart.ts +6 -0
- package/server/interfaces/marketing.ts +150 -0
- package/server/services/ApiCartService.ts +16 -0
- package/server/services/ApiMarketingService.ts +292 -0
- package/server/services/MockMarketingService.ts +178 -0
- package/server/utils/marketingService.ts +63 -0
- package/server/utils/orderAttribution.ts +65 -0
- package/server/utils/personalisationViewer.ts +28 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The double opt-in click (RAD-293): the link in the confirmation mail opens
|
|
3
|
+
* the shop's confirmation page, and the page — in the browser, never while it
|
|
4
|
+
* is rendered on the server — confirms here. A mail scanner that fetches the
|
|
5
|
+
* link without running the page confirms nothing.
|
|
6
|
+
*
|
|
7
|
+
* The address and the browser go along as the evidence marketing-consents
|
|
8
|
+
* keeps for the confirmation.
|
|
9
|
+
*/
|
|
10
|
+
export default defineEventHandler(async (event) => {
|
|
11
|
+
const token = routeToken(event);
|
|
12
|
+
const body = await readBody<{ page_url?: unknown }>(event).catch(() => null);
|
|
13
|
+
const pageUrl = typeof body?.page_url === "string" ? body.page_url.slice(0, 2048) : null;
|
|
14
|
+
try {
|
|
15
|
+
const result = await getMarketingService(event).confirmSubscription(event, token, consentEvidence(event, pageUrl));
|
|
16
|
+
return { confirmed: true, alreadyConfirmed: result.alreadyConfirmed };
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
return rethrowTokenRefusal(err);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The preference centre (RAD-293): what a recipient of the shop's mails is
|
|
3
|
+
* subscribed to, behind the personal link in every mail's footer. The address
|
|
4
|
+
* comes back masked; the link is the only proof of who is asking.
|
|
5
|
+
*/
|
|
6
|
+
export default defineEventHandler(async (event) => {
|
|
7
|
+
const token = routeToken(event);
|
|
8
|
+
try {
|
|
9
|
+
return await getMarketingService(event).preferences(event, token, resolveLocale(event));
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
return rethrowTokenRefusal(err);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PreferenceChoice } from "../../../interfaces/marketing";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a recipient changed in the preference centre: one choice per type, or
|
|
5
|
+
* leaving everything at once. A type that needs a double opt-in is requested,
|
|
6
|
+
* not subscribed — marketing-consents sends the confirmation mail.
|
|
7
|
+
*/
|
|
8
|
+
export default defineEventHandler(async (event) => {
|
|
9
|
+
const token = routeToken(event);
|
|
10
|
+
const body = await readBody<{ choices?: unknown; unsubscribe_all?: unknown }>(event).catch(() => null);
|
|
11
|
+
const unsubscribeAll = body?.unsubscribe_all === true;
|
|
12
|
+
const choices: PreferenceChoice[] = Array.isArray(body?.choices)
|
|
13
|
+
? (body.choices as Record<string, unknown>[])
|
|
14
|
+
.filter(choice => choice && typeof choice.subscription_type === "string" && typeof choice.subscribed === "boolean")
|
|
15
|
+
.slice(0, 50)
|
|
16
|
+
.map(choice => ({
|
|
17
|
+
subscription_type: String(choice.subscription_type),
|
|
18
|
+
subscribed: choice.subscribed === true,
|
|
19
|
+
...(typeof choice.consent_text_id === "string" ? { consent_text_id: choice.consent_text_id } : {}),
|
|
20
|
+
}))
|
|
21
|
+
: [];
|
|
22
|
+
if (!unsubscribeAll && !choices.length) {
|
|
23
|
+
throw createError({ statusCode: 422, message: "Nothing to change" });
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return await getMarketingService(event).savePreferences(
|
|
27
|
+
event,
|
|
28
|
+
token,
|
|
29
|
+
{ choices, unsubscribeAll, locale: resolveLocale(event) },
|
|
30
|
+
consentEvidence(event),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
return rethrowTokenRefusal(err);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
@@ -6,6 +6,8 @@ import { type EligibleMethodFee, paymentFee } from "../../utils/paymentFee";
|
|
|
6
6
|
import { orderLinePrices, type PriceResolveAnswer } from "../../utils/orderLinePrices";
|
|
7
7
|
import { toIsoCountry } from "../../utils/country";
|
|
8
8
|
import { checkoutTakesPayment, orderPaymentRequest, recordOrderPaymentStatus } from "../../utils/orderPayment";
|
|
9
|
+
import { placeOrderWithAttribution } from "../../utils/orderAttribution";
|
|
10
|
+
import { sanitizeAttribution } from "../../../app/shared/campaignAttribution";
|
|
9
11
|
|
|
10
12
|
// Method codes are generic — live mode validates them against the
|
|
11
13
|
// payments app; the demo-only methods keep their extra fields.
|
|
@@ -44,6 +46,11 @@ interface OrderPayload {
|
|
|
44
46
|
paymentInstrument?: { token?: string };
|
|
45
47
|
/** Where the PSP returns the buyer after a redirect/3DS step-up. */
|
|
46
48
|
returnUrl?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Where the visit came from (RAD-293), as the browser kept it under the
|
|
51
|
+
* visitor's consent. Rebuilt into the closed shape before it travels.
|
|
52
|
+
*/
|
|
53
|
+
attribution?: unknown;
|
|
47
54
|
}
|
|
48
55
|
|
|
49
56
|
function isNonEmptyString(v: unknown): v is string {
|
|
@@ -445,7 +452,9 @@ export default defineEventHandler(async (event) => {
|
|
|
445
452
|
? await submitThroughProcurement(submission as never)
|
|
446
453
|
: {
|
|
447
454
|
kind: "order" as const,
|
|
448
|
-
|
|
455
|
+
// The cart's attribution goes with the order: orders never
|
|
456
|
+
// reads the cart for it (RAD-287).
|
|
457
|
+
order: await placeOrderWithAttribution<SubmittedOrder>(submission, sanitizeAttribution(body.attribution)),
|
|
449
458
|
};
|
|
450
459
|
|
|
451
460
|
if (outcome.kind === "request") {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The review form behind a review request link (RAD-293): the products of
|
|
3
|
+
* one order the buyer may rate, and which of them they already rated. The
|
|
4
|
+
* link is the reviews app's; the journey that sent it asked for it.
|
|
5
|
+
*/
|
|
6
|
+
export default defineEventHandler(async (event) => {
|
|
7
|
+
const token = routeToken(event);
|
|
8
|
+
try {
|
|
9
|
+
return await getMarketingService(event).reviewRequest(event, token);
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
return rethrowTokenRefusal(err);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ReviewInput } from "../../../interfaces/marketing";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The ratings a buyer gave on the review form. Only rated products are sent;
|
|
5
|
+
* the reviews app checks each one against the request and answers per product.
|
|
6
|
+
*/
|
|
7
|
+
export default defineEventHandler(async (event) => {
|
|
8
|
+
const token = routeToken(event);
|
|
9
|
+
const body = await readBody<{ reviews?: unknown }>(event).catch(() => null);
|
|
10
|
+
const reviews: ReviewInput[] = Array.isArray(body?.reviews)
|
|
11
|
+
? (body.reviews as Record<string, unknown>[])
|
|
12
|
+
.filter(review => review && typeof review.product_id === "string"
|
|
13
|
+
&& Number.isInteger(review.rating) && Number(review.rating) >= 1 && Number(review.rating) <= 5)
|
|
14
|
+
.slice(0, 50)
|
|
15
|
+
.map(review => ({
|
|
16
|
+
product_id: String(review.product_id),
|
|
17
|
+
rating: Number(review.rating),
|
|
18
|
+
...(typeof review.title === "string" && review.title.trim() ? { title: review.title.trim().slice(0, 200) } : {}),
|
|
19
|
+
...(typeof review.body === "string" && review.body.trim() ? { body: review.body.trim().slice(0, 5000) } : {}),
|
|
20
|
+
}))
|
|
21
|
+
: [];
|
|
22
|
+
if (!reviews.length) {
|
|
23
|
+
throw createError({ statusCode: 422, message: "No rating given" });
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return await getMarketingService(event).submitReviews(event, token, reviews, resolveLocale(event));
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
return rethrowTokenRefusal(err);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { H3Event } from "h3";
|
|
2
2
|
|
|
3
3
|
import type { CartItem } from "../../app/interfaces/cart-item";
|
|
4
|
+
import type { Attribution } from "../../app/shared/campaignAttribution";
|
|
4
5
|
|
|
5
6
|
/** Summary row for the cart switcher / account page. */
|
|
6
7
|
export interface CartSummaryRow {
|
|
@@ -43,4 +44,9 @@ export interface ICartManager {
|
|
|
43
44
|
claimSession(event: H3Event, contactId: string): Promise<void>;
|
|
44
45
|
/** Order hand-over: freeze the owner's current cart as ordered (+order_ref). */
|
|
45
46
|
orderActiveCart(event: H3Event, orderRef: string): Promise<string | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Where the visit came from, written onto the owner's current cart as a
|
|
49
|
+
* whole (null clears it). False when there is no cart to write it on yet.
|
|
50
|
+
*/
|
|
51
|
+
setActiveCartAttribution(event: H3Event, attribution: Attribution | null): Promise<boolean>;
|
|
46
52
|
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The marketing apps as the storefront reaches them (RAD-293): the double
|
|
5
|
+
* opt-in confirmation and the preference centre (marketing-consents), the
|
|
6
|
+
* review form (reviews), the loyalty account page and perk hint (loyalty),
|
|
7
|
+
* the audiences of a signed-in contact (audiences) and onsite touches
|
|
8
|
+
* (campaigns). Shapes follow the marketing contracts and their appendix.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Labels as the apps keep them: one text per language. */
|
|
12
|
+
export type Labels = Record<string, string> | null;
|
|
13
|
+
|
|
14
|
+
/** Why a token page cannot be used. */
|
|
15
|
+
export type TokenRefusal = "unknown" | "expired";
|
|
16
|
+
|
|
17
|
+
export class MarketingTokenError extends Error {
|
|
18
|
+
constructor(public readonly refusal: TokenRefusal) {
|
|
19
|
+
super(refusal === "expired" ? "link expired" : "link unknown");
|
|
20
|
+
this.name = "MarketingTokenError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Evidence {
|
|
25
|
+
ip: string | null;
|
|
26
|
+
user_agent: string | null;
|
|
27
|
+
page_url?: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ConfirmResult {
|
|
31
|
+
alreadyConfirmed: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PreferenceType {
|
|
35
|
+
code: string;
|
|
36
|
+
label: string;
|
|
37
|
+
description: string | null;
|
|
38
|
+
subscribed: boolean;
|
|
39
|
+
state: string | null;
|
|
40
|
+
consentTextId: string | null;
|
|
41
|
+
consentText: string | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface PreferenceView {
|
|
45
|
+
addressHint: string;
|
|
46
|
+
types: PreferenceType[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface PreferenceChoice {
|
|
50
|
+
subscription_type: string;
|
|
51
|
+
subscribed: boolean;
|
|
52
|
+
consent_text_id?: string | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface PreferenceChange {
|
|
56
|
+
subscription_type: string | null;
|
|
57
|
+
action: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ReviewProduct {
|
|
61
|
+
productId: string;
|
|
62
|
+
sku: string | null;
|
|
63
|
+
name: string;
|
|
64
|
+
reviewed: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ReviewRequestView {
|
|
68
|
+
orderNumber: string | null;
|
|
69
|
+
expiresAt: string | null;
|
|
70
|
+
products: ReviewProduct[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ReviewInput {
|
|
74
|
+
product_id: string;
|
|
75
|
+
rating: number;
|
|
76
|
+
title?: string;
|
|
77
|
+
body?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ReviewSubmitResult {
|
|
81
|
+
accepted: string[];
|
|
82
|
+
rejected: { productId: string | null; reason: string }[];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface LoyaltyTier {
|
|
86
|
+
code: string;
|
|
87
|
+
labels: Labels;
|
|
88
|
+
perks: Labels[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface LoyaltyProgressTier {
|
|
92
|
+
code: string;
|
|
93
|
+
labels: Labels;
|
|
94
|
+
threshold: number | null;
|
|
95
|
+
bonusPercent: number;
|
|
96
|
+
reached: boolean;
|
|
97
|
+
current: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface LoyaltyProgress {
|
|
101
|
+
period: string | number | null;
|
|
102
|
+
currency: string;
|
|
103
|
+
turnoverYtd: number;
|
|
104
|
+
projectedTurnover: number | null;
|
|
105
|
+
tiers: LoyaltyProgressTier[];
|
|
106
|
+
nextTier: { code: string; labels: Labels; gap: number; bonusPercent: number } | null;
|
|
107
|
+
bonusNow: number | null;
|
|
108
|
+
expectedBonus: number | null;
|
|
109
|
+
requiresNoOverdue: boolean;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface LoyaltyMembership {
|
|
113
|
+
program: { code: string; labels: Labels; kind: "points" | "turnover_bonus" | string };
|
|
114
|
+
tier: LoyaltyTier | null;
|
|
115
|
+
pointsBalance: number | null;
|
|
116
|
+
agreementId: string | null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface LoyaltyAccount {
|
|
120
|
+
memberships: LoyaltyMembership[];
|
|
121
|
+
progress: LoyaltyProgress | null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface CampaignTouch {
|
|
125
|
+
campaign_id: string;
|
|
126
|
+
kind: "onsite_impression" | "onsite_click";
|
|
127
|
+
ref?: string;
|
|
128
|
+
key?: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface IMarketingService {
|
|
132
|
+
confirmSubscription(event: H3Event, token: string, evidence: Evidence): Promise<ConfirmResult>;
|
|
133
|
+
preferences(event: H3Event, token: string, locale: string): Promise<PreferenceView>;
|
|
134
|
+
savePreferences(
|
|
135
|
+
event: H3Event,
|
|
136
|
+
token: string,
|
|
137
|
+
input: { choices?: PreferenceChoice[]; unsubscribeAll?: boolean; locale: string },
|
|
138
|
+
evidence: Evidence,
|
|
139
|
+
): Promise<{ view: PreferenceView; changes: PreferenceChange[] }>;
|
|
140
|
+
reviewRequest(event: H3Event, token: string): Promise<ReviewRequestView>;
|
|
141
|
+
submitReviews(event: H3Event, token: string, reviews: ReviewInput[], locale: string): Promise<ReviewSubmitResult>;
|
|
142
|
+
/** Null when the buyer holds no membership, or loyalty is not there to ask. */
|
|
143
|
+
loyaltyAccount(event: H3Event, holder: { contactId: string; organizationId?: string | null }): Promise<LoyaltyAccount | null>;
|
|
144
|
+
/** The audience codes of a signed-in contact; empty when unknown or unanswerable. */
|
|
145
|
+
audienceCodes(event: H3Event, contactId: string): Promise<string[]>;
|
|
146
|
+
recordTouches(
|
|
147
|
+
event: H3Event,
|
|
148
|
+
touches: (CampaignTouch & { contact_id?: string; session_key?: string; market?: string })[],
|
|
149
|
+
): Promise<void>;
|
|
150
|
+
}
|
|
@@ -3,6 +3,7 @@ import type { Models } from "@revenexx/sdk";
|
|
|
3
3
|
import type { H3Event } from "h3";
|
|
4
4
|
|
|
5
5
|
import type { CartItem } from "../../app/interfaces/cart-item";
|
|
6
|
+
import type { Attribution } from "../../app/shared/campaignAttribution";
|
|
6
7
|
import type { CartSummaryRow, ICartManager, ICartService } from "../interfaces/cart";
|
|
7
8
|
|
|
8
9
|
/** Cart row of the carts app. */
|
|
@@ -360,6 +361,21 @@ export class ApiCartService implements ICartService, ICartManager {
|
|
|
360
361
|
return cartId;
|
|
361
362
|
}
|
|
362
363
|
|
|
364
|
+
/**
|
|
365
|
+
* `PUT /v1/carts/{id} { attribution }` replaces the whole object (carts
|
|
366
|
+
* 2.9.0, RAD-287); first and last touch are the storefront's to compute.
|
|
367
|
+
* Through the escape hatch: the typed SDK predates the field.
|
|
368
|
+
*/
|
|
369
|
+
async setActiveCartAttribution(event: H3Event, attribution: Attribution | null): Promise<boolean> {
|
|
370
|
+
const cartId = await this.activeCartId(event);
|
|
371
|
+
if (!cartId) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
const query = visitCartKey(event, cartId) ?? undefined;
|
|
375
|
+
await useRevenexxSdk().call("PUT", cartPath(cartId), { ...(query ? { query } : {}), body: { attribution } });
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
|
|
363
379
|
async claimSession(event: H3Event, contactId: string): Promise<void> {
|
|
364
380
|
const sessionKey = getCookie(event, "cover-cart-session");
|
|
365
381
|
if (!sessionKey) {
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
CampaignTouch,
|
|
5
|
+
ConfirmResult,
|
|
6
|
+
Evidence,
|
|
7
|
+
IMarketingService,
|
|
8
|
+
Labels,
|
|
9
|
+
LoyaltyAccount,
|
|
10
|
+
LoyaltyMembership,
|
|
11
|
+
LoyaltyProgress,
|
|
12
|
+
PreferenceChange,
|
|
13
|
+
PreferenceChoice,
|
|
14
|
+
PreferenceView,
|
|
15
|
+
ReviewInput,
|
|
16
|
+
ReviewRequestView,
|
|
17
|
+
ReviewSubmitResult,
|
|
18
|
+
} from "../interfaces/marketing";
|
|
19
|
+
import { MarketingTokenError } from "../interfaces/marketing";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The marketing apps through the public revenexx API (RAD-293).
|
|
23
|
+
*
|
|
24
|
+
* Every call goes through the transport's escape hatch: the generated SDK
|
|
25
|
+
* predates marketing-consents, reviews, loyalty, audiences and campaigns.
|
|
26
|
+
* Routes and shapes are the ones the marketing contracts settle (CONTRACTS.md
|
|
27
|
+
* §3 and its appendix); a token page maps 404 and 410 to the refusal the
|
|
28
|
+
* visitor is shown, everything else stays an error.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
interface PreferenceAnswer {
|
|
32
|
+
address_hint?: string;
|
|
33
|
+
subscription_types?: {
|
|
34
|
+
code: string;
|
|
35
|
+
label?: string | null;
|
|
36
|
+
description?: string | null;
|
|
37
|
+
state?: string | null;
|
|
38
|
+
subscribed?: boolean;
|
|
39
|
+
consent_text?: { id?: string; wording?: string } | null;
|
|
40
|
+
}[];
|
|
41
|
+
changes?: { subscription_type: string | null; action: string }[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface LookupItem {
|
|
45
|
+
program?: { code?: string; labels?: Labels; kind?: string } | null;
|
|
46
|
+
tier?: { code?: string; labels?: Labels; perks?: Labels[] } | null;
|
|
47
|
+
points_balance?: number | null;
|
|
48
|
+
agreement?: { id?: string } | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface ProgressAnswer {
|
|
52
|
+
period?: string | number | null;
|
|
53
|
+
currency?: string;
|
|
54
|
+
turnover_ytd?: number;
|
|
55
|
+
projected_turnover?: number | null;
|
|
56
|
+
tiers?: { code: string; labels?: Labels; threshold?: number | null; bonus_percent?: number; reached?: boolean; current?: boolean }[];
|
|
57
|
+
next_tier?: { code: string; labels?: Labels; gap?: number; bonus_percent?: number } | null;
|
|
58
|
+
bonus_now?: number | null;
|
|
59
|
+
expected_bonus?: number | null;
|
|
60
|
+
rules?: { requires_no_overdue?: boolean };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function tokenPath(base: string, token: string): string {
|
|
64
|
+
return `${base}/${encodeURIComponent(token)}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 404 and 410 on a token route are the visitor's link, not a failure of the shop. */
|
|
68
|
+
function asTokenRefusal(err: unknown): never {
|
|
69
|
+
if (err instanceof RevenexxException && (err.code === 404 || err.code === 410)) {
|
|
70
|
+
throw new MarketingTokenError(err.code === 410 ? "expired" : "unknown");
|
|
71
|
+
}
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toPreferenceView(answer: PreferenceAnswer): PreferenceView {
|
|
76
|
+
return {
|
|
77
|
+
addressHint: answer.address_hint ?? "",
|
|
78
|
+
types: (answer.subscription_types ?? []).map(type => ({
|
|
79
|
+
code: type.code,
|
|
80
|
+
label: type.label || type.code,
|
|
81
|
+
description: type.description ?? null,
|
|
82
|
+
subscribed: type.subscribed === true,
|
|
83
|
+
state: type.state ?? null,
|
|
84
|
+
consentTextId: type.consent_text?.id ?? null,
|
|
85
|
+
consentText: type.consent_text?.wording ?? null,
|
|
86
|
+
})),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function toProgress(answer: ProgressAnswer): LoyaltyProgress {
|
|
91
|
+
return {
|
|
92
|
+
period: answer.period ?? null,
|
|
93
|
+
currency: answer.currency ?? "EUR",
|
|
94
|
+
turnoverYtd: Number(answer.turnover_ytd ?? 0),
|
|
95
|
+
projectedTurnover: answer.projected_turnover ?? null,
|
|
96
|
+
tiers: (answer.tiers ?? []).map(tier => ({
|
|
97
|
+
code: tier.code,
|
|
98
|
+
labels: tier.labels ?? null,
|
|
99
|
+
threshold: tier.threshold ?? null,
|
|
100
|
+
bonusPercent: Number(tier.bonus_percent ?? 0),
|
|
101
|
+
reached: tier.reached === true,
|
|
102
|
+
current: tier.current === true,
|
|
103
|
+
})),
|
|
104
|
+
nextTier: answer.next_tier
|
|
105
|
+
? {
|
|
106
|
+
code: answer.next_tier.code,
|
|
107
|
+
labels: answer.next_tier.labels ?? null,
|
|
108
|
+
gap: Number(answer.next_tier.gap ?? 0),
|
|
109
|
+
bonusPercent: Number(answer.next_tier.bonus_percent ?? 0),
|
|
110
|
+
}
|
|
111
|
+
: null,
|
|
112
|
+
bonusNow: answer.bonus_now ?? null,
|
|
113
|
+
expectedBonus: answer.expected_bonus ?? null,
|
|
114
|
+
requiresNoOverdue: answer.rules?.requires_no_overdue !== false,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export class ApiMarketingService implements IMarketingService {
|
|
119
|
+
async confirmSubscription(_event: H3Event, token: string, evidence: Evidence): Promise<ConfirmResult> {
|
|
120
|
+
try {
|
|
121
|
+
const answer = await useRevenexxSdk().call<{ already_confirmed?: boolean }>(
|
|
122
|
+
"POST",
|
|
123
|
+
tokenPath("/v1/marketing-consents/confirm", token),
|
|
124
|
+
{ body: { ip: evidence.ip, user_agent: evidence.user_agent, page_url: evidence.page_url ?? null } },
|
|
125
|
+
);
|
|
126
|
+
return { alreadyConfirmed: answer?.already_confirmed === true };
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
return asTokenRefusal(err);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async preferences(_event: H3Event, token: string, locale: string): Promise<PreferenceView> {
|
|
134
|
+
try {
|
|
135
|
+
const answer = await useRevenexxSdk().call<PreferenceAnswer>(
|
|
136
|
+
"GET",
|
|
137
|
+
tokenPath("/v1/marketing-consents/preferences", token),
|
|
138
|
+
{ query: { locale } },
|
|
139
|
+
);
|
|
140
|
+
return toPreferenceView(answer);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
return asTokenRefusal(err);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async savePreferences(
|
|
148
|
+
_event: H3Event,
|
|
149
|
+
token: string,
|
|
150
|
+
input: { choices?: PreferenceChoice[]; unsubscribeAll?: boolean; locale: string },
|
|
151
|
+
evidence: Evidence,
|
|
152
|
+
): Promise<{ view: PreferenceView; changes: PreferenceChange[] }> {
|
|
153
|
+
try {
|
|
154
|
+
const answer = await useRevenexxSdk().call<PreferenceAnswer>(
|
|
155
|
+
"POST",
|
|
156
|
+
tokenPath("/v1/marketing-consents/preferences", token),
|
|
157
|
+
{
|
|
158
|
+
body: {
|
|
159
|
+
...(input.unsubscribeAll ? { unsubscribe_all: true } : { choices: input.choices ?? [] }),
|
|
160
|
+
locale: input.locale,
|
|
161
|
+
ip: evidence.ip,
|
|
162
|
+
user_agent: evidence.user_agent,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
);
|
|
166
|
+
return { view: toPreferenceView(answer), changes: answer.changes ?? [] };
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
return asTokenRefusal(err);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async reviewRequest(_event: H3Event, token: string): Promise<ReviewRequestView> {
|
|
174
|
+
try {
|
|
175
|
+
const answer = await useRevenexxSdk().call<{
|
|
176
|
+
state?: string;
|
|
177
|
+
expires_at?: string | null;
|
|
178
|
+
order_number?: string | null;
|
|
179
|
+
products?: { product_id: string; sku?: string | null; name?: string; reviewed?: boolean }[];
|
|
180
|
+
}>("GET", tokenPath("/v1/reviews/requests", token));
|
|
181
|
+
// A link the app still answers but has marked expired reads as expired.
|
|
182
|
+
if (answer.state === "expired") {
|
|
183
|
+
throw new MarketingTokenError("expired");
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
orderNumber: answer.order_number ?? null,
|
|
187
|
+
expiresAt: answer.expires_at ?? null,
|
|
188
|
+
products: (answer.products ?? []).map(product => ({
|
|
189
|
+
productId: product.product_id,
|
|
190
|
+
sku: product.sku ?? null,
|
|
191
|
+
name: product.name || product.sku || product.product_id,
|
|
192
|
+
reviewed: product.reviewed === true,
|
|
193
|
+
})),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
if (err instanceof MarketingTokenError) {
|
|
198
|
+
throw err;
|
|
199
|
+
}
|
|
200
|
+
return asTokenRefusal(err);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async submitReviews(_event: H3Event, token: string, reviews: ReviewInput[], locale: string): Promise<ReviewSubmitResult> {
|
|
205
|
+
try {
|
|
206
|
+
const answer = await useRevenexxSdk().call<{
|
|
207
|
+
reviews?: { product_id: string }[];
|
|
208
|
+
rejected?: { product_id?: string | null; reason: string }[];
|
|
209
|
+
}>("POST", tokenPath("/v1/reviews/submit", token), { body: { reviews, locale } });
|
|
210
|
+
return {
|
|
211
|
+
accepted: (answer.reviews ?? []).map(review => review.product_id),
|
|
212
|
+
rejected: (answer.rejected ?? []).map(entry => ({ productId: entry.product_id ?? null, reason: entry.reason })),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
// Nothing accepted at all is a 400 with the reasons — the form shows them.
|
|
217
|
+
if (err instanceof RevenexxException && err.code === 400 && apiErrorCode(err) === "nothing_accepted") {
|
|
218
|
+
return { accepted: [], rejected: reviews.map(review => ({ productId: review.product_id, reason: "nothing_accepted" })) };
|
|
219
|
+
}
|
|
220
|
+
return asTokenRefusal(err);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async loyaltyAccount(_event: H3Event, holder: { contactId: string; organizationId?: string | null }): Promise<LoyaltyAccount | null> {
|
|
225
|
+
const sdk = useRevenexxSdk();
|
|
226
|
+
let items: LookupItem[];
|
|
227
|
+
try {
|
|
228
|
+
const answer = await sdk.call<LookupItem & { items?: LookupItem[] }>("GET", "/v1/loyalty/memberships/lookup", {
|
|
229
|
+
query: {
|
|
230
|
+
contact_id: holder.contactId,
|
|
231
|
+
...(holder.organizationId ? { organization_id: holder.organizationId } : {}),
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
items = answer.items ?? (answer.program ? [answer] : []);
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
// Loyalty not installed, or not answering: the buyer simply has no
|
|
238
|
+
// programme to be shown — never an error page in the account.
|
|
239
|
+
getLogService().warn("loyalty lookup failed", apiErrorContext(err));
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
const memberships: LoyaltyMembership[] = items
|
|
243
|
+
.filter(item => item.program?.code)
|
|
244
|
+
.map(item => ({
|
|
245
|
+
program: { code: item.program!.code!, labels: item.program!.labels ?? null, kind: item.program!.kind ?? "points" },
|
|
246
|
+
tier: item.tier?.code ? { code: item.tier.code, labels: item.tier.labels ?? null, perks: item.tier.perks ?? [] } : null,
|
|
247
|
+
pointsBalance: typeof item.points_balance === "number" ? item.points_balance : null,
|
|
248
|
+
agreementId: item.agreement?.id ?? null,
|
|
249
|
+
}));
|
|
250
|
+
if (!memberships.length) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
const agreementId = memberships.find(m => m.agreementId)?.agreementId ?? null;
|
|
254
|
+
let progress: LoyaltyProgress | null = null;
|
|
255
|
+
if (agreementId) {
|
|
256
|
+
try {
|
|
257
|
+
progress = toProgress(await sdk.call<ProgressAnswer>(
|
|
258
|
+
"GET",
|
|
259
|
+
`/v1/loyalty/bonus-agreements/${encodeURIComponent(agreementId)}/progress`,
|
|
260
|
+
));
|
|
261
|
+
}
|
|
262
|
+
catch (err) {
|
|
263
|
+
getLogService().warn("loyalty progress failed", apiErrorContext(err));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return { memberships, progress };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async audienceCodes(_event: H3Event, contactId: string): Promise<string[]> {
|
|
270
|
+
try {
|
|
271
|
+
const answer = await useRevenexxSdk().call<{ codes?: string[] }>("POST", "/v1/audiences/memberships", {
|
|
272
|
+
body: { contact_id: contactId },
|
|
273
|
+
});
|
|
274
|
+
return Array.isArray(answer.codes) ? answer.codes.filter(code => typeof code === "string") : [];
|
|
275
|
+
}
|
|
276
|
+
catch (err) {
|
|
277
|
+
// Unanswerable reads as anonymous: a visitor then sees what everybody sees.
|
|
278
|
+
getLogService().warn("audience memberships failed", apiErrorContext(err));
|
|
279
|
+
return [];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async recordTouches(
|
|
284
|
+
_event: H3Event,
|
|
285
|
+
touches: (CampaignTouch & { contact_id?: string; session_key?: string; market?: string })[],
|
|
286
|
+
): Promise<void> {
|
|
287
|
+
if (!touches.length) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
await useRevenexxSdk().call("POST", "/v1/campaigns/touches", { body: { touches } });
|
|
291
|
+
}
|
|
292
|
+
}
|