@qbcart/cosmos
Version:
Azure Cosmos DB access common across the QBCart node ecosystem.
83 lines (82 loc) • 3.6 kB
JavaScript
;
/***********************************************
* @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 CustomPricing {
constructor(parent) {
this.parent = parent;
}
get(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, `CUSTOM-PRICE-${userId}`);
}
catch (error) {
console.log(error);
return null;
}
});
}
getAll(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, `CUSTOM-PRICE-${userId}`))) !== null && _a !== void 0 ? _a : []);
}
catch (error) {
console.log(error);
return [];
}
});
}
embed(returnProps, userId, items) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
try {
if (!userId)
throw 'Must be a valid userId.';
if (((_a = items === null || items === void 0 ? void 0 : items.length) !== null && _a !== void 0 ? _a : 0) === 0)
throw 'Items cannot be empty.';
let query = `SELECT ${returnProps} FROM c WHERE `;
items.map(item => {
query += `c.id="${item.id}" OR `;
});
query = query.substring(0, query.lastIndexOf(' OR '));
const results = yield this.parent.queryAll(query, `CUSTOM-PRICE-${userId}`);
if (((_b = results === null || results === void 0 ? void 0 : results.length) !== null && _b !== void 0 ? _b : 0) > 0)
items.map(item => {
const customPrice = results.find(customPrice => {
return customPrice.id === item.id;
});
if (customPrice) {
item.CustomerPrice = customPrice.price;
}
});
return items !== null && items !== void 0 ? items : [];
}
catch (error) {
console.log(error);
return [];
}
});
}
}
exports.default = CustomPricing;