tickethead-sdk
Version:
SDK for the Tickethead API
239 lines • 8.96 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderService = void 0;
const query_string_1 = __importDefault(require("query-string"));
const query_1 = require("../common/query");
/**
* Service class for event API calls. Requires an organizer to be set (call to `useOrganizer`)
*/
class OrderService {
constructor(client, version) {
this.client = client;
this.version = version;
}
/**
* Returns true if the service is reachable
* Currently the
*
* @returns Services' online status
*/
health() {
return __awaiter(this, void 0, void 0, function* () {
try {
const res = yield this.client.get(`event/health`);
if (res.data.status === 'ok') {
return { online: true };
}
}
catch (e) {
// Do nothing
}
return { online: false };
});
}
/**
* Creates a ticket order
*
* @param req `OrderRequest`
* @returns `Order`
*/
createOrder(req) {
return __awaiter(this, void 0, void 0, function* () {
let res = yield this.client.post(`event/${this.version}/order`, req);
const order = res.data.data;
if (res && req.orderContact) {
res = yield this.client.post(`event/${this.version}/order/${order.id}/order_contact`, req.orderContact);
}
return Object.assign(Object.assign({}, order), { orderContact: res.data.data });
});
}
/**
* Returns an order by it's ID
*
* @param req ID of the order
* @returns `Order`
*/
getOrder(req) {
return __awaiter(this, void 0, void 0, function* () {
const query = query_string_1.default.stringify({
with: !req.with ? undefined : (0, query_1.buildQuery)(req.with),
}, { arrayFormat: 'comma', skipNull: true, skipEmptyString: true });
const res = yield this.client.get(`event/${this.version}/order/${req.id}?${query}`);
return res.data.data;
});
}
/**
* Cancel an order by it's ID. Returns the tickets to the available ticket pool.
*
* @param req Order ID
*/
cancelOrder(req) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.post(`event/${this.version}/order/${req.id}/cancel`, {});
});
}
/**
* Removes a ticket identified by its ID from an order item in an order.
*
* @param req.id Ticket ID
* @param req.orderId Order ID
* @param req.orderItemId ID of the order item, in the order, where the ticket is.
*/
removeTicketFromOrder(req) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.delete(`event/${this.version}/order/${req.orderId}/order_item/${req.orderItemId}/ticket/${req.id}`, {});
});
}
/**
* Downloads all tickets associated with an order
*
* @param req Request contains the ID of the order
* @returns PDF document in binary format
*/
downloadTickets(req) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.client.get(`event/${this.version}/order/${req.id}/download_tickets`, {
responseType: 'blob',
});
return res.data;
});
}
/**
* Downloads a single ticket from an order
*
* @param req.orderId Order ID
* @param req.id Ticket ID
* @returns PDF document in binary format
*/
downloadTicket(req) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.client.get(`event/${this.version}/order/${req.orderId}/download_tickets/${req.id}`, {
responseType: 'blob',
});
return res.data;
});
}
/**
* Sends an email with the tickets belonging to the order.
*
* @param req Request contains the ID of the order
*/
sendOrderEmail(req) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.post(`event/${this.version}/order/${req.id}/email_tickets`, {});
});
}
/**
* Sends an email with the tickets belonging to the order.
* Invocable by an organizer
*
* @param req Request contains the ID of the order
*/
adminOrderEmailResend(req) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.post(`event/${this.version}/order/${req.id}/admin_email_tickets`, {});
});
}
/**
* Creates and assigns an order contact for a particular order
*
* @param orderId Id of the order
* @param orderContact Order contact details
* @returns Created order contact
*/
createOrderContact(orderId, orderContact) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.client.post(`event/${this.version}/order/${orderId.id}/order_contact`, orderContact);
return res.data.data;
});
}
/**
* Get list of orders
* @param id Query orders by the order ID
* @param params.organizer_id Name of the organizer the order belongs to
* @param include_not_owned Set this to true if you want to list orders created by other users
* @param contact_email Query orders by the order contact email
* @param event_id Query orders by the event ID
* @returns List of orders with the paging information included
*/
listOrders() {
return __awaiter(this, arguments, void 0, function* (params = {}) {
const query = (0, query_1.getStringifiedQuery)(params);
const res = yield this.client.get(`event/${this.version}/order?${query}`);
return res.data;
});
}
/**
* Refund an order. Partially if you specify ticketIds, fully otherwise.
*
* @param req Order ID and tickets to refund
* @param body Optional list of tickets to refund instead of the whole order
* @returns Refund success status
*/
refundOrder(id_1) {
return __awaiter(this, arguments, void 0, function* (id, body = {}) {
const res = yield this.client.post(`event/${this.version}/order/${id.id}/refund`, body);
return res.data.data;
});
}
/**
* Convert a paid order to the requested format (only PDF format is allowed)
*
* @param id Order ID
* @param body Format into which to convert the order
*/
convertOrder(id, body) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.post(`event/${this.version}/order/${id.id}/convert`, body);
});
}
/**
* Generate a PDF download token for an order
*
* @param id Order ID
* @returns Token and its expiry date
*/
generatePdfDownloadToken(id) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.client.post(`event/${this.version}/order/${id.id}/generate_pdf_token`, {});
return res.data.data;
});
}
/**
* Download order PDF using a previously generated token
*
* @param token Token used for downloading the order PDF
* @returns Single PDF document
*/
downloadPdfWithToken(token) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.client.get(`event/${this.version}/order/pdf/${token}`, {
responseType: 'blob',
});
return res.data;
});
}
/**
* Use this for generating the QR code URL for downloading the order PDF
*
* @param token Token used for downloading the order PDF
* @returns URL of the endpoint for downloading the order PDF
*/
getDownloadPdfQRCodeUrl(token) {
return `${this.client.getUri()}/event/${this.version}/order/pdf/${token}`;
}
}
exports.OrderService = OrderService;
//# sourceMappingURL=service.js.map