This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from DivanteLtd/develop
Release 1.5.0
- Loading branch information
Showing
9 changed files
with
859 additions
and
1,114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,13 +117,25 @@ | |
"registeredExtensions": [ | ||
"mailchimp-subscribe", | ||
"example-magento-api", | ||
"cms-data" | ||
"cms-data", | ||
"mail-service" | ||
], | ||
"extensions": { | ||
"mailchimp": { | ||
"listId": "e06875a7e1", | ||
"apiKey": "a9a3318ea7d30f5c5596bd4a78ae0985-us3", | ||
"apiUrl": "https://us3.api.mailchimp.com/3.0" | ||
}, | ||
"mailService": { | ||
"transport": { | ||
"host": "smtp.gmail.com", | ||
"port": 465, | ||
"secure": true, | ||
"user": "vuestorefront", | ||
"pass": "vuestorefront.io" | ||
}, | ||
"targetAddressWhitelist": ["[email protected]"], | ||
"secretString": "__THIS_IS_SO_SECRET__" | ||
} | ||
}, | ||
"magento2": { | ||
|
@@ -160,7 +172,7 @@ | |
"concurrency": 0, | ||
"counters": { | ||
"queue": 2, | ||
"process": 4 | ||
"process": 4 | ||
}, | ||
"simd": true, | ||
"keepDownloads": true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ program | |
} | ||
|
||
console.log('Done! Bye!') | ||
process.exit(0) | ||
}) | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
const program = require('commander') | ||
const config = require('config') | ||
const spawn = require('child_process').spawn | ||
|
||
function multiStoreConfig(apiConfig, storeCode) { | ||
let confCopy = Object.assign({}, apiConfig) | ||
|
||
if (storeCode && config.availableStores.indexOf(storeCode) >= 0) | ||
{ | ||
if (config.magento2['api_' + storeCode]) { | ||
confCopy = Object.assign({}, config.magento2['api_' + storeCode]) // we're to use the specific api configuration - maybe even separate magento instance | ||
} | ||
confCopy.url = confCopy.url + '/' + storeCode | ||
} else { | ||
if (storeCode) { | ||
console.error('Unavailable store code', storeCode) | ||
} | ||
} | ||
return confCopy | ||
} | ||
|
||
|
||
function exec(cmd, args, opts) { | ||
return new Promise((resolve, reject) => { | ||
let child = spawn(cmd, args, opts) | ||
child.stdout.on('data', (data) => { | ||
console.log(data.toString('utf8')); | ||
}); | ||
|
||
child.stderr.on('data', (data) => { | ||
console.log(data.toString('utf8')); | ||
}); | ||
|
||
child.on('close', (code) => { | ||
resolve(code) | ||
}); | ||
|
||
child.on('error', (error) => { | ||
console.error(errror) | ||
reject(error) | ||
}); | ||
}) | ||
} | ||
|
||
program | ||
.command('import') | ||
.option('--store-code <storeCode>', 'storeCode in multistore setup', null) | ||
.action((cmd) => { | ||
const apiConfig = multiStoreConfig(config.magento2.api, cmd.storeCode) | ||
let magentoConfig = { | ||
TIME_TO_EXIT: 2000, | ||
PRODUCTS_SPECIAL_PRICES: true, | ||
MAGENTO_CONSUMER_KEY: apiConfig.consumerKey, | ||
MAGENTO_CONSUMER_SECRET: apiConfig.consumerSecret, | ||
MAGENTO_ACCESS_TOKEN: apiConfig.accessToken, | ||
MAGENTO_ACCESS_TOKEN_SECRET: apiConfig.accessTokenSecret, | ||
MAGENTO_URL: apiConfig.url, | ||
INDEX_NAME: config.elasticsearch.indices[0] | ||
} | ||
|
||
if (cmd.storeCode) { | ||
const storeView = config.storeViews[cmd.storeCode] | ||
if (!storeView) { | ||
console.error('Wrong storeCode provided - no such store in the config.storeViews[storeCode]', cmd.storeCode) | ||
process.exit(-1) | ||
} else { | ||
magentoConfig.INDEX_NAME = storeView.elasticsearch.index; | ||
} | ||
} | ||
|
||
const env = Object.assign({}, magentoConfig, process.env) // use process env as well | ||
console.log('=== The mage2vuestorefront full reindex is about to start. Using the following Magento2 config ===', magentoConfig) | ||
|
||
console.log(' == CREATING NEW DATABASE ==') | ||
exec('node', [ | ||
'scripts/db.js', | ||
'new', | ||
`--indexName=${env.INDEX_NAME}` | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == REVIEWS IMPORTER ==') | ||
exec('node', [ | ||
'--harmony', | ||
'node_modules/mage2vuestorefront/src/cli.js', | ||
'reviews' | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == CATEGORIES IMPORTER ==') | ||
exec('node', [ | ||
'--harmony', | ||
'node_modules/mage2vuestorefront/src/cli.js', | ||
'categories', | ||
'--removeNonExistent=true', | ||
'--extendedCategories=true' | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == PRODUCT-CATEGORIES IMPORTER ==') | ||
exec('node', [ | ||
'--harmony', | ||
'node_modules/mage2vuestorefront/src/cli.js', | ||
'productcategories' | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == ATTRIBUTES IMPORTER ==') | ||
exec('node', [ | ||
'--harmony', | ||
'node_modules/mage2vuestorefront/src/cli.js', | ||
'attributes', | ||
'--removeNonExistent=true' | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == TAXRULE IMPORTER ==') | ||
exec('node', [ | ||
'--harmony', | ||
'node_modules/mage2vuestorefront/src/cli.js', | ||
'taxrule', | ||
'--removeNonExistent=true' | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == PRODUCTS IMPORTER ==') | ||
exec('node', [ | ||
'--harmony', | ||
'node_modules/mage2vuestorefront/src/cli.js', | ||
'products', | ||
'--removeNonExistent=true', | ||
'--partitions=1' | ||
], { env: env, shell: true }).then((res) => { | ||
|
||
console.log(' == REINDEXING DATABASE ==') | ||
exec('node', [ | ||
'scripts/db.js', | ||
'rebuild', | ||
`--indexName=${env.INDEX_NAME}` | ||
], { env: env, shell: true }).then((res) => { | ||
console.log('Done! Bye Bye!') | ||
process.exit(0) | ||
}); | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
|
||
program | ||
.on('command:*', () => { | ||
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' ')); | ||
process.exit(1); | ||
}); | ||
|
||
program | ||
.parse(process.argv) | ||
|
||
process.on('unhandledRejection', (reason, p) => { | ||
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason) | ||
}) | ||
|
||
process.on('uncaughtException', function(exception) { | ||
console.log(exception) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { apiStatus } from '../../../lib/util' | ||
import { Router } from 'express' | ||
import EmailCheck from 'email-check' | ||
import jwt from 'jwt-simple' | ||
import NodeMailer from 'nodemailer' | ||
|
||
module.exports = ({ config }) => { | ||
const msApi = Router() | ||
let token | ||
|
||
/** | ||
* GET send token to authorize email | ||
*/ | ||
msApi.get('/get-token', (req, res) => { | ||
token = jwt.encode(Date.now(), config.extensions.mailService.secretString) | ||
apiStatus(res, token, 200) | ||
}) | ||
|
||
/** | ||
* POST send an email | ||
*/ | ||
msApi.post('/send-email', (req, res) => { | ||
const userData = req.body | ||
if (!userData.token || userData.token !== token) { | ||
apiStatus(res, 'Email is not authorized!', 500) | ||
} | ||
const { host, port, secure, user, pass } = config.extensions.mailService.transport | ||
if (!host || !port || !user || !pass) { | ||
apiStatus(res, 'No transport is defined for mail service!', 500) | ||
} | ||
if (!userData.sourceAddress) { | ||
apiStatus(res, 'Source email address is not provided!', 500) | ||
return | ||
} | ||
if (!userData.targetAddress) { | ||
apiStatus(res, 'Target email address is not provided!', 500) | ||
return | ||
} | ||
// Check if email address we're sending to is from the white list from config | ||
const whiteList = config.extensions.mailService.targetAddressWhitelist | ||
const email = userData.confirmation ? userData.sourceAddress : userData.targetAddress | ||
if (!whiteList.includes(email)) { | ||
apiStatus(res, `Target email address (${email}) is not from the whitelist!`, 500) | ||
return | ||
} | ||
|
||
// check if provided email addresses actually exist | ||
EmailCheck(userData.sourceAddress) | ||
.then(response => { | ||
if (response) return EmailCheck(userData.targetAddress) | ||
else { | ||
apiStatus(res, 'Source email address is invalid!', 500) | ||
return | ||
} | ||
}) | ||
.then(response => { | ||
if (response) { | ||
let transporter = NodeMailer.createTransport({ | ||
host, | ||
port, | ||
secure, | ||
auth: { | ||
user, | ||
pass | ||
} | ||
}) | ||
const mailOptions = { | ||
from: userData.sourceAddress, | ||
to: userData.targetAddress, | ||
subject: userData.subject, | ||
text: userData.emailText | ||
} | ||
transporter.sendMail(mailOptions, (error) => { | ||
if (error) { | ||
apiStatus(res, error, 500) | ||
return | ||
} | ||
apiStatus(res, 'OK', 200) | ||
transporter.close() | ||
}) | ||
} else { | ||
apiStatus(res, 'Target email address is invalid!', 500) | ||
} | ||
}) | ||
.catch(() => { | ||
apiStatus(res, 'Invalid email address is provided!', 500) | ||
}) | ||
}) | ||
|
||
return msApi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.