@qbcart/cosmos
Version:
Azure Cosmos DB access common across the QBCart node ecosystem.
64 lines (63 loc) • 2.12 kB
JavaScript
/***********************************************
* @license
* Copyright (c) QBCart Inc. All rights reserved.
************************************************/
export default class CustomPricing {
parent;
constructor(parent) {
this.parent = parent;
}
async get(id, userId) {
try {
if (!id)
throw 'Missing an id.';
if (!userId)
throw 'Must be a valid userId.';
return await this.parent.read(id, `CUSTOM-PRICE-${userId}`);
}
catch (error) {
console.log(error);
return null;
}
}
async getAll(returnProps, userId, lastSynced) {
try {
if (!userId)
throw 'Must be a valid userId.';
const query = `SELECT ${returnProps} FROM c WHERE c._ts > ${lastSynced ?? 0}`;
return ((await this.parent.queryAll(query, `CUSTOM-PRICE-${userId}`)) ?? []);
}
catch (error) {
console.log(error);
return [];
}
}
async embed(returnProps, userId, items) {
try {
if (!userId)
throw 'Must be a valid userId.';
if ((items?.length ?? 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 = await this.parent.queryAll(query, `CUSTOM-PRICE-${userId}`);
if ((results?.length ?? 0) > 0)
items.map(item => {
const customPrice = results.find(customPrice => {
return customPrice.id === item.id;
});
if (customPrice) {
item.CustomerPrice = customPrice.price;
}
});
return items ?? [];
}
catch (error) {
console.log(error);
return [];
}
}
}