From 8c9ebcd7033b90c20b38438e4dfd0ce0a9f57a48 Mon Sep 17 00:00:00 2001 From: Stepan Jakl Date: Fri, 17 May 2024 22:12:27 +0100 Subject: [PATCH] add various updates --- README.md | 32 ++++++++++++++++++++++++ index.js | 12 +++++---- modules/stripe-products/product/index.js | 1 + package.json | 6 ++--- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 67d722b..1a6207f 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,37 @@ STRIPE_DASHBOARD_BASE_URL='https://dashboard.stripe.com'
+## Recommended use + +This module is utilized to primarily mirror datasets fetched from the connected Stripe account. To enhance the front-end with additional custom data, it is recommended to create a separate piece type and link it to the Stripe product piece using a `relationship` schema field type. This enables the addition of extra schema fields for images, supporting documents, and other necessary data while keeping it decoupled from Stripe's data, which may change during synchronization. + +```js +module.exports = { + extend: 'apostrophe-pieces', + name: 'product', + fields: { + add: { + _stripeProduct: { + label: 'Stripe Product', + type: 'relationship', + withType: 'stripe-products/product', + max: 1 + }, + _images: { + label: 'Images', + type: 'relationship', + withType: '@apostrophecms/image' + }, + _documents: { + label: 'Documents', + type: 'relationship', + withType: '@apostrophecms/file' + } + } + } +} +``` + ## API Routes The `stripe-products` module contains a custom API route (`'/api/v1/stripe-products/synchronize'`) triggered by the `Synchronize Products` utility operation. It is executed through the `'@apostrophecms/job'` module. Once the job is completed, it saves the difference between the existing and received data to the results object in the `aposJobs` collection document. @@ -125,3 +156,4 @@ Once set up, run tests using `npm run tests` to validate any changes before depl ## TODOs (Limitations) - fix disappering `stripeProductObject` and `stripePriceObject` data when moved between `draft` and `published` modes and vice versa +- two-way synchronization between ApostropheCMS and Stripe diff --git a/index.js b/index.js index 38c4da6..881b2e2 100644 --- a/index.js +++ b/index.js @@ -66,16 +66,16 @@ _.mixin({ deepDiff }); const Stripe = require('stripe'); let stripe = null; -if (process.env.STRIPE_TEST_MODE === 'false') { - // Using Stripe production mode settings with the API key - stripe = Stripe(process.env.STRIPE_KEY); -} else { - // Using Stripe test mode settings +if (process.env.STRIPE_MOCK_TEST_MODE === 'true') { + // Using Stripe mock test mode settings stripe = new Stripe('sk_test_xyz', { host: '127.0.0.1', protocol: 'http', port: 12111 }); +} else { + // Using Stripe production mode settings with the API key + stripe = Stripe(process.env.STRIPE_KEY); } module.exports = { @@ -209,6 +209,8 @@ module.exports = { stripeProductInstance.stripeProductObject = product; stripeProductInstance.stripePriceObject = product.default_price ? price : null; + stripeProductInstance.archived = !product.active; + await self.apos.stripeProduct.insert(req, stripeProductInstance); } } catch (error) { diff --git a/modules/stripe-products/product/index.js b/modules/stripe-products/product/index.js index 677f927..0eb893e 100644 --- a/modules/stripe-products/product/index.js +++ b/modules/stripe-products/product/index.js @@ -10,6 +10,7 @@ module.exports = { publicApiProjection: { 'stripeProductObject.id': 1, 'stripeProductObject.name': 1, + 'stripeProductObject.description': 1, 'stripeProductObject.type': 1, 'stripeProductObject.images': 1, diff --git a/package.json b/package.json index 39d099b..a754765 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stepanjakl/apostrophe-stripe-products", - "version": "0.0.4", + "version": "0.0.5", "description": "Stripe Products For ApostropheCMS", "keywords": [ "apostrophe", @@ -38,8 +38,8 @@ "mocha": "^10.x.x", "read-only-field": "npm:@stepanjakl/apostrophe-read-only-field@latest", "stripe": "^15.x.x", - "stylelint": "^15.x.x", - "stylelint-config-apostrophe": "^3.0.2" + "stylelint": "^16.x.x", + "stylelint-config-apostrophe": "^4.x.x" }, "publishConfig": { "access": "public"