UNPKG

@qbcart/cosmos

Version:

Azure Cosmos DB access common across the QBCart node ecosystem.

152 lines (151 loc) 5.75 kB
"use strict"; /*********************************************** * @license * Copyright (c) QBCart Inc. All rights reserved. ************************************************/ 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); class Cart { constructor(parent) { this.parent = parent; } getItem(id, userId) { return __awaiter(this, void 0, void 0, function* () { try { if (!id) throw 'Missing an id.'; if (!userId) throw 'Must be a valid userId'; return yield this.parent.read(id, `CART-ITEM-${userId}`); } catch (error) { console.log(error); return null; } }); } getItems(returnProps, userId, lastSynced) { var _a; return __awaiter(this, void 0, void 0, function* () { try { if (!userId) throw 'Must be a valid userId'; const query = `SELECT ${returnProps} FROM c WHERE c._ts > ${lastSynced !== null && lastSynced !== void 0 ? lastSynced : 0}`; return ((_a = (yield this.parent.queryAll(query, `CART-ITEM-${userId}`))) !== null && _a !== void 0 ? _a : []); } catch (error) { console.log(error); return []; } }); } addItem(id, price, quantity, sortOrder, userId) { return __awaiter(this, void 0, void 0, function* () { try { if (!id) throw 'Missing an id.'; if (!userId) throw 'Must be a valid userId'; const item = yield this.getItem(id, userId); if (item) { item.price = price; item.quantity += quantity; // Don't assign item.sortOrder = sortOrder, as this shuffle the previously added order yield this.parent.container .item(item.id, item.Discriminator) .replace(item); } else { yield this.parent.container.items.create({ id: id, Discriminator: `CART-ITEM-${userId}`, Created: new Date(), price: price, quantity: quantity, sortOrder: sortOrder }); } return true; } catch (error) { console.log(error); return false; } }); } updateItem(id, price, quantity, userId) { return __awaiter(this, void 0, void 0, function* () { try { if (!id) throw 'Missing an id.'; if (!userId) throw 'Must be a valid userId.'; const item = yield this.getItem(id, userId); if (!item) throw 'Cannot update item that does not exist.'; item.price = price; item.quantity = quantity; yield this.parent.container .item(item.id, item.Discriminator) .replace(item); return true; } catch (error) { console.log(error); return false; } }); } removeItem(id, userId) { return __awaiter(this, void 0, void 0, function* () { try { if (!id) throw 'Missing an id.'; if (!userId) throw 'Must be a valid userId.'; const item = yield this.getItem(id, userId); if (!item) throw 'Cannot remove item that does not exist.'; item.quantity = 0; yield this.parent.container .item(item.id, item.Discriminator) .replace(item); return true; } catch (error) { console.log(error); return false; } }); } clearItems(userId) { return __awaiter(this, void 0, void 0, function* () { try { if (!userId) throw 'Must be a valid userId.'; const items = yield this.getItems('*', userId); for (let i = 0; i < items.length; i++) { items[i].quantity = 0; items[i].Discriminator = `CART-ITEM-${userId}`; yield this.parent.container .item(items[i].id, items[i].Discriminator) .replace(items[i]); } return true; } catch (error) { console.log(error); return false; } }); } } exports.default = Cart;