@vivlab/api-database
Advanced tools
Comparing version 1.0.27 to 1.0.28
import ProductPrice from "../../functional/ProductPrice"; | ||
declare const products: ProductPrice<import("../../functional/ProductPrice").Product>; | ||
import type { Product } from "@vivlab/types"; | ||
declare const products: ProductPrice<Product.EditProductData>; | ||
export default products; |
@@ -27,4 +27,8 @@ "use strict"; | ||
const ProductPrice_1 = __importDefault(require("../../functional/ProductPrice")); | ||
const products = new ProductPrice_1.default({ table: "products", transform }); | ||
const products = new ProductPrice_1.default({ | ||
table: "products", | ||
transform, | ||
algoliaIndice: stripeAccount => `products_${stripeAccount}` | ||
}); | ||
exports.default = products; | ||
//# sourceMappingURL=index.js.map |
@@ -11,1 +11,13 @@ import type { Stripe } from "stripe"; | ||
export declare function dataToStripePriceUpdate(data: ApiPriceData): Stripe.PriceUpdateParams; | ||
export declare type HierarchyLevel = "collections.lvl0" | "collections.lvl1" | "collections.lvl2" | "collections.lvl3" | "collections.lvl4" | "collections.lvl5"; | ||
export interface AlgoliaProductData extends Omit<Partial<ApiProductData>, "prices"> { | ||
link?: string; | ||
description?: string | null; | ||
"collections.lvl0"?: string[]; | ||
"collections.lvl1"?: string[]; | ||
"collections.lvl2"?: string[]; | ||
"collections.lvl3"?: string[]; | ||
"collections.lvl4"?: string[]; | ||
"collections.lvl5"?: string[]; | ||
} | ||
export declare function dataToAlgoliaProduct(data: ApiProductData): AlgoliaProductData; |
@@ -22,3 +22,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dataToStripePriceUpdate = exports.dataToStripePriceCreate = exports.dataToStripeProductUpdate = exports.dataToStripeProductCreate = exports.stripeToPriceData = exports.stripeToProductData = void 0; | ||
exports.dataToAlgoliaProduct = exports.dataToStripePriceUpdate = exports.dataToStripePriceCreate = exports.dataToStripeProductUpdate = exports.dataToStripeProductCreate = exports.stripeToPriceData = exports.stripeToProductData = void 0; | ||
const transform_1 = require("../../libraries/transform"); | ||
@@ -138,2 +138,26 @@ const sanitizeHtml = __importStar(require("../../libraries/sanitizeHtml")); | ||
exports.dataToStripePriceUpdate = dataToStripePriceUpdate; | ||
function dataToAlgoliaProduct(data) { | ||
const payload = { ...data }; | ||
if (data.name !== undefined) { | ||
payload.link = `/products/${(0, utils_1.removeDiacritics)(data.name)}/${data.id}`; | ||
} | ||
if (data.description !== undefined) { | ||
payload.description = sanitizeHtml.htmlToText(data.description); | ||
} | ||
if (data.collections) { | ||
const levels = Array.from({ length: 6 }, (_, i) => `collections.lvl${i}`); | ||
for (const el of data.collections) { | ||
const fragments = el.split(" > "); | ||
for (let i = 0; i < fragments.length; i++) { | ||
const frag = fragments.slice(0, i + 1).join(" > "); | ||
if (!payload[levels[i]]) | ||
payload[levels[i]] = [frag]; | ||
else if (!payload[levels[i]]?.includes(frag)) | ||
payload[levels[i]]?.push(frag); | ||
} | ||
} | ||
} | ||
return payload; | ||
} | ||
exports.dataToAlgoliaProduct = dataToAlgoliaProduct; | ||
//# sourceMappingURL=productsTransform.js.map |
@@ -27,4 +27,8 @@ "use strict"; | ||
const ProductPrice_1 = __importDefault(require("../../functional/ProductPrice")); | ||
const shippings = new ProductPrice_1.default({ table: "shippings", transform }); | ||
const shippings = new ProductPrice_1.default({ | ||
table: "shippings", | ||
transform, | ||
algoliaIndice: () => "shippings" | ||
}); | ||
exports.default = shippings; | ||
//# sourceMappingURL=index.js.map |
@@ -11,1 +11,5 @@ import type { Stripe } from "stripe"; | ||
export declare function dataToStripePriceUpdate(data: ApiPriceData): Stripe.PriceUpdateParams; | ||
export interface AlgoliaShippingData extends Omit<Partial<ApiProductData>, "prices"> { | ||
description?: string | null; | ||
} | ||
export declare function dataToAlgoliaProduct(data: ApiProductData): AlgoliaShippingData; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dataToStripePriceUpdate = exports.dataToStripePriceCreate = exports.dataToStripeProductUpdate = exports.dataToStripeProductCreate = exports.stripeToPriceData = exports.stripeToProductData = void 0; | ||
exports.dataToAlgoliaProduct = exports.dataToStripePriceUpdate = exports.dataToStripePriceCreate = exports.dataToStripeProductUpdate = exports.dataToStripeProductCreate = exports.stripeToPriceData = exports.stripeToProductData = void 0; | ||
const transform_1 = require("../../libraries/transform"); | ||
const sanitizeHtml = __importStar(require("../../libraries/sanitizeHtml")); | ||
function stripeToProductData(data) { | ||
@@ -84,2 +104,10 @@ return { | ||
exports.dataToStripePriceUpdate = dataToStripePriceUpdate; | ||
function dataToAlgoliaProduct(data) { | ||
const payload = { ...data }; | ||
if (data.description !== undefined) { | ||
payload.description = sanitizeHtml.htmlToText(data.description); | ||
} | ||
return payload; | ||
} | ||
exports.dataToAlgoliaProduct = dataToAlgoliaProduct; | ||
//# sourceMappingURL=shippingsTransform.js.map |
@@ -19,2 +19,3 @@ import type { IdAccount, IdDataAccount, DataAccount } from "../libraries/stripe"; | ||
table: string; | ||
algoliaIndice: (stripeAccount: string) => string; | ||
transform: { | ||
@@ -31,2 +32,5 @@ dataToStripeProductCreate: (e: ProductData<T>) => Stripe.ProductCreateParams; | ||
}; | ||
dataToAlgoliaProduct: (e: ProductData<T>) => ProductData<T> & { | ||
objectID: string; | ||
}; | ||
}; | ||
@@ -49,2 +53,5 @@ } | ||
}; | ||
stats: { | ||
lowest_price: number | null; | ||
}; | ||
} | ||
@@ -54,3 +61,4 @@ export default class ProductPrice<T extends Product> { | ||
table: ProductPriceTransformOptions<T>["table"]; | ||
constructor({ transform, table }: ProductPriceTransformOptions<T>); | ||
algoliaIndice: ProductPriceTransformOptions<T>["algoliaIndice"]; | ||
constructor({ transform, table, algoliaIndice }: ProductPriceTransformOptions<T>); | ||
retrieve({ id, stripeAccount }: IdAccount): Promise<{ | ||
@@ -85,3 +93,5 @@ prices: (Utils.ArrayElement<T["prices"]> & Price & { | ||
}>; | ||
createPrice({ data, stripeAccount }: DataAccount<PriceData<T>>): Promise<Utils.ArrayElement<T["prices"]> & Price & { | ||
createPrice({ product, data, stripeAccount }: DataAccount<PriceData<T>> & { | ||
product: string; | ||
}): Promise<Utils.ArrayElement<T["prices"]> & Price & { | ||
id: string; | ||
@@ -108,2 +118,10 @@ }>; | ||
clearCache({ id, stripeAccount }: IdAccount): Promise<void>; | ||
syncAlgolia({ id, data, stats, stripeAccount }: { | ||
id: string; | ||
stripeAccount: string; | ||
data?: ProductData<T>; | ||
stats?: { | ||
lowest_price: number | null; | ||
}; | ||
}): Promise<void>; | ||
} |
@@ -24,5 +24,7 @@ "use strict"; | ||
const cache = __importStar(require("../libraries/cache")); | ||
const algoliasearch = __importStar(require("../libraries/algoliasearch")); | ||
class ProductPrice { | ||
constructor({ transform, table }) { | ||
constructor({ transform, table, algoliaIndice }) { | ||
this.transform = transform; | ||
this.algoliaIndice = algoliaIndice; | ||
this.table = table; | ||
@@ -39,2 +41,4 @@ } | ||
const { prices: pricesData = [], ...productData } = data; | ||
if (!pricesData.length) | ||
throw new Error("you must pass prices parameter"); | ||
const product = await this.createProduct({ data: productData, stripeAccount }); | ||
@@ -44,11 +48,14 @@ const prices = []; | ||
const data = { ...pricesData[i], lookup_key: `${product.id}_${i}` }; | ||
const price = await this.createPrice({ data, stripeAccount }); | ||
const price = await this.createPrice({ product: product.id, data, stripeAccount }); | ||
prices.push(price); | ||
} | ||
const stats = { lowest_price: Math.min(...pricesData.map(e => e.unit_amount)) }; | ||
await this.syncAlgolia({ id: product.id, data: product, stats, stripeAccount }); | ||
return { ...product, prices }; | ||
} | ||
async update({ id, data, stripeAccount }) { | ||
const { create, update, del } = data; | ||
const { create, update, del, stats } = data; | ||
let product; | ||
if (update.product) { | ||
await this.updateProduct({ id, data: update.product, stripeAccount }); | ||
product = await this.updateProduct({ id, data: update.product, stripeAccount }); | ||
} | ||
@@ -60,3 +67,3 @@ const entries = Object.entries(update.prices); | ||
for (const data of create.prices) { | ||
await this.createPrice({ data: { ...data, product: id }, stripeAccount }); | ||
await this.createPrice({ product: id, data, stripeAccount }); | ||
} | ||
@@ -67,2 +74,3 @@ for (const id of del.prices) { | ||
await this.clearCache({ id, stripeAccount }); | ||
await this.syncAlgolia({ id, data: product, stats, stripeAccount }); | ||
return { status: "ok" }; | ||
@@ -77,2 +85,7 @@ } | ||
await this.clearCache({ id, stripeAccount }); | ||
await algoliasearch.deleteObjects({ | ||
indice: this.algoliaIndice(stripeAccount), | ||
id, | ||
stripeAccount | ||
}); | ||
return { status: "ok" }; | ||
@@ -94,8 +107,7 @@ } | ||
} | ||
async createPrice({ data, stripeAccount }) { | ||
async createPrice({ product, data, stripeAccount }) { | ||
const payload = this.transform.dataToStripePriceCreate(data); | ||
const price = await stripe_1.default.prices.create(payload, { stripeAccount }); | ||
const price = await stripe_1.default.prices.create({ ...payload, product }, { stripeAccount }); | ||
return this.transform.stripeToPriceData(price); | ||
} | ||
// async updatePrice({ id, data, stripeAccount }: IdDataAccount<PriceData<T>>) { | ||
async updatePrice({ id, data, stripeAccount }) { | ||
@@ -135,4 +147,12 @@ const payload = this.transform.dataToStripePriceUpdate(data); | ||
} | ||
async syncAlgolia({ id, data, stats, stripeAccount }) { | ||
const payload = data && this.transform.dataToAlgoliaProduct(data); | ||
await algoliasearch.partialUpdateObjects({ | ||
indice: this.algoliaIndice(stripeAccount), | ||
payload: { objectID: id, ...payload, price_unit_amount: stats?.lowest_price ?? undefined }, | ||
stripeAccount | ||
}); | ||
} | ||
} | ||
exports.default = ProductPrice; | ||
//# sourceMappingURL=ProductPrice.js.map |
export declare function sanitize(rawData: string | null | undefined): string | null | undefined; | ||
export declare function htmlToText(html: string): string | null; | ||
export declare function htmlToText(html: string | null): string | null; |
{ | ||
"name": "@vivlab/api-database", | ||
"version": "1.0.27", | ||
"version": "1.0.28", | ||
"description": "", | ||
@@ -38,2 +38,3 @@ "main": "./dist/index.js", | ||
"@vivlab/redis-queries": "^1.2.8", | ||
"algoliasearch": "^4.11.0", | ||
"fast-deep-equal": "^3.1.3", | ||
@@ -40,0 +41,0 @@ "sanitize-html": "^2.5.3", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
274901
124
4402
5
13
+ Addedalgoliasearch@^4.11.0
+ Added@algolia/cache-browser-local-storage@4.24.0(transitive)
+ Added@algolia/cache-common@4.24.0(transitive)
+ Added@algolia/cache-in-memory@4.24.0(transitive)
+ Added@algolia/client-account@4.24.0(transitive)
+ Added@algolia/client-analytics@4.24.0(transitive)
+ Added@algolia/client-common@4.24.0(transitive)
+ Added@algolia/client-personalization@4.24.0(transitive)
+ Added@algolia/client-search@4.24.0(transitive)
+ Added@algolia/logger-common@4.24.0(transitive)
+ Added@algolia/logger-console@4.24.0(transitive)
+ Added@algolia/recommend@4.24.0(transitive)
+ Added@algolia/requester-browser-xhr@4.24.0(transitive)
+ Added@algolia/requester-common@4.24.0(transitive)
+ Added@algolia/requester-node-http@4.24.0(transitive)
+ Added@algolia/transporter@4.24.0(transitive)
+ Addedalgoliasearch@4.24.0(transitive)