better-auth-streampay 2.1.0 → 2.2.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/README.md +35 -8
- package/dist/client.cjs +2 -0
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +4 -2
- package/dist/client.d.ts +4 -2
- package/dist/client.js +2 -0
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +442 -81
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -2
- package/dist/index.d.ts +15 -2
- package/dist/index.js +442 -81
- package/dist/index.js.map +1 -1
- package/dist/{streampay-DTuEbhQ9.d.cts → streampay-B3vf7Hvq.d.cts} +1388 -153
- package/dist/{streampay-DTuEbhQ9.d.ts → streampay-B3vf7Hvq.d.ts} +1388 -153
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -45,6 +45,8 @@ var $ERROR_CODES = defineErrorCodes({
|
|
|
45
45
|
SUBSCRIPTION_EXPIRED: "The subscription has expired.",
|
|
46
46
|
SUBSCRIPTION_ALREADY_SCHEDULED_CANCEL: "The subscription is already scheduled to cancel at period end.",
|
|
47
47
|
SUBSCRIPTION_ALREADY_ON_PLAN: "The subscription is already on the requested plan.",
|
|
48
|
+
SUBSCRIPTION_SEAT_COUNT_INVALID: "The requested seat count is invalid for this plan.",
|
|
49
|
+
SUBSCRIPTION_SEAT_CHANGE_ALREADY_SCHEDULED: "A different subscription change is already scheduled.",
|
|
48
50
|
SUBSCRIPTION_IMMEDIATE_CANCEL_UNSUPPORTED: "StreamPay does not support immediate cancellation for active subscriptions.",
|
|
49
51
|
SUBSCRIPTION_PERIOD_END_CANCEL_UNSUPPORTED: "StreamPay only supports period-end cancellation for active subscriptions.",
|
|
50
52
|
SUBSCRIPTION_PLAN_GROUP_MISMATCH: "Subscription plan changes must stay within the same configured plan group.",
|
|
@@ -672,7 +674,8 @@ function projectSubscriptionFields(sub) {
|
|
|
672
674
|
}
|
|
673
675
|
function parseDate(value) {
|
|
674
676
|
if (!value) return null;
|
|
675
|
-
const
|
|
677
|
+
const normalized = /[T ]/.test(value) && !/(?:Z|[+-]\d{2}:?\d{2})$/i.test(value) ? `${value}Z` : value;
|
|
678
|
+
const date = new Date(normalized);
|
|
676
679
|
return Number.isNaN(date.getTime()) ? null : date;
|
|
677
680
|
}
|
|
678
681
|
function minorUnitExponent(currency) {
|
|
@@ -711,13 +714,19 @@ function amountToSmallestUnit(value, currency) {
|
|
|
711
714
|
}
|
|
712
715
|
var SubscriptionItemSchema = zod.z.object({
|
|
713
716
|
product: zod.z.object({ id: zod.z.string() }).passthrough().optional(),
|
|
714
|
-
product_id: zod.z.string().optional()
|
|
717
|
+
product_id: zod.z.string().optional(),
|
|
718
|
+
quantity: zod.z.number().optional()
|
|
715
719
|
}).passthrough();
|
|
716
720
|
function subscriptionItemProductId(item) {
|
|
717
721
|
const parsed = SubscriptionItemSchema.safeParse(item);
|
|
718
722
|
if (!parsed.success) return null;
|
|
719
723
|
return parsed.data.product?.id ?? parsed.data.product_id ?? null;
|
|
720
724
|
}
|
|
725
|
+
function subscriptionItemQuantity(item) {
|
|
726
|
+
const parsed = SubscriptionItemSchema.safeParse(item);
|
|
727
|
+
if (!parsed.success || parsed.data.quantity === void 0) return null;
|
|
728
|
+
return Number.isSafeInteger(parsed.data.quantity) && parsed.data.quantity > 0 ? parsed.data.quantity : null;
|
|
729
|
+
}
|
|
721
730
|
function inferPlanFromItems(sub, plans) {
|
|
722
731
|
if (!Array.isArray(sub.items)) return null;
|
|
723
732
|
return inferPlanFromItemList(sub.items, plans);
|
|
@@ -726,14 +735,32 @@ function inferPlanFromItemList(items, plans) {
|
|
|
726
735
|
const productIds = new Set(
|
|
727
736
|
items.map(subscriptionItemProductId).filter((id) => Boolean(id))
|
|
728
737
|
);
|
|
729
|
-
|
|
738
|
+
const matches = plans.list.filter((plan) => productIds.has(plan.productId));
|
|
739
|
+
if (matches.length > 1) {
|
|
740
|
+
throw new api.APIError("INTERNAL_SERVER_ERROR", {
|
|
741
|
+
code: $ERROR_CODES.SUBSCRIPTION_INVALID_STATE.code,
|
|
742
|
+
message: "StreamPay returned a subscription containing multiple configured plan products."
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
return matches[0]?.name ?? null;
|
|
730
746
|
}
|
|
731
747
|
function projectPlanFields(sub, plans) {
|
|
732
748
|
const currentPlan = inferPlanFromItems(sub, plans);
|
|
733
|
-
const currentProductId = subscriptionItemProductId(sub.items?.[0]);
|
|
734
|
-
const pendingPlan = Array.isArray(sub.pending_change?.target_items) ? inferPlanFromItemList(sub.pending_change.target_items, plans) : null;
|
|
735
|
-
const pendingProductId = subscriptionItemProductId(sub.pending_change?.target_items?.[0]);
|
|
736
749
|
const configuredCurrent = currentPlan ? plans.byName.get(currentPlan) : void 0;
|
|
750
|
+
const currentItem = configuredCurrent ? sub.items?.find((item) => subscriptionItemProductId(item) === configuredCurrent.productId) : sub.items?.[0];
|
|
751
|
+
const currentProductId = subscriptionItemProductId(currentItem);
|
|
752
|
+
const currentSeats = currentItem ? subscriptionItemQuantity(currentItem) ?? 1 : null;
|
|
753
|
+
const inferredPendingPlan = Array.isArray(sub.pending_change?.target_items) ? inferPlanFromItemList(sub.pending_change.target_items, plans) : null;
|
|
754
|
+
const configuredPending = inferredPendingPlan ? plans.byName.get(inferredPendingPlan) : void 0;
|
|
755
|
+
const pendingItem = configuredPending ? sub.pending_change?.target_items?.find(
|
|
756
|
+
(item) => subscriptionItemProductId(item) === configuredPending.productId
|
|
757
|
+
) : currentProductId ? sub.pending_change?.target_items?.find(
|
|
758
|
+
(item) => subscriptionItemProductId(item) === currentProductId
|
|
759
|
+
) : sub.pending_change?.target_items?.[0];
|
|
760
|
+
const pendingProductId = subscriptionItemProductId(pendingItem);
|
|
761
|
+
const pendingSeats = pendingItem ? subscriptionItemQuantity(pendingItem) ?? 1 : null;
|
|
762
|
+
const pendingEffectiveAt = parseDate(sub.pending_change?.effective_at);
|
|
763
|
+
const planChanges = Boolean(inferredPendingPlan) && (inferredPendingPlan !== currentPlan || pendingProductId !== currentProductId);
|
|
737
764
|
return {
|
|
738
765
|
...currentPlan ? {
|
|
739
766
|
plan: currentPlan,
|
|
@@ -741,9 +768,12 @@ function projectPlanFields(sub, plans) {
|
|
|
741
768
|
productId: currentProductId,
|
|
742
769
|
group: configuredCurrent?.group ?? null
|
|
743
770
|
} : currentProductId ? { productId: currentProductId } : {},
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
771
|
+
...currentSeats === null ? {} : { seats: currentSeats },
|
|
772
|
+
pendingPlan: planChanges ? inferredPendingPlan : null,
|
|
773
|
+
pendingProductId: planChanges ? pendingProductId : null,
|
|
774
|
+
pendingPlanEffectiveAt: planChanges ? pendingEffectiveAt : null,
|
|
775
|
+
pendingSeats,
|
|
776
|
+
pendingSeatsEffectiveAt: pendingSeats === null ? null : pendingEffectiveAt
|
|
747
777
|
};
|
|
748
778
|
}
|
|
749
779
|
function subscriptionSlotKey(referenceType, referenceId, group) {
|
|
@@ -2944,9 +2974,10 @@ function checkoutInProgressError() {
|
|
|
2944
2974
|
});
|
|
2945
2975
|
}
|
|
2946
2976
|
async function resumeOrReserveCheckoutSlot(args) {
|
|
2947
|
-
const { client, adapter, candidates, activeSlotKey, planName, now, log } = args;
|
|
2977
|
+
const { client, adapter, candidates, activeSlotKey, planName, seats, now, log } = args;
|
|
2978
|
+
const matchesCheckout = (row2) => row2.plan === planName && (row2.seats ?? 1) === seats;
|
|
2948
2979
|
const reuseCandidate = candidates.find(
|
|
2949
|
-
(row2) => row2
|
|
2980
|
+
(row2) => matchesCheckout(row2) && row2.status === "incomplete" && Boolean(row2.streampayPaymentLinkId) && row2.createdAt instanceof Date && now - row2.createdAt.getTime() < UPGRADE_IDEMPOTENCY_WINDOW_MS
|
|
2950
2981
|
);
|
|
2951
2982
|
if (reuseCandidate) {
|
|
2952
2983
|
let recovery;
|
|
@@ -2985,21 +3016,23 @@ async function resumeOrReserveCheckoutSlot(args) {
|
|
|
2985
3016
|
where: [{ field: "activeSlotKey", value: activeSlotKey }]
|
|
2986
3017
|
});
|
|
2987
3018
|
if (reserved?.status !== "incomplete") throw err;
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3019
|
+
if (matchesCheckout(reserved)) {
|
|
3020
|
+
let recovery;
|
|
3021
|
+
try {
|
|
3022
|
+
recovery = await recoverCheckoutUrl(client, reserved);
|
|
3023
|
+
} catch (recoveryError) {
|
|
3024
|
+
toAPIError(
|
|
3025
|
+
{
|
|
3026
|
+
logPrefix: `resume concurrent subscription checkout failed for row=${reserved.id}:`,
|
|
3027
|
+
userMessage: "Unable to resume subscription checkout."
|
|
3028
|
+
},
|
|
3029
|
+
recoveryError,
|
|
3030
|
+
log
|
|
3031
|
+
);
|
|
3032
|
+
}
|
|
3033
|
+
if (recovery.kind === "recovered") {
|
|
3034
|
+
return { kind: "recovered", row: reserved, url: recovery.url };
|
|
3035
|
+
}
|
|
3003
3036
|
}
|
|
3004
3037
|
const reservationIsStale = !(reserved.createdAt instanceof Date) || now - reserved.createdAt.getTime() >= UPGRADE_IDEMPOTENCY_WINDOW_MS;
|
|
3005
3038
|
if (!reservationIsStale) throw checkoutInProgressError();
|
|
@@ -3025,24 +3058,67 @@ async function resumeOrReserveCheckoutSlot(args) {
|
|
|
3025
3058
|
}
|
|
3026
3059
|
return { kind: "reserved", row, consumerId };
|
|
3027
3060
|
}
|
|
3028
|
-
|
|
3029
|
-
// src/plugins/subscriptions/lifecycle.ts
|
|
3030
3061
|
function isAlreadyCanceledFreezeError(err) {
|
|
3031
3062
|
const sdkError = readSdkErrorFields(err);
|
|
3032
3063
|
const envelope = readEnvelope(sdkError.body);
|
|
3033
3064
|
return sdkError.status === 403 && envelope?.code === "STREAM_ERROR" && envelope.additionalInfo === "Cannot delete non-latest freeze entry.";
|
|
3034
3065
|
}
|
|
3035
3066
|
function subscriptionCouponIds(stream) {
|
|
3067
|
+
return couponIdsFromMetadata(stream.coupon_calculation_metadata);
|
|
3068
|
+
}
|
|
3069
|
+
function couponIdsFromMetadata(metadata) {
|
|
3036
3070
|
const coupons = [];
|
|
3037
|
-
for (const coupon of
|
|
3071
|
+
for (const coupon of metadata?.coupons ?? []) {
|
|
3038
3072
|
if (coupon.coupon_id) coupons.push(coupon.coupon_id);
|
|
3039
3073
|
}
|
|
3040
3074
|
return coupons;
|
|
3041
3075
|
}
|
|
3076
|
+
function subscriptionItemsForUpdate(stream, currentProductId, targetProductId, targetSeats) {
|
|
3077
|
+
if (!stream.items?.length) {
|
|
3078
|
+
throw invalidProviderItems("StreamPay returned a subscription without any items.");
|
|
3079
|
+
}
|
|
3080
|
+
const subscriptionCoupons = new Set(subscriptionCouponIds(stream));
|
|
3081
|
+
let matches = 0;
|
|
3082
|
+
const items = stream.items.map((item) => {
|
|
3083
|
+
const productId = subscriptionItemProductId(item);
|
|
3084
|
+
const quantity = item.quantity ?? 1;
|
|
3085
|
+
if (!productId || !Number.isSafeInteger(quantity) || quantity < 1) {
|
|
3086
|
+
throw invalidProviderItems("StreamPay returned an invalid subscription item.");
|
|
3087
|
+
}
|
|
3088
|
+
const isPlanItem = productId === currentProductId;
|
|
3089
|
+
if (isPlanItem) matches += 1;
|
|
3090
|
+
const itemCoupons = couponIdsFromMetadata(item.coupon_calculation_metadata).filter(
|
|
3091
|
+
(couponId) => !subscriptionCoupons.has(couponId)
|
|
3092
|
+
);
|
|
3093
|
+
return {
|
|
3094
|
+
product_id: isPlanItem ? targetProductId : productId,
|
|
3095
|
+
quantity: isPlanItem ? targetSeats : quantity,
|
|
3096
|
+
...itemCoupons.length > 0 ? { coupons: itemCoupons } : {}
|
|
3097
|
+
};
|
|
3098
|
+
});
|
|
3099
|
+
if (matches !== 1) {
|
|
3100
|
+
throw invalidProviderItems(
|
|
3101
|
+
`Expected exactly one plan item for product "${currentProductId}", received ${matches}.`
|
|
3102
|
+
);
|
|
3103
|
+
}
|
|
3104
|
+
return items;
|
|
3105
|
+
}
|
|
3106
|
+
function invalidProviderItems(message) {
|
|
3107
|
+
return new api.APIError("INTERNAL_SERVER_ERROR", {
|
|
3108
|
+
code: $ERROR_CODES.SUBSCRIPTION_INVALID_STATE.code,
|
|
3109
|
+
message
|
|
3110
|
+
});
|
|
3111
|
+
}
|
|
3042
3112
|
function configuredPlanForSubscription(subscription, plans) {
|
|
3043
|
-
|
|
3113
|
+
const matches = plans.list.filter(
|
|
3044
3114
|
(plan) => subscription.items?.some((item) => subscriptionItemProductId(item) === plan.productId)
|
|
3045
3115
|
);
|
|
3116
|
+
if (matches.length > 1) {
|
|
3117
|
+
throw invalidProviderItems(
|
|
3118
|
+
"StreamPay returned a subscription containing multiple configured plan products."
|
|
3119
|
+
);
|
|
3120
|
+
}
|
|
3121
|
+
return matches[0];
|
|
3046
3122
|
}
|
|
3047
3123
|
var SUBSCRIPTION_INTERVALS = ["WEEK", "MONTH", "QUARTER", "YEAR"];
|
|
3048
3124
|
var PlanSchema = zod.z.object({
|
|
@@ -3065,7 +3141,46 @@ var PlanSchema = zod.z.object({
|
|
|
3065
3141
|
message: "has invalid `trialPeriodDays` \u2014 must be an integer from 1 to 365."
|
|
3066
3142
|
}).optional(),
|
|
3067
3143
|
group: zod.z.string().min(1, "has an empty `group`.").optional(),
|
|
3144
|
+
seatBilling: zod.z.object({
|
|
3145
|
+
default: zod.z.number().safe().int().positive().optional(),
|
|
3146
|
+
minimum: zod.z.number().safe().int().positive().optional(),
|
|
3147
|
+
maximum: zod.z.number().safe().int().positive().optional(),
|
|
3148
|
+
customerEditable: zod.z.boolean().optional()
|
|
3149
|
+
}).optional(),
|
|
3068
3150
|
limits: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
|
|
3151
|
+
}).superRefine((plan, ctx) => {
|
|
3152
|
+
const seats = plan.seatBilling;
|
|
3153
|
+
if (!seats) return;
|
|
3154
|
+
const minimum = seats.minimum ?? 1;
|
|
3155
|
+
const defaultSeats = seats.default ?? minimum;
|
|
3156
|
+
if (seats.maximum !== void 0 && seats.maximum < minimum) {
|
|
3157
|
+
ctx.addIssue({
|
|
3158
|
+
code: "custom",
|
|
3159
|
+
path: ["seatBilling", "maximum"],
|
|
3160
|
+
message: "must be greater than or equal to `minimum`."
|
|
3161
|
+
});
|
|
3162
|
+
}
|
|
3163
|
+
if (defaultSeats < minimum || seats.maximum !== void 0 && defaultSeats > seats.maximum) {
|
|
3164
|
+
ctx.addIssue({
|
|
3165
|
+
code: "custom",
|
|
3166
|
+
path: ["seatBilling", "default"],
|
|
3167
|
+
message: "must be within the configured minimum and maximum."
|
|
3168
|
+
});
|
|
3169
|
+
}
|
|
3170
|
+
if (seats.customerEditable && (seats.minimum === void 0 || seats.maximum === void 0)) {
|
|
3171
|
+
ctx.addIssue({
|
|
3172
|
+
code: "custom",
|
|
3173
|
+
path: ["seatBilling", "customerEditable"],
|
|
3174
|
+
message: "requires explicit `minimum` and `maximum` bounds for hosted checkout editing."
|
|
3175
|
+
});
|
|
3176
|
+
}
|
|
3177
|
+
if (seats.maximum !== void 0 && !Number.isSafeInteger(plan.priceInSmallestUnit * seats.maximum)) {
|
|
3178
|
+
ctx.addIssue({
|
|
3179
|
+
code: "custom",
|
|
3180
|
+
path: ["seatBilling", "maximum"],
|
|
3181
|
+
message: "produces an unsafe total with `priceInSmallestUnit`."
|
|
3182
|
+
});
|
|
3183
|
+
}
|
|
3069
3184
|
});
|
|
3070
3185
|
var PlansSchema = zod.z.array(PlanSchema).nonempty("`plans` resolved to an empty list. Provide at least one plan.").superRefine((plans, ctx) => {
|
|
3071
3186
|
const seenNames = /* @__PURE__ */ new Set();
|
|
@@ -3169,6 +3284,15 @@ var CheckLimitQuery = zod.z.object({
|
|
|
3169
3284
|
count: zod.z.coerce.number().int().min(0),
|
|
3170
3285
|
group: zod.z.string().min(1).optional()
|
|
3171
3286
|
});
|
|
3287
|
+
function presentSubscription(row, plans) {
|
|
3288
|
+
return {
|
|
3289
|
+
...row,
|
|
3290
|
+
// The column is nullable during safe rolling migrations. Keep the public
|
|
3291
|
+
// contract stable while applications backfill legacy rows to one seat.
|
|
3292
|
+
seats: row.seats ?? 1,
|
|
3293
|
+
plan: plans.byName.get(row.plan) ?? null
|
|
3294
|
+
};
|
|
3295
|
+
}
|
|
3172
3296
|
async function resolveLiveSubscription(ctx, query, subsOptions, accessStatuses) {
|
|
3173
3297
|
const user = requireUser(ctx);
|
|
3174
3298
|
const reference = resolveReference(user, query);
|
|
@@ -3219,12 +3343,7 @@ function buildSubscriptionReadEndpoints(subsOptions, plansRef) {
|
|
|
3219
3343
|
]
|
|
3220
3344
|
});
|
|
3221
3345
|
const plans = await plansRef();
|
|
3222
|
-
return ctx.json(
|
|
3223
|
-
rows.map((row) => ({
|
|
3224
|
-
...row,
|
|
3225
|
-
plan: plans.byName.get(row.plan) ?? null
|
|
3226
|
-
}))
|
|
3227
|
-
);
|
|
3346
|
+
return ctx.json(rows.map((row) => presentSubscription(row, plans)));
|
|
3228
3347
|
}
|
|
3229
3348
|
),
|
|
3230
3349
|
currentSubscription: api.createAuthEndpoint(
|
|
@@ -3243,7 +3362,7 @@ function buildSubscriptionReadEndpoints(subsOptions, plansRef) {
|
|
|
3243
3362
|
);
|
|
3244
3363
|
if (!live) return ctx.json(null);
|
|
3245
3364
|
const plans = await plansRef();
|
|
3246
|
-
return ctx.json(
|
|
3365
|
+
return ctx.json(presentSubscription(live, plans));
|
|
3247
3366
|
}
|
|
3248
3367
|
),
|
|
3249
3368
|
hasSubscriptionFeature: api.createAuthEndpoint(
|
|
@@ -3278,16 +3397,63 @@ function buildSubscriptionReadEndpoints(subsOptions, plansRef) {
|
|
|
3278
3397
|
)
|
|
3279
3398
|
};
|
|
3280
3399
|
}
|
|
3400
|
+
function resolveSeatCount(plan, requested) {
|
|
3401
|
+
const seats = requested ?? plan.seatBilling?.default ?? plan.seatBilling?.minimum ?? 1;
|
|
3402
|
+
const minimum = plan.seatBilling?.minimum ?? 1;
|
|
3403
|
+
const maximum = plan.seatBilling?.maximum;
|
|
3404
|
+
if (!Number.isSafeInteger(seats) || seats < minimum || maximum !== void 0 && seats > maximum) {
|
|
3405
|
+
const range = maximum === void 0 ? `${minimum} or more` : `${minimum}\u2013${maximum}`;
|
|
3406
|
+
throw new api.APIError("BAD_REQUEST", {
|
|
3407
|
+
code: $ERROR_CODES.SUBSCRIPTION_SEAT_COUNT_INVALID.code,
|
|
3408
|
+
message: `Plan "${plan.name}" requires a safe-integer seat count in the range ${range}.`
|
|
3409
|
+
});
|
|
3410
|
+
}
|
|
3411
|
+
return seats;
|
|
3412
|
+
}
|
|
3413
|
+
function quotedSeatTotal(plan, seats) {
|
|
3414
|
+
const total = plan.priceInSmallestUnit * seats;
|
|
3415
|
+
if (!Number.isSafeInteger(total)) {
|
|
3416
|
+
throw new api.APIError("BAD_REQUEST", {
|
|
3417
|
+
code: $ERROR_CODES.SUBSCRIPTION_SEAT_COUNT_INVALID.code,
|
|
3418
|
+
message: `The quoted total for ${seats} seats exceeds JavaScript's safe-integer range.`
|
|
3419
|
+
});
|
|
3420
|
+
}
|
|
3421
|
+
return total;
|
|
3422
|
+
}
|
|
3423
|
+
function buildSeatCheckoutItem(plan, seats) {
|
|
3424
|
+
if (!plan.seatBilling?.customerEditable) {
|
|
3425
|
+
return {
|
|
3426
|
+
product_id: plan.productId,
|
|
3427
|
+
quantity: seats,
|
|
3428
|
+
allow_custom_quantity: false
|
|
3429
|
+
};
|
|
3430
|
+
}
|
|
3431
|
+
const minimum = plan.seatBilling.minimum;
|
|
3432
|
+
const maximum = plan.seatBilling.maximum;
|
|
3433
|
+
if (minimum === void 0 || maximum === void 0) {
|
|
3434
|
+
throw new TypeError(
|
|
3435
|
+
`Plan "${plan.name}" must configure seatBilling.minimum and seatBilling.maximum when customerEditable is true.`
|
|
3436
|
+
);
|
|
3437
|
+
}
|
|
3438
|
+
return {
|
|
3439
|
+
product_id: plan.productId,
|
|
3440
|
+
quantity: seats,
|
|
3441
|
+
allow_custom_quantity: true,
|
|
3442
|
+
min_quantity: minimum,
|
|
3443
|
+
max_quantity: maximum
|
|
3444
|
+
};
|
|
3445
|
+
}
|
|
3281
3446
|
|
|
3282
3447
|
// src/plugins/subscriptions/endpoints.ts
|
|
3283
3448
|
var SUBSCRIPTION_MODEL6 = "subscription";
|
|
3284
3449
|
var UpgradeBody = zod.z.object({
|
|
3285
3450
|
plan: zod.z.string().min(1),
|
|
3451
|
+
seats: zod.z.number().int().positive().safe().optional(),
|
|
3286
3452
|
referenceId: zod.z.string().min(1).optional(),
|
|
3287
3453
|
referenceType: zod.z.enum(["user", "organization", "custom"]).optional(),
|
|
3288
3454
|
successUrl: zod.z.string().url().optional(),
|
|
3289
3455
|
failureUrl: zod.z.string().url().optional()
|
|
3290
|
-
});
|
|
3456
|
+
}).strict();
|
|
3291
3457
|
var SuccessQuery = zod.z.object({
|
|
3292
3458
|
subscriptionId: zod.z.string().min(1)
|
|
3293
3459
|
});
|
|
@@ -3298,7 +3464,12 @@ var CancelBody = zod.z.object({
|
|
|
3298
3464
|
});
|
|
3299
3465
|
var ChangePlanBody = zod.z.object({
|
|
3300
3466
|
subscriptionId: zod.z.string().min(1),
|
|
3301
|
-
plan: zod.z.string().min(1)
|
|
3467
|
+
plan: zod.z.string().min(1),
|
|
3468
|
+
seats: zod.z.number().int().positive().safe().optional()
|
|
3469
|
+
}).strict();
|
|
3470
|
+
var UpdateSeatsBody = zod.z.object({
|
|
3471
|
+
subscriptionId: zod.z.string().min(1),
|
|
3472
|
+
seats: zod.z.number().int().positive().safe()
|
|
3302
3473
|
}).strict();
|
|
3303
3474
|
var FreezeBody = zod.z.object({
|
|
3304
3475
|
subscriptionId: zod.z.string().min(1),
|
|
@@ -3328,11 +3499,72 @@ function isCheckoutSubscriptionCandidate(subscription, row, productId) {
|
|
|
3328
3499
|
return true;
|
|
3329
3500
|
}
|
|
3330
3501
|
if (!subscription.created_at || !(row.createdAt instanceof Date)) return false;
|
|
3331
|
-
const createdAt =
|
|
3332
|
-
return
|
|
3502
|
+
const createdAt = parseDate(subscription.created_at)?.getTime();
|
|
3503
|
+
return createdAt !== void 0 && createdAt >= row.createdAt.getTime() - SUCCESS_RECONCILIATION_CLOCK_SKEW_MS;
|
|
3333
3504
|
}
|
|
3334
3505
|
function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
3335
3506
|
const client = options.client;
|
|
3507
|
+
const cancelPendingChangeEndpoint = (path) => api.createAuthEndpoint(
|
|
3508
|
+
path,
|
|
3509
|
+
{
|
|
3510
|
+
method: "POST",
|
|
3511
|
+
body: UnfreezeBody,
|
|
3512
|
+
use: [api.sessionMiddleware]
|
|
3513
|
+
},
|
|
3514
|
+
async (ctx) => {
|
|
3515
|
+
const { adapter, row } = await requireConfirmedOwnedSubscription(
|
|
3516
|
+
ctx,
|
|
3517
|
+
ctx.body.subscriptionId,
|
|
3518
|
+
subsOptions,
|
|
3519
|
+
"cancel-plan-change"
|
|
3520
|
+
);
|
|
3521
|
+
try {
|
|
3522
|
+
const plans = await plansRef();
|
|
3523
|
+
const current = await client.getSubscription(row.streampaySubscriptionId);
|
|
3524
|
+
if (!current.pending_change) {
|
|
3525
|
+
await adapter.update({
|
|
3526
|
+
model: SUBSCRIPTION_MODEL6,
|
|
3527
|
+
update: {
|
|
3528
|
+
...projectSubscriptionAgainstExisting(row, current),
|
|
3529
|
+
...projectPlanFields(current, plans),
|
|
3530
|
+
pendingPlan: null,
|
|
3531
|
+
pendingProductId: null,
|
|
3532
|
+
pendingPlanEffectiveAt: null,
|
|
3533
|
+
pendingSeats: null,
|
|
3534
|
+
pendingSeatsEffectiveAt: null
|
|
3535
|
+
},
|
|
3536
|
+
where: [{ field: "id", value: row.id }]
|
|
3537
|
+
});
|
|
3538
|
+
return ctx.json({ canceled: true, reused: true, subscription: current });
|
|
3539
|
+
}
|
|
3540
|
+
await client.deletePendingSubscriptionChange(row.streampaySubscriptionId);
|
|
3541
|
+
const stream = await client.getSubscription(row.streampaySubscriptionId);
|
|
3542
|
+
await adapter.update({
|
|
3543
|
+
model: SUBSCRIPTION_MODEL6,
|
|
3544
|
+
update: {
|
|
3545
|
+
...projectSubscriptionAgainstExisting(row, stream),
|
|
3546
|
+
...projectPlanFields(stream, plans),
|
|
3547
|
+
pendingPlan: null,
|
|
3548
|
+
pendingProductId: null,
|
|
3549
|
+
pendingPlanEffectiveAt: null,
|
|
3550
|
+
pendingSeats: null,
|
|
3551
|
+
pendingSeatsEffectiveAt: null
|
|
3552
|
+
},
|
|
3553
|
+
where: [{ field: "id", value: row.id }]
|
|
3554
|
+
});
|
|
3555
|
+
return ctx.json({ canceled: true, reused: false, subscription: stream });
|
|
3556
|
+
} catch (err) {
|
|
3557
|
+
toAPIError(
|
|
3558
|
+
{
|
|
3559
|
+
logPrefix: `cancel pending subscription change failed for row=${row.id}:`,
|
|
3560
|
+
userMessage: "Canceling the pending subscription change failed."
|
|
3561
|
+
},
|
|
3562
|
+
err,
|
|
3563
|
+
getLogger(ctx)
|
|
3564
|
+
);
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
);
|
|
3336
3568
|
return {
|
|
3337
3569
|
...buildSubscriptionReadEndpoints(subsOptions, plansRef),
|
|
3338
3570
|
upgradeSubscription: api.createAuthEndpoint(
|
|
@@ -3355,6 +3587,8 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3355
3587
|
message: `Plan "${ctx.body.plan}" is not configured.`
|
|
3356
3588
|
});
|
|
3357
3589
|
}
|
|
3590
|
+
const seats = resolveSeatCount(plan, ctx.body.seats);
|
|
3591
|
+
const quotedTotal = quotedSeatTotal(plan, seats);
|
|
3358
3592
|
const adapter = getAdapter2(ctx);
|
|
3359
3593
|
const existingActive = await adapter.findMany({
|
|
3360
3594
|
model: SUBSCRIPTION_MODEL6,
|
|
@@ -3392,6 +3626,7 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3392
3626
|
candidates: existingActive,
|
|
3393
3627
|
activeSlotKey,
|
|
3394
3628
|
planName: plan.name,
|
|
3629
|
+
seats,
|
|
3395
3630
|
now: Date.now(),
|
|
3396
3631
|
log: getLogger(ctx),
|
|
3397
3632
|
createReservation: async () => {
|
|
@@ -3411,13 +3646,14 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3411
3646
|
planVersion: plan.version ?? null,
|
|
3412
3647
|
productId: plan.productId,
|
|
3413
3648
|
group: plan.group ?? null,
|
|
3649
|
+
seats,
|
|
3414
3650
|
streampayConsumerId: consumerId2,
|
|
3415
3651
|
streampayPaymentLinkId: null,
|
|
3416
3652
|
status: "incomplete",
|
|
3417
3653
|
providerStatus: null,
|
|
3418
3654
|
billingStatus: "current",
|
|
3419
|
-
amountInSmallestUnit:
|
|
3420
|
-
originalAmountInSmallestUnit:
|
|
3655
|
+
amountInSmallestUnit: quotedTotal,
|
|
3656
|
+
originalAmountInSmallestUnit: quotedTotal,
|
|
3421
3657
|
currency: plan.currency ?? "SAR",
|
|
3422
3658
|
billingInterval: plan.billingInterval,
|
|
3423
3659
|
billingIntervalCount: plan.billingIntervalCount ?? 1,
|
|
@@ -3433,6 +3669,8 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3433
3669
|
pendingPlan: null,
|
|
3434
3670
|
pendingProductId: null,
|
|
3435
3671
|
pendingPlanEffectiveAt: null,
|
|
3672
|
+
pendingSeats: null,
|
|
3673
|
+
pendingSeatsEffectiveAt: null,
|
|
3436
3674
|
endedAt: null,
|
|
3437
3675
|
frozenAt: null,
|
|
3438
3676
|
freezeEndAt: null,
|
|
@@ -3451,14 +3689,15 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3451
3689
|
redirect: true,
|
|
3452
3690
|
reused: true,
|
|
3453
3691
|
status: reservation.row.status,
|
|
3454
|
-
plan: reservation.row.plan
|
|
3692
|
+
plan: reservation.row.plan,
|
|
3693
|
+
seats: reservation.row.seats ?? 1
|
|
3455
3694
|
});
|
|
3456
3695
|
}
|
|
3457
3696
|
const { row, consumerId } = reservation;
|
|
3458
3697
|
const linkInput = {
|
|
3459
3698
|
name: `Subscription \u2014 ${plan.name}`,
|
|
3460
3699
|
currency: plan.currency ?? "SAR",
|
|
3461
|
-
items: [
|
|
3700
|
+
items: [buildSeatCheckoutItem(plan, seats)],
|
|
3462
3701
|
max_number_of_payments: 1,
|
|
3463
3702
|
organization_consumer_id: consumerId,
|
|
3464
3703
|
custom_metadata: {
|
|
@@ -3551,7 +3790,8 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3551
3790
|
redirect: true,
|
|
3552
3791
|
reused: false,
|
|
3553
3792
|
status: "incomplete",
|
|
3554
|
-
plan: plan.name
|
|
3793
|
+
plan: plan.name,
|
|
3794
|
+
seats
|
|
3555
3795
|
});
|
|
3556
3796
|
}
|
|
3557
3797
|
),
|
|
@@ -3622,6 +3862,7 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3622
3862
|
model: SUBSCRIPTION_MODEL6,
|
|
3623
3863
|
update: {
|
|
3624
3864
|
...projectSubscriptionFields(match),
|
|
3865
|
+
...projectPlanFields(match, plans),
|
|
3625
3866
|
planVersion: plan.version ?? null,
|
|
3626
3867
|
productId: plan.productId,
|
|
3627
3868
|
billingStatus: "current",
|
|
@@ -3733,8 +3974,14 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3733
3974
|
}
|
|
3734
3975
|
try {
|
|
3735
3976
|
const stream = await client.getSubscription(row.streampaySubscriptionId);
|
|
3736
|
-
const currentPlan = configuredPlanForSubscription(stream, plans);
|
|
3737
|
-
|
|
3977
|
+
const currentPlan = configuredPlanForSubscription(stream, plans) ?? plans.byName.get(row.plan);
|
|
3978
|
+
if (!currentPlan) {
|
|
3979
|
+
throw new api.APIError("BAD_REQUEST", {
|
|
3980
|
+
code: $ERROR_CODES.SUBSCRIPTION_INVALID_STATE.code,
|
|
3981
|
+
message: "The current StreamPay product is not a configured subscription plan."
|
|
3982
|
+
});
|
|
3983
|
+
}
|
|
3984
|
+
const currentGroup = currentPlan.group ?? row.group;
|
|
3738
3985
|
if (currentGroup !== (nextPlan.group ?? null)) {
|
|
3739
3986
|
throw new api.APIError("BAD_REQUEST", {
|
|
3740
3987
|
code: $ERROR_CODES.SUBSCRIPTION_PLAN_GROUP_MISMATCH.code,
|
|
@@ -3747,27 +3994,35 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3747
3994
|
message: "The subscription is scheduled to cancel. Uncancel it before scheduling a plan change."
|
|
3748
3995
|
});
|
|
3749
3996
|
}
|
|
3750
|
-
const
|
|
3751
|
-
(item) => subscriptionItemProductId(item) ===
|
|
3997
|
+
const currentItem = stream.items?.find(
|
|
3998
|
+
(item) => subscriptionItemProductId(item) === currentPlan.productId
|
|
3752
3999
|
);
|
|
3753
|
-
|
|
4000
|
+
const currentSeats = currentItem ? subscriptionItemQuantity(currentItem) ?? 1 : row.seats;
|
|
4001
|
+
const nextSeats = resolveSeatCount(nextPlan, ctx.body.seats ?? currentSeats ?? 1);
|
|
4002
|
+
const currentProductIsRequestedPlan = currentPlan.productId === nextPlan.productId;
|
|
4003
|
+
if (currentProductIsRequestedPlan && currentSeats === nextSeats && !stream.pending_change) {
|
|
3754
4004
|
throw new api.APIError("BAD_REQUEST", {
|
|
3755
4005
|
code: $ERROR_CODES.SUBSCRIPTION_ALREADY_ON_PLAN.code,
|
|
3756
|
-
message: `Already on plan "${nextPlan.name}".`
|
|
4006
|
+
message: `Already on plan "${nextPlan.name}" with ${nextSeats} seats.`
|
|
3757
4007
|
});
|
|
3758
4008
|
}
|
|
3759
4009
|
const pendingTargets = stream.pending_change?.target_items ?? [];
|
|
3760
|
-
const
|
|
4010
|
+
const pendingTarget = pendingTargets.find(
|
|
3761
4011
|
(item) => subscriptionItemProductId(item) === nextPlan.productId
|
|
3762
4012
|
);
|
|
3763
|
-
|
|
4013
|
+
const pendingTargetMatches = Boolean(pendingTarget) && (subscriptionItemQuantity(pendingTarget) ?? 1) === nextSeats;
|
|
4014
|
+
if (pendingTargetMatches) {
|
|
4015
|
+
const effectiveAt2 = stream.pending_change?.effective_at ?? null;
|
|
3764
4016
|
await adapter.update({
|
|
3765
4017
|
model: SUBSCRIPTION_MODEL6,
|
|
3766
4018
|
update: {
|
|
3767
4019
|
...projectSubscriptionAgainstExisting(row, stream),
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
4020
|
+
...projectPlanFields(stream, plans),
|
|
4021
|
+
pendingPlan: currentPlan.productId === nextPlan.productId ? null : nextPlan.name,
|
|
4022
|
+
pendingProductId: currentPlan.productId === nextPlan.productId ? null : nextPlan.productId,
|
|
4023
|
+
pendingPlanEffectiveAt: currentPlan.productId !== nextPlan.productId && effectiveAt2 ? parseDate(effectiveAt2) : null,
|
|
4024
|
+
pendingSeats: nextSeats,
|
|
4025
|
+
pendingSeatsEffectiveAt: parseDate(effectiveAt2)
|
|
3771
4026
|
},
|
|
3772
4027
|
where: [{ field: "id", value: row.id }]
|
|
3773
4028
|
});
|
|
@@ -3775,7 +4030,8 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3775
4030
|
mode: "at_period_end",
|
|
3776
4031
|
subscriptionId: row.id,
|
|
3777
4032
|
plan: nextPlan.name,
|
|
3778
|
-
|
|
4033
|
+
seats: nextSeats,
|
|
4034
|
+
effectiveAt: effectiveAt2,
|
|
3779
4035
|
pendingChange: stream.pending_change,
|
|
3780
4036
|
reused: true
|
|
3781
4037
|
});
|
|
@@ -3787,28 +4043,36 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3787
4043
|
});
|
|
3788
4044
|
}
|
|
3789
4045
|
const patch = {
|
|
3790
|
-
items:
|
|
4046
|
+
items: subscriptionItemsForUpdate(
|
|
4047
|
+
stream,
|
|
4048
|
+
currentPlan.productId,
|
|
4049
|
+
nextPlan.productId,
|
|
4050
|
+
nextSeats
|
|
4051
|
+
),
|
|
3791
4052
|
coupons: subscriptionCouponIds(stream),
|
|
3792
4053
|
recurring_interval: nextPlan.billingInterval,
|
|
3793
4054
|
recurring_interval_count: nextPlan.billingIntervalCount ?? 1
|
|
3794
4055
|
};
|
|
3795
4056
|
const result = await client.updateSubscription(row.streampaySubscriptionId, patch);
|
|
3796
|
-
const
|
|
4057
|
+
const pending = Boolean(result.pending_change);
|
|
4058
|
+
const effectiveAt = result.pending_change?.effective_at ?? null;
|
|
4059
|
+
const planProjection = projectPlanFields(result, plans);
|
|
3797
4060
|
await adapter.update({
|
|
3798
4061
|
model: SUBSCRIPTION_MODEL6,
|
|
3799
4062
|
update: {
|
|
3800
4063
|
...projectSubscriptionAgainstExisting(row, result),
|
|
3801
|
-
|
|
3802
|
-
pendingProductId: nextPlan.productId,
|
|
3803
|
-
pendingPlanEffectiveAt: effectiveAt ? new Date(effectiveAt) : null,
|
|
4064
|
+
...planProjection,
|
|
3804
4065
|
updatedAt: /* @__PURE__ */ new Date()
|
|
3805
4066
|
},
|
|
3806
4067
|
where: [{ field: "id", value: row.id }]
|
|
3807
4068
|
});
|
|
4069
|
+
const providerPlan = pending ? planProjection.pendingPlan ?? planProjection.plan ?? row.plan : planProjection.plan ?? row.plan;
|
|
4070
|
+
const providerSeats = pending ? planProjection.pendingSeats ?? nextSeats : planProjection.seats ?? row.seats ?? nextSeats;
|
|
3808
4071
|
return ctx.json({
|
|
3809
|
-
mode: "at_period_end",
|
|
4072
|
+
mode: pending ? "at_period_end" : "immediate",
|
|
3810
4073
|
subscriptionId: row.id,
|
|
3811
|
-
plan:
|
|
4074
|
+
plan: providerPlan,
|
|
4075
|
+
seats: providerSeats,
|
|
3812
4076
|
effectiveAt,
|
|
3813
4077
|
pendingChange: result.pending_change ?? null,
|
|
3814
4078
|
subscription: result,
|
|
@@ -3826,11 +4090,11 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3826
4090
|
}
|
|
3827
4091
|
}
|
|
3828
4092
|
),
|
|
3829
|
-
|
|
3830
|
-
"/subscription/
|
|
4093
|
+
updateSubscriptionSeats: api.createAuthEndpoint(
|
|
4094
|
+
"/subscription/update-seats",
|
|
3831
4095
|
{
|
|
3832
4096
|
method: "POST",
|
|
3833
|
-
body:
|
|
4097
|
+
body: UpdateSeatsBody,
|
|
3834
4098
|
use: [api.sessionMiddleware]
|
|
3835
4099
|
},
|
|
3836
4100
|
async (ctx) => {
|
|
@@ -3838,27 +4102,107 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3838
4102
|
ctx,
|
|
3839
4103
|
ctx.body.subscriptionId,
|
|
3840
4104
|
subsOptions,
|
|
3841
|
-
"
|
|
4105
|
+
"update-seats"
|
|
3842
4106
|
);
|
|
4107
|
+
const plans = await plansRef();
|
|
3843
4108
|
try {
|
|
3844
|
-
await client.deletePendingSubscriptionChange(row.streampaySubscriptionId);
|
|
3845
4109
|
const stream = await client.getSubscription(row.streampaySubscriptionId);
|
|
4110
|
+
const plan = configuredPlanForSubscription(stream, plans) ?? plans.byName.get(row.plan);
|
|
4111
|
+
if (!plan) {
|
|
4112
|
+
throw new api.APIError("BAD_REQUEST", {
|
|
4113
|
+
code: $ERROR_CODES.SUBSCRIPTION_INVALID_STATE.code,
|
|
4114
|
+
message: "The current StreamPay product is not a configured subscription plan."
|
|
4115
|
+
});
|
|
4116
|
+
}
|
|
4117
|
+
if (stream.cancel_at_period_end) {
|
|
4118
|
+
throw new api.APIError("CONFLICT", {
|
|
4119
|
+
code: $ERROR_CODES.SUBSCRIPTION_ALREADY_SCHEDULED_CANCEL.code,
|
|
4120
|
+
message: "Uncancel the subscription before scheduling a seat change."
|
|
4121
|
+
});
|
|
4122
|
+
}
|
|
4123
|
+
const seats = resolveSeatCount(plan, ctx.body.seats);
|
|
4124
|
+
const currentItem = stream.items?.find(
|
|
4125
|
+
(item) => subscriptionItemProductId(item) === plan.productId
|
|
4126
|
+
);
|
|
4127
|
+
const currentSeats = currentItem ? subscriptionItemQuantity(currentItem) ?? 1 : row.seats;
|
|
4128
|
+
const pendingTarget = stream.pending_change?.target_items?.find(
|
|
4129
|
+
(item) => subscriptionItemProductId(item) === plan.productId
|
|
4130
|
+
);
|
|
4131
|
+
if (stream.pending_change) {
|
|
4132
|
+
if (pendingTarget && (subscriptionItemQuantity(pendingTarget) ?? 1) === seats) {
|
|
4133
|
+
const effectiveAt2 = stream.pending_change.effective_at ?? null;
|
|
4134
|
+
await adapter.update({
|
|
4135
|
+
model: SUBSCRIPTION_MODEL6,
|
|
4136
|
+
update: {
|
|
4137
|
+
...projectSubscriptionAgainstExisting(row, stream),
|
|
4138
|
+
...projectPlanFields(stream, plans)
|
|
4139
|
+
},
|
|
4140
|
+
where: [{ field: "id", value: row.id }]
|
|
4141
|
+
});
|
|
4142
|
+
return ctx.json({
|
|
4143
|
+
mode: "at_period_end",
|
|
4144
|
+
subscriptionId: row.id,
|
|
4145
|
+
seats,
|
|
4146
|
+
effectiveAt: effectiveAt2,
|
|
4147
|
+
pendingChange: stream.pending_change,
|
|
4148
|
+
reused: true
|
|
4149
|
+
});
|
|
4150
|
+
}
|
|
4151
|
+
throw new api.APIError("CONFLICT", {
|
|
4152
|
+
code: $ERROR_CODES.SUBSCRIPTION_SEAT_CHANGE_ALREADY_SCHEDULED.code,
|
|
4153
|
+
message: "A different subscription change is pending. Cancel it before scheduling this seat count."
|
|
4154
|
+
});
|
|
4155
|
+
}
|
|
4156
|
+
if (currentSeats === seats) {
|
|
4157
|
+
await adapter.update({
|
|
4158
|
+
model: SUBSCRIPTION_MODEL6,
|
|
4159
|
+
update: {
|
|
4160
|
+
...projectSubscriptionAgainstExisting(row, stream),
|
|
4161
|
+
...projectPlanFields(stream, plans)
|
|
4162
|
+
},
|
|
4163
|
+
where: [{ field: "id", value: row.id }]
|
|
4164
|
+
});
|
|
4165
|
+
return ctx.json({
|
|
4166
|
+
mode: "current",
|
|
4167
|
+
subscriptionId: row.id,
|
|
4168
|
+
seats,
|
|
4169
|
+
effectiveAt: null,
|
|
4170
|
+
pendingChange: null,
|
|
4171
|
+
reused: true
|
|
4172
|
+
});
|
|
4173
|
+
}
|
|
4174
|
+
const patch = {
|
|
4175
|
+
items: subscriptionItemsForUpdate(stream, plan.productId, plan.productId, seats),
|
|
4176
|
+
coupons: subscriptionCouponIds(stream)
|
|
4177
|
+
};
|
|
4178
|
+
const result = await client.updateSubscription(row.streampaySubscriptionId, patch);
|
|
4179
|
+
const pending = Boolean(result.pending_change);
|
|
4180
|
+
const effectiveAt = result.pending_change?.effective_at ?? null;
|
|
4181
|
+
const planProjection = projectPlanFields(result, plans);
|
|
3846
4182
|
await adapter.update({
|
|
3847
4183
|
model: SUBSCRIPTION_MODEL6,
|
|
3848
4184
|
update: {
|
|
3849
|
-
...projectSubscriptionAgainstExisting(row,
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
pendingPlanEffectiveAt: null
|
|
4185
|
+
...projectSubscriptionAgainstExisting(row, result),
|
|
4186
|
+
...planProjection,
|
|
4187
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
3853
4188
|
},
|
|
3854
4189
|
where: [{ field: "id", value: row.id }]
|
|
3855
4190
|
});
|
|
3856
|
-
|
|
4191
|
+
const providerSeats = pending ? planProjection.pendingSeats ?? seats : planProjection.seats ?? row.seats ?? seats;
|
|
4192
|
+
return ctx.json({
|
|
4193
|
+
mode: pending ? "at_period_end" : "immediate",
|
|
4194
|
+
subscriptionId: row.id,
|
|
4195
|
+
seats: providerSeats,
|
|
4196
|
+
effectiveAt,
|
|
4197
|
+
pendingChange: result.pending_change ?? null,
|
|
4198
|
+
subscription: result,
|
|
4199
|
+
reused: false
|
|
4200
|
+
});
|
|
3857
4201
|
} catch (err) {
|
|
3858
4202
|
toAPIError(
|
|
3859
4203
|
{
|
|
3860
|
-
logPrefix: `
|
|
3861
|
-
userMessage: "
|
|
4204
|
+
logPrefix: `update-seats failed for row=${row.id}:`,
|
|
4205
|
+
userMessage: "Seat change scheduling failed."
|
|
3862
4206
|
},
|
|
3863
4207
|
err,
|
|
3864
4208
|
getLogger(ctx)
|
|
@@ -3866,6 +4210,10 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3866
4210
|
}
|
|
3867
4211
|
}
|
|
3868
4212
|
),
|
|
4213
|
+
cancelSubscriptionPlanChange: cancelPendingChangeEndpoint("/subscription/change-plan/cancel"),
|
|
4214
|
+
cancelSubscriptionPendingChange: cancelPendingChangeEndpoint(
|
|
4215
|
+
"/subscription/pending-change/cancel"
|
|
4216
|
+
),
|
|
3869
4217
|
uncancelSubscription: api.createAuthEndpoint(
|
|
3870
4218
|
"/subscription/uncancel",
|
|
3871
4219
|
{
|
|
@@ -3965,9 +4313,9 @@ function buildSubscriptionEndpoints(options, subsOptions, plansRef) {
|
|
|
3965
4313
|
const freezes = await client.listSubscriptionFreezes(row.streampaySubscriptionId);
|
|
3966
4314
|
const active = freezes.data?.find((freeze) => {
|
|
3967
4315
|
if (!freeze.id || !freeze.freeze_start_datetime) return false;
|
|
3968
|
-
const start =
|
|
3969
|
-
const end = freeze.freeze_end_datetime ?
|
|
3970
|
-
return start <= nowMs && nowMs <= end;
|
|
4316
|
+
const start = parseDate(freeze.freeze_start_datetime)?.getTime();
|
|
4317
|
+
const end = freeze.freeze_end_datetime ? parseDate(freeze.freeze_end_datetime)?.getTime() : Number.POSITIVE_INFINITY;
|
|
4318
|
+
return start !== void 0 && end !== void 0 && start <= nowMs && nowMs <= end;
|
|
3971
4319
|
});
|
|
3972
4320
|
if (!active?.id || !active.freeze_start_datetime) {
|
|
3973
4321
|
throw new api.APIError("BAD_REQUEST", {
|
|
@@ -4107,6 +4455,11 @@ var subscriptionTable = {
|
|
|
4107
4455
|
required: false,
|
|
4108
4456
|
index: true
|
|
4109
4457
|
},
|
|
4458
|
+
seats: {
|
|
4459
|
+
type: "number",
|
|
4460
|
+
required: false,
|
|
4461
|
+
defaultValue: 1
|
|
4462
|
+
},
|
|
4110
4463
|
amountInSmallestUnit: {
|
|
4111
4464
|
type: "number",
|
|
4112
4465
|
required: false
|
|
@@ -4195,6 +4548,14 @@ var subscriptionTable = {
|
|
|
4195
4548
|
type: "date",
|
|
4196
4549
|
required: false
|
|
4197
4550
|
},
|
|
4551
|
+
pendingSeats: {
|
|
4552
|
+
type: "number",
|
|
4553
|
+
required: false
|
|
4554
|
+
},
|
|
4555
|
+
pendingSeatsEffectiveAt: {
|
|
4556
|
+
type: "date",
|
|
4557
|
+
required: false
|
|
4558
|
+
},
|
|
4198
4559
|
endedAt: {
|
|
4199
4560
|
type: "date",
|
|
4200
4561
|
required: false
|
|
@@ -4793,7 +5154,7 @@ var onUserDelete = (options) => async (user, context) => {
|
|
|
4793
5154
|
|
|
4794
5155
|
// package.json
|
|
4795
5156
|
var package_default = {
|
|
4796
|
-
version: "2.
|
|
5157
|
+
version: "2.2.0"};
|
|
4797
5158
|
|
|
4798
5159
|
// src/version.ts
|
|
4799
5160
|
var version = package_default.version;
|