@misiki/gocommerce-connector 0.21.0 → 0.23.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.
@@ -174,6 +174,41 @@ function metaBrowser() {
174
174
  event_source_url: window.location.origin + window.location.pathname
175
175
  };
176
176
  }
177
+ var ATTRIBUTION_KEY = "gc_attribution";
178
+ var ATTRIBUTION_DAYS = 30;
179
+ var TOUCH_FIELDS = [
180
+ "utm_source",
181
+ "utm_medium",
182
+ "utm_campaign",
183
+ "utm_content",
184
+ "utm_term",
185
+ "gclid",
186
+ "gbraid",
187
+ "wbraid",
188
+ "fbclid",
189
+ "ttclid",
190
+ "msclkid",
191
+ "referrer",
192
+ "at"
193
+ ];
194
+ function cleanTouch(raw, now) {
195
+ if (!raw || typeof raw !== "object") return void 0;
196
+ const at = Date.parse(String(raw.at ?? ""));
197
+ if (!Number.isFinite(at) || now - at > ATTRIBUTION_DAYS * 864e5 || at - now > 864e5) return void 0;
198
+ const out = {};
199
+ for (const k of TOUCH_FIELDS) {
200
+ const v = raw[k];
201
+ if (typeof v === "string" && v.trim()) out[k] = v.trim().slice(0, 200);
202
+ }
203
+ return Object.keys(out).length > 1 ? out : void 0;
204
+ }
205
+ function landingAttribution(now = Date.now()) {
206
+ const saved = readJSON(ATTRIBUTION_KEY);
207
+ const last = cleanTouch(saved?.last, now);
208
+ const first = cleanTouch(saved?.first, now) ?? last;
209
+ if (!first && !last) return void 0;
210
+ return { ...first ? { first } : {}, ...last ? { last } : {} };
211
+ }
177
212
  function storedCartId() {
178
213
  const v = readLocal("cart_id");
179
214
  if (!v || v === "undefined" || v === "null") return null;
@@ -2202,10 +2237,18 @@ var CheckoutService = class extends BaseService {
2202
2237
  const retry = extras.attempt?.body === `${code}:${serialised}`;
2203
2238
  const key = retry ? extras.attempt.key : newKey();
2204
2239
  const meta = retry ? extras.attempt?.meta : metaBrowser();
2205
- saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {} } });
2240
+ const attribution = retry ? extras.attempt?.attribution : landingAttribution();
2241
+ saveCartExtras(id2, {
2242
+ attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {}, ...attribution ? { attribution } : {} }
2243
+ });
2244
+ const metadata = {
2245
+ ...extra.metadata,
2246
+ ...meta ? { meta_capi: meta } : {},
2247
+ ...attribution ? { attribution } : {}
2248
+ };
2206
2249
  const result = await this.data(`/api/checkout/${encodeURIComponent(code)}`, {
2207
2250
  method: "POST",
2208
- body: meta ? { ...body, metadata: { ...extra.metadata, meta_capi: meta } } : body,
2251
+ body: Object.keys(metadata).length ? { ...body, metadata } : body,
2209
2252
  headers: { "Idempotency-Key": key }
2210
2253
  });
2211
2254
  if (result.order.access_token) {
@@ -3657,6 +3700,50 @@ function withAmounts(v) {
3657
3700
  }
3658
3701
  var variantService = new VariantService();
3659
3702
 
3703
+ // src/services/whatsapp-service.ts
3704
+ var WhatsappService = class extends BaseService {
3705
+ /**
3706
+ * Whether to show the boxes. Never throws: an engine without the module, or
3707
+ * one that fails, answers both off, because checkout must not depend on it.
3708
+ */
3709
+ async consentOffer() {
3710
+ try {
3711
+ const d = await this.data("/x/notify-whatsapp/consent");
3712
+ const enabled = d?.enabled === true;
3713
+ return { enabled, marketing: enabled && d?.marketing === true };
3714
+ } catch {
3715
+ return { enabled: false, marketing: false };
3716
+ }
3717
+ }
3718
+ /**
3719
+ * Records both answers for the phone as typed, against the bag (with
3720
+ * `marketing`, the engine keeps the phone on it for basket reminders). Throws
3721
+ * the engine's 400 (`validation_failed`) when the phone is not a WhatsApp
3722
+ * number for the store's country.
3723
+ */
3724
+ async setConsent({
3725
+ cartId,
3726
+ phone,
3727
+ orderUpdates,
3728
+ marketing
3729
+ }) {
3730
+ const d = await this.data(
3731
+ "/x/notify-whatsapp/consent",
3732
+ {
3733
+ method: "PUT",
3734
+ body: {
3735
+ cart_id: cartId || storedCartId() || "",
3736
+ phone: String(phone ?? "").trim(),
3737
+ order_updates: orderUpdates === true,
3738
+ marketing: marketing === true
3739
+ }
3740
+ }
3741
+ );
3742
+ return { phone: d.phone, orderUpdates: d.order_updates, marketing: d.marketing };
3743
+ }
3744
+ };
3745
+ var whatsappService = new WhatsappService();
3746
+
3660
3747
  // src/services/b2b-service.ts
3661
3748
  var B2bService = class extends BaseService {
3662
3749
  // ------------------------------------------------------------ company
@@ -3977,6 +4064,7 @@ exports.VarniCustomDesignService = VarniCustomDesignService;
3977
4064
  exports.VarniCustomProductService = VarniCustomProductService;
3978
4065
  exports.VendorService = VendorService;
3979
4066
  exports.WarrantyService = WarrantyService;
4067
+ exports.WhatsappService = WhatsappService;
3980
4068
  exports.WishlistService = WishlistService;
3981
4069
  exports.addressService = addressService;
3982
4070
  exports.authService = authService;
@@ -4039,6 +4127,7 @@ exports.varniCustomDesignService = varniCustomDesignService;
4039
4127
  exports.varniCustomProductService = varniCustomProductService;
4040
4128
  exports.vendorService = vendorService;
4041
4129
  exports.warrantyService = warrantyService;
4130
+ exports.whatsappService = whatsappService;
4042
4131
  exports.wishlistService = wishlistService;
4043
4132
  //# sourceMappingURL=index.cjs.map
4044
4133
  //# sourceMappingURL=index.cjs.map