@redeemid/emdash 0.0.7 → 0.0.9

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/src/webhooks.ts CHANGED
@@ -88,7 +88,13 @@ export type Outcome = "processed" | "skipped" | "stale" | "ignored";
88
88
  * Apply one webhook to the mirror. Throws on storage errors so the caller can
89
89
  * answer 500 and let Shopify retry.
90
90
  */
91
- export async function apply(s: Stores, topic: string, payload: Record<string, unknown>): Promise<Outcome> {
91
+ export async function apply(
92
+ s: Stores,
93
+ topic: string,
94
+ payload: Record<string, unknown>,
95
+ /** Called after a member is written, with their tags before (null when new). */
96
+ onWrite?: (beforeTags: string[] | null, after: MemberRecord) => Promise<void> | void,
97
+ ): Promise<Outcome> {
92
98
  const kind = kindOf(topic);
93
99
  if (!kind || kind === "ignored") return "ignored";
94
100
 
@@ -128,7 +134,9 @@ export async function apply(s: Stores, topic: string, payload: Record<string, un
128
134
  return "processed";
129
135
  }
130
136
 
131
- const current: MemberRecord = (await s.members.get(id)) ?? emptyMember(id);
137
+ const existing = await s.members.get(id);
138
+ const beforeTags = existing ? [...existing.tags] : null;
139
+ const current: MemberRecord = existing ?? emptyMember(id);
132
140
  const updatedAt = time(payload.updated_at);
133
141
 
134
142
  if (kind === "profile") {
@@ -157,5 +165,6 @@ export async function apply(s: Stores, topic: string, payload: Record<string, un
157
165
  current.syncedAt = new Date().toISOString();
158
166
  current.source = topic;
159
167
  await s.members.put(id, stampTags(current));
168
+ await onWrite?.(beforeTags, current);
160
169
  return "processed";
161
170
  }