@arc-publishing/sdk-sales
Version:
JS Sales SDK for working with Arc Subs Sales API
45 lines • 2.41 kB
JavaScript
import { __assign, __awaiter, __generator } from "tslib";
import JSONResponseHandler from '@arc-publishing/sdk-subs-core/lib/utils/JSONResponseHandler';
import { headers, logPrefix } from './constants';
import { updateSubscription } from './utils';
import Sales from './sales';
import { isAddress, isSubscription } from './subscription';
export default function updateBillingAddress(billingAddress, subscriptionID) {
return __awaiter(this, void 0, void 0, function () {
var isLoggedIn, body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, Sales._Identity.isLoggedIn()];
case 1:
isLoggedIn = _a.sent();
if (!isLoggedIn) {
throw new Error("".concat(logPrefix, " Missing or invalid jwt"));
}
body = JSON.stringify({ billingAddress: billingAddress, subscriptionID: subscriptionID });
return [2, Sales.getSubscriptionDetails(subscriptionID).then(function (sub) {
if (isSubscription(sub)) {
return fetch("".concat(Sales.apiOrigin, "/sales/public/v1/subscription/address"), {
method: 'PUT',
headers: __assign({ Authorization: "Bearer ".concat(Sales._Identity.userIdentity.accessToken) }, headers),
body: body
})
.then(JSONResponseHandler)
.then(function (json) {
if (isAddress(json)) {
Sales.subscriptions = updateSubscription(Sales.subscriptions, __assign(__assign({}, sub), { subscriptionID: subscriptionID, billingAddress: json }));
return json;
}
else {
throw json;
}
});
}
else {
throw sub;
}
})];
}
});
});
}
//# sourceMappingURL=updateBillingAddress.js.map