@bunnyapp/api-client
Version:
Node.js client for Bunny CRM
112 lines • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = subscriptionCreate;
const query = `mutation subscriptionCreate ($attributes: SubscriptionAttributes!) {
subscriptionCreate (attributes: $attributes) {
subscription {
id
account {
id
name
contacts {
id
firstName
lastName
}
}
trialStartDate
trialEndDate
startDate
endDate
state
plan {
code
name
}
priceList {
code
name
}
tenant {
id
code
name
account {
id
name
billingDay
}
}
}
errors
}
}`;
/**
* Create a subscription
* @param {string} priceListCode The unique code that represents the price list
* @param {SubscriptionOptions} [options={}] Additional options for subscription creation
* @returns {Promise<NonNullable<NonNullable<Mutation['subscriptionCreate']>['subscription']>>} The created subscription
*/
async function subscriptionCreate(priceListCode, options = {}) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
const variables = {
attributes: {
priceListCode,
trial: options.trial || false,
evergreen: (_a = options.evergreen) !== null && _a !== void 0 ? _a : true,
},
};
if (options.accountId) {
variables.attributes.accountId = options.accountId;
}
else {
const billingContact = {
firstName: (_b = options.firstName) === null || _b === void 0 ? void 0 : _b.toString(),
lastName: (_c = options.lastName) === null || _c === void 0 ? void 0 : _c.toString(),
email: (_d = options.email) === null || _d === void 0 ? void 0 : _d.toString(),
phone: (_e = options.phone) === null || _e === void 0 ? void 0 : _e.toString(),
mobile: (_f = options.mobile) === null || _f === void 0 ? void 0 : _f.toString(),
salutation: (_g = options.salutation) === null || _g === void 0 ? void 0 : _g.toString(),
title: (_h = options.title) === null || _h === void 0 ? void 0 : _h.toString(),
mailingStreet: (_j = options.mailingStreet) === null || _j === void 0 ? void 0 : _j.toString(),
mailingCity: (_k = options.mailingCity) === null || _k === void 0 ? void 0 : _k.toString(),
mailingZip: (_l = options.mailingZip) === null || _l === void 0 ? void 0 : _l.toString(),
mailingState: (_m = options.mailingState) === null || _m === void 0 ? void 0 : _m.toString(),
mailingCountry: (_o = options.mailingCountry) === null || _o === void 0 ? void 0 : _o.toString(),
};
// Only include non-empty billing contact
const filteredBillingContact = Object.fromEntries(Object.entries(billingContact).filter(([_, value]) => value !== undefined));
variables.attributes.account = {
name: (_p = options.accountName) === null || _p === void 0 ? void 0 : _p.toString(),
ownerUserId: (_q = options.ownerUserId) === null || _q === void 0 ? void 0 : _q.toString(),
phone: (_r = options.phone) === null || _r === void 0 ? void 0 : _r.toString(),
fax: (_s = options.fax) === null || _s === void 0 ? void 0 : _s.toString(),
website: (_t = options.website) === null || _t === void 0 ? void 0 : _t.toString(),
billingStreet: (_u = options.billingStreet) === null || _u === void 0 ? void 0 : _u.toString(),
billingCity: (_v = options.billingCity) === null || _v === void 0 ? void 0 : _v.toString(),
billingZip: (_w = options.billingZip) === null || _w === void 0 ? void 0 : _w.toString(),
billingState: (_x = options.billingState) === null || _x === void 0 ? void 0 : _x.toString(),
billingCountry: (_y = options.billingCountry) === null || _y === void 0 ? void 0 : _y.toString(),
...(Object.keys(filteredBillingContact).length > 0 && { billingContact: filteredBillingContact }),
};
}
if (options.tenantCode) {
variables.attributes.tenant = {
code: options.tenantCode.toString(),
name: (_z = options.tenantName) === null || _z === void 0 ? void 0 : _z.toString(),
};
}
const res = await this.query(query, variables);
const subscriptionCreate = (_0 = res === null || res === void 0 ? void 0 : res.data) === null || _0 === void 0 ? void 0 : _0.subscriptionCreate;
if (res === null || res === void 0 ? void 0 : res.errors) {
throw new Error(Array.isArray(res.errors) ? res.errors.map(e => e.message).join() : res.errors);
}
if (subscriptionCreate === null || subscriptionCreate === void 0 ? void 0 : subscriptionCreate.errors) {
throw new Error(Array.isArray(subscriptionCreate.errors) ? subscriptionCreate.errors.join() : subscriptionCreate.errors);
}
if (!(subscriptionCreate === null || subscriptionCreate === void 0 ? void 0 : subscriptionCreate.subscription)) {
throw new Error('No subscription returned from subscription creation');
}
return subscriptionCreate.subscription;
}
//# sourceMappingURL=subscription-create.js.map