@wepublish/api
Version:
API core for we.publish.
73 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePublicSubscription = void 0;
const tslib_1 = require("tslib");
const error_1 = require("../../error");
const subscription_private_mutation_1 = require("./subscription.private-mutation");
const updatePublicSubscription = (id, input, authenticateUser, memberContext, activeMemberPlansByID, activePaymentMethodsByID, subscriptionClient, paymentProviders) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a;
const { user } = authenticateUser();
const subscription = yield subscriptionClient.findUnique({
where: { id },
include: {
properties: true,
deactivation: true
}
});
if (!subscription)
throw new error_1.NotFound('subscription', id);
const { memberPlanID, paymentPeriodicity, monthlyAmount, autoRenew, paymentMethodID } = input;
const memberPlan = yield activeMemberPlansByID.load(memberPlanID);
if (!memberPlan)
throw new error_1.NotFound('MemberPlan', memberPlanID);
const paymentMethod = yield activePaymentMethodsByID.load(paymentMethodID);
if (!paymentMethod)
throw new error_1.NotFound('PaymentMethod', paymentMethodID);
if (!monthlyAmount || monthlyAmount < memberPlan.amountPerMonthMin)
throw new error_1.MonthlyAmountNotEnough();
if (subscription.deactivation) {
throw new Error('You are not allowed to change a deactivated subscription!');
}
if (!memberPlan.availablePaymentMethods.some(apm => {
if (apm.forceAutoRenewal && !autoRenew) {
return false;
}
return (apm.paymentPeriodicities.includes(paymentPeriodicity) &&
apm.paymentMethodIDs.includes(paymentMethodID));
})) {
throw new error_1.PaymentConfigurationNotAllowed();
}
// handle remote managed subscriptions (Payrexx Subscription)
const { paymentProviderID } = yield memberContext.getPaymentMethodByIDOrSlug(memberContext.loaders, undefined, subscription.paymentMethodID);
const paymentProvider = paymentProviders.find(paymentProvider => paymentProvider.id === paymentProviderID);
if (paymentProvider.remoteManagedSubscription) {
yield (0, subscription_private_mutation_1.handleRemoteManagedSubscription)({
paymentProvider,
originalSubscription: subscription,
input: input
});
}
const updateSubscription = yield subscriptionClient.update({
where: { id },
data: {
userID: (_a = subscription.userID) !== null && _a !== void 0 ? _a : user.id,
memberPlanID,
paymentPeriodicity,
monthlyAmount,
autoRenew,
paymentMethodID
},
include: {
deactivation: true,
periods: true,
properties: true
}
});
if (!updateSubscription)
throw new Error('Error during updateSubscription');
return yield memberContext.handleSubscriptionChange({
subscription: updateSubscription
});
});
exports.updatePublicSubscription = updatePublicSubscription;
//# sourceMappingURL=subscription.public-mutation.js.map