From cedfc43decc89c8d6c9a7d12ce7d1356d52cef8e Mon Sep 17 00:00:00 2001 From: vladimir talas Date: Fri, 15 Mar 2024 13:59:20 +0100 Subject: [PATCH] Shopify: mandatory webhooks --- src/appmixer/shopify/CustomerDataRequest.js | 26 +++++ src/appmixer/shopify/CustomerRedactRequest.js | 26 +++++ src/appmixer/shopify/ShopRedactRequest.js | 27 +++++ src/appmixer/shopify/package.json | 1 + src/appmixer/shopify/plugin.js | 47 +++----- src/appmixer/shopify/routes.js | 104 ++++++++++++++++++ 6 files changed, 200 insertions(+), 31 deletions(-) create mode 100644 src/appmixer/shopify/CustomerDataRequest.js create mode 100644 src/appmixer/shopify/CustomerRedactRequest.js create mode 100644 src/appmixer/shopify/ShopRedactRequest.js create mode 100644 src/appmixer/shopify/routes.js diff --git a/src/appmixer/shopify/CustomerDataRequest.js b/src/appmixer/shopify/CustomerDataRequest.js new file mode 100644 index 000000000..08f61ca9b --- /dev/null +++ b/src/appmixer/shopify/CustomerDataRequest.js @@ -0,0 +1,26 @@ +module.exports = context => { + + class CustomerDataRequest extends context.db.Model { + + static get collection() { + return 'customerDataRequests_temp_1'; + } + + static get idProperty() { + return 'id'; + } + + static get properties() { + + return [ + 'id', + 'request', + 'created' + ]; + } + } + + CustomerDataRequest.createSettersAndGetters(); + + return CustomerDataRequest; +}; diff --git a/src/appmixer/shopify/CustomerRedactRequest.js b/src/appmixer/shopify/CustomerRedactRequest.js new file mode 100644 index 000000000..43ca1a29f --- /dev/null +++ b/src/appmixer/shopify/CustomerRedactRequest.js @@ -0,0 +1,26 @@ +module.exports = context => { + + class CustomerRedactRequest extends context.db.Model { + + static get collection() { + return 'customerRedactsRequests_temp_1'; + } + + static get idProperty() { + return 'id'; + } + + static get properties() { + + return [ + 'id', + 'request', + 'created' + ]; + } + } + + CustomerRedactRequest.createSettersAndGetters(); + + return CustomerRedactRequest; +}; diff --git a/src/appmixer/shopify/ShopRedactRequest.js b/src/appmixer/shopify/ShopRedactRequest.js new file mode 100644 index 000000000..15284640b --- /dev/null +++ b/src/appmixer/shopify/ShopRedactRequest.js @@ -0,0 +1,27 @@ +module.exports = context => { + + class ShopRedactRequest extends context.db.Model { + + static get collection() { + return 'shopRedactRequests_temp_1'; + } + + static get idProperty() { + return 'id'; + } + + static get properties() { + + return [ + 'id', + 'request', + 'created' + ]; + } + } + + ShopRedactRequest.createSettersAndGetters(); + + return ShopRedactRequest; +}; + diff --git a/src/appmixer/shopify/package.json b/src/appmixer/shopify/package.json index 0aedc280a..78edee664 100644 --- a/src/appmixer/shopify/package.json +++ b/src/appmixer/shopify/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "author": "Jimoh Damilola ", "dependencies": { + "crypto": "1.0.1", "shopify-api-node": "3.12.1", "shopify-token": "4.0.0" } diff --git a/src/appmixer/shopify/plugin.js b/src/appmixer/shopify/plugin.js index 4f1a6b7fc..1058a9dd9 100644 --- a/src/appmixer/shopify/plugin.js +++ b/src/appmixer/shopify/plugin.js @@ -1,37 +1,22 @@ -const path = require('path'); +module.exports = async function(context) { -module.exports = function(context) { + const lock = await context.lock('shopify-plugin-init'); - context.http.router.register({ - method: 'POST', - path: '/customers/data_request', - options: { - handler: (request, h) => { - return {} - }, - auth: false - } - }); + let secret; + try { + secret = await context.service.stateGet('secret'); + if (!secret) { - context.http.router.register({ - method: 'POST', - path: '/customers/redact', - options: { - handler: (request, h) => { - return {} - }, - auth: false - } - }); + await require('./CustomerDataRequest')(context).createIndex({ status: 1 }); + await require('./CustomerRedactRequest')(context).createIndex({ status: 1 }); + await require('./ShopRedactRequest')(context).createIndex({ status: 1 }); - context.http.router.register({ - method: 'POST', - path: '/shop/redact', - options: { - handler: (request, h) => { - return {} - }, - auth: false + secret = require('crypto').randomBytes(128).toString('base64'); + await context.service.stateSet('secret', secret); } - }); + } finally { + lock.unlock(); + } + + require('./routes')(context); }; diff --git a/src/appmixer/shopify/routes.js b/src/appmixer/shopify/routes.js new file mode 100644 index 000000000..67e53620c --- /dev/null +++ b/src/appmixer/shopify/routes.js @@ -0,0 +1,104 @@ +module.exports = function(context) { + + const CustomerDataRequest = require('./CustomerDataRequest')(context); + const CustomerRedactRequest = require('./CustomerRedactRequest')(context); + const ShopRedactRequest = require('./ShopRedactRequest')(context); + + context.http.router.register({ + method: 'POST', + path: '/customers/data_request', + options: { + handler: (req) => { + + const { payload } = req; + + return new CustomerDataRequest().populate({ + request: payload, + created: new Date(), + }).save(); + }, + auth: false + } + }); + + context.http.router.register({ + method: 'POST', + path: '/customers/redact', + options: { + handler: (req) => { + + const { payload } = req; + + return new CustomerRedactRequest().populate({ + request: payload, + created: new Date(), + }).save(); + }, + auth: false + } + }); + + context.http.router.register({ + method: 'POST', + path: '/shop/redact', + options: { + handler: (req) => { + + const { payload } = req; + + return new ShopRedactRequest().populate({ + request: payload, + created: new Date(), + }).save(); + }, + auth: false + } + }); + + context.http.router.register({ + method: 'GET', + path: '/customers/data_request', + options: { + handler: (request, h) => { + return CustomerDataRequest.find() || []; + }, + auth: false + } + }); + + context.http.router.register({ + method: 'GET', + path: '/customers/redact', + options: { + handler: () => { + return CustomerRedactRequest.find() || []; + }, + auth: false + } + }); + + context.http.router.register({ + method: 'GET', + path: '/shop/redact', + options: { + handler: () => { + return ShopRedactRequest.find() || []; + }, + auth: false + } + }); + + context.http.router.register({ + method: 'GET', + path: '/', + options: { + handler: (request, h) => { + return { + version: '1.0', + published: new Date().toISOString() + }; + }, + auth: false + } + }); +};