@fire-spark/analytics-masivo 0.17.3 → 0.19.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.
Files changed (3) hide show
  1. package/README.md +13 -0
  2. package/dist/index.js +56 -15
  3. package/package.json +4 -4
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @fire-spark/analytics-masivo
2
+
3
+ Masivo destination for `@fire-spark/analytics`. Sends Masivo's Emit event
4
+ body (https://docs.masivo.ai/api-reference/behavior/events/post).
5
+
6
+ ```typescript
7
+ import { masivo } from "@fire-spark/analytics-masivo";
8
+
9
+ masivo({ publishableKey: "pk_live_..." });
10
+ ```
11
+
12
+ Pass it to `adapters` in `configure()`. Options and behavior:
13
+ https://firespark.cloud/docs/guides/analytics/destinations#masivo
package/dist/index.js CHANGED
@@ -32,32 +32,73 @@ const masivoRewardRedemptions = (tenders, paymentId) => {
32
32
  });
33
33
  });
34
34
  };
35
- /** Adds `redemptions` (one per reward) when the event carries `tenders`. */
36
- const withRedemptions = (params) => {
37
- if (!Array.isArray(params.tenders)) return params;
38
- const paymentId = stringOr(params.masivo_redemption_payment_id);
39
- const redemptions = masivoRewardRedemptions(params.tenders, paymentId);
40
- if (redemptions.length === 0) return params;
41
- return {
42
- ...params,
43
- redemptions
44
- };
35
+ const isRecord = (value) => {
36
+ return typeof value === "object" && value !== null && !Array.isArray(value);
37
+ };
38
+ const RAW_REDEMPTION_KEYS = ["tenders", "masivo_redemption_payment_id"];
39
+ const withoutRaw = (record) => {
40
+ const next = { ...record };
41
+ for (const key of RAW_REDEMPTION_KEYS) delete next[key];
42
+ return next;
43
+ };
44
+ /**
45
+ * Raw `tenders` never reach Masivo (they are not part of the Emit event
46
+ * body). Their rewards go to `metadata.redemptions`, one per reward, and not
47
+ * to `redeem`: the redemption already happened at payment time.
48
+ */
49
+ const withRedemptions = (body) => {
50
+ const metadata = isRecord(body.metadata) ? body.metadata : {};
51
+ const tenders = body.tenders ?? metadata.tenders;
52
+ const b1 = body.masivo_redemption_payment_id;
53
+ const paymentId = stringOr(b1 ?? metadata.masivo_redemption_payment_id);
54
+ const next = withoutRaw(body);
55
+ const cleanMetadata = withoutRaw(metadata);
56
+ if (Array.isArray(tenders)) {
57
+ const redemptions = masivoRewardRedemptions(tenders, paymentId);
58
+ if (redemptions.length > 0) cleanMetadata.redemptions = redemptions;
59
+ }
60
+ if (Object.keys(cleanMetadata).length === 0) {
61
+ delete next.metadata;
62
+ return next;
63
+ }
64
+ next.metadata = cleanMetadata;
65
+ return next;
66
+ };
67
+ const REQUIRED_FIELDS = [
68
+ "customer_id",
69
+ "brand_id",
70
+ "type"
71
+ ];
72
+ /** Masivo rejects the Emit event body without these fields. */
73
+ const assertRequired = (name, body) => {
74
+ const missing = REQUIRED_FIELDS.filter((field) => {
75
+ const value = body[field];
76
+ return typeof value !== "string" || value.trim() === "";
77
+ });
78
+ if (missing.length === 0) return;
79
+ const list = missing.join(", ");
80
+ throw new Error(`Masivo event "${name}" is missing ${list}`);
45
81
  };
46
82
  const masivo = (options) => {
47
83
  const key = options.publishableKey?.trim() ?? "";
48
84
  if (!key) throw new Error("Masivo publishableKey is required");
85
+ let initialized = null;
49
86
  return {
50
87
  provider: "masivo",
51
88
  send: async (event) => {
52
- const params = withRedemptions(event.params);
89
+ const body = withRedemptions(event.params);
90
+ assertRequired(event.name, body);
53
91
  if (options.track) {
54
- await options.track(event.name, params);
92
+ await options.track(event.name, body);
55
93
  return;
56
94
  }
57
95
  const sdk = existingSdk();
58
- sdk?.init?.(key);
59
- if (!sdk?.track) return;
60
- await sdk.track(event.name, params);
96
+ if (!sdk?.track) throw new Error("Masivo SDK not found; pass `track` to masivo()");
97
+ if (initialized !== sdk) {
98
+ sdk.init?.(key);
99
+ initialized = sdk;
100
+ }
101
+ await sdk.track(event.name, body);
61
102
  }
62
103
  };
63
104
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fire-spark/analytics-masivo",
3
- "version": "0.17.3",
3
+ "version": "0.19.0",
4
4
  "description": "Fire Spark Masivo analytics adapter (public-key SDK)",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -17,16 +17,16 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "@fire-spark/payments": "^0.17.3"
20
+ "@fire-spark/payments": "^0.19.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@fire-spark/analytics": "^0.17.3"
23
+ "@fire-spark/analytics": "^0.19.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "tsdown": "^0.22.14",
27
27
  "typescript": "^7.0.2",
28
28
  "vitest": "^2.1.8",
29
- "@fire-spark/analytics": "0.17.3"
29
+ "@fire-spark/analytics": "0.19.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"