@odla-ai/chapter 0.38.1 → 0.39.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.
@@ -3346,9 +3346,21 @@ function webhookMutationId(eventId) {
3346
3346
  function findApplicationRef(obj) {
3347
3347
  const metaOf = (v) => v && typeof v === "object" ? v.metadata ?? {} : {};
3348
3348
  const pick = (m) => typeof m.applicationId === "string" ? m.applicationId : void 0;
3349
- const applicationId = pick(metaOf(obj)) ?? pick(metaOf(obj.subscription_details)) ?? pick(metaOf(obj.parent?.subscription_details));
3349
+ const candidates = [
3350
+ metaOf(obj),
3351
+ metaOf(obj.subscription_details),
3352
+ metaOf(obj.parent?.subscription_details)
3353
+ ];
3354
+ const applicationId = candidates.map(pick).find(Boolean);
3355
+ const appId = candidates.map((metadata) => typeof metadata.odlaAppId === "string" ? metadata.odlaAppId : void 0).find(Boolean);
3356
+ const environment = candidates.map((metadata) => typeof metadata.odlaEnvironment === "string" ? metadata.odlaEnvironment : void 0).find(Boolean);
3350
3357
  const customerId = typeof obj.customer === "string" ? obj.customer : void 0;
3351
- return { ...applicationId ? { applicationId } : {}, ...customerId ? { customerId } : {} };
3358
+ return {
3359
+ ...applicationId ? { applicationId } : {},
3360
+ ...customerId ? { customerId } : {},
3361
+ ...appId ? { appId } : {},
3362
+ ...environment ? { environment } : {}
3363
+ };
3352
3364
  }
3353
3365
  function findGiftRef(obj) {
3354
3366
  const metaOf = (value) => value && typeof value === "object" ? value.metadata ?? {} : {};
@@ -3439,7 +3451,13 @@ function createStripeProvider(config) {
3439
3451
  return {
3440
3452
  async createSubscription(input) {
3441
3453
  const sk = requireSecret(secretKey);
3442
- const meta = { applicationId: input.applicationId, groupId: input.groupId, email: input.email };
3454
+ const meta = {
3455
+ applicationId: input.applicationId,
3456
+ groupId: input.groupId,
3457
+ email: input.email,
3458
+ odlaAppId: input.appId,
3459
+ odlaEnvironment: input.environment
3460
+ };
3443
3461
  let customerId = input.existingCustomerId;
3444
3462
  if (!customerId) {
3445
3463
  const cust = await stripeCall(
@@ -3959,6 +3977,8 @@ async function startSubscription(req, env, ctx) {
3959
3977
  let result;
3960
3978
  try {
3961
3979
  result = await provider.createSubscription({
3980
+ appId: env.ODLA_APP_ID,
3981
+ environment: env.ODLA_ENV,
3962
3982
  applicationId,
3963
3983
  groupId: String(group.id),
3964
3984
  email: String(app.email ?? ""),
@@ -3988,6 +4008,10 @@ async function ingestWebhook(req, env, ctx) {
3988
4008
  if (!ingest.ok) return json({ error: "invalid signature" }, 400);
3989
4009
  const { eventId, event } = ingest;
3990
4010
  if (event.kind === "ignored") return json({ ok: true, ignored: event.type });
4011
+ const identifiedEnvironment = event.appId !== void 0 || event.environment !== void 0;
4012
+ if (env.ODLA_APP_ID && identifiedEnvironment && (event.appId !== env.ODLA_APP_ID || event.environment !== env.ODLA_ENV)) {
4013
+ return json({ ok: true, matched: false });
4014
+ }
3991
4015
  if (event.kind === "gift_paid" || event.kind === "gift_revoked") {
3992
4016
  const intent = await firstRow2(db, "giftPurchaseIntents", { where: { id: event.giftIntentId }, limit: 1 });
3993
4017
  if (!intent || intent.chapterId !== ctx.chapter.id || intent.environment !== env.ODLA_ENV) {