-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(process): added cli parameters for importing directly to sphere
- Loading branch information
Showing
6 changed files
with
285 additions
and
42 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
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,94 @@ | ||
_ = require 'underscore' | ||
Promise = require 'bluebird' | ||
ProductTypeImport = require 'sphere-product-type-import' | ||
{ExtendedLogger, ProjectCredentialsConfig} = require 'sphere-node-utils' | ||
package_json = require '../package.json' | ||
|
||
class ProductTypeImporter | ||
|
||
### | ||
Prepare for importing, initialize used modules | ||
### | ||
init: (argv) -> | ||
@argv = argv | ||
@logOptions = | ||
name: "#{package_json.name}-#{package_json.version}" | ||
silent: !! argv.logSilent | ||
streams: [ | ||
{ level: 'error', stream: process.stderr } | ||
{ level: argv.logLevel || 'info', path: (argv.logDir || '.') + "/#{package_json.name}.log" } | ||
] | ||
|
||
@logger = new ExtendedLogger | ||
additionalFields: | ||
'project_key': argv.projectKey | ||
logConfig: @logOptions | ||
if argv.logSilent | ||
@logger.bunyanLogger.trace = -> # noop | ||
@logger.bunyanLogger.debug = -> # noop | ||
|
||
@_ensureProductTypeImporter @argv, @logger | ||
.then (sphereImporter) -> | ||
@sphereImporter = sphereImporter | ||
Promise.resolve @ | ||
|
||
### | ||
Create sphere credentials config from command line arguments | ||
### | ||
_ensureCredentials: (argv) -> | ||
credentialsPromise = null | ||
|
||
if argv.accessToken | ||
credentialsPromise = Promise.resolve | ||
config: | ||
project_key: argv.projectKey | ||
access_token: argv.accessToken | ||
else | ||
credentialsPromise = ProjectCredentialsConfig.create() | ||
.then (credentials) -> | ||
Promise.resolve | ||
config: credentials.enrichCredentials | ||
project_key: argv.projectKey | ||
client_id: argv.clientId | ||
client_secret: argv.clientSecret | ||
|
||
credentialsPromise.then (credentials) -> | ||
options = _.extend credentials, | ||
timeout: argv.timeout | ||
user_agent: "#{package_json.name} - #{package_json.version}" | ||
|
||
options.host = argv.sphereHost if argv.sphereHost | ||
options.protocol = argv.sphereProtocol if argv.sphereProtocol | ||
if argv.sphereAuthHost | ||
options.oauth_host = argv.sphereAuthHost | ||
options.rejectUnauthorized = false | ||
options.oauth_protocol = argv.sphereAuthProtocol if argv.sphereAuthProtocol | ||
|
||
config = | ||
sphereClientConfig: options | ||
|
||
Promise.resolve config | ||
|
||
### | ||
Create sphere product import class | ||
### | ||
_ensureProductTypeImporter: (argv, logger) -> | ||
@_ensureCredentials argv | ||
.then (credentials) -> | ||
new ProductTypeImport.default(logger, credentials) | ||
|
||
### | ||
Import product types using sphere product import tool | ||
### | ||
import: (data) -> | ||
console.log "Importing product types to sphere" | ||
Promise.map data.productTypes, (productType) -> | ||
@sphereImporter.importProductType productType | ||
.then (res) -> | ||
console.log "Imported product type", res.name | ||
Promise.resolve res | ||
.catch (e) -> | ||
console.error "Oops, something went wrong when importing product type #{productType.name} to sphere" | ||
Promise.reject e | ||
|
||
module.exports = ProductTypeImporter |
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
File renamed without changes.
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,110 @@ | ||
Promise = require 'bluebird' | ||
ProductTypeImporter = require '../lib/product-type-import' | ||
{SphereClient} = require 'sphere-node-sdk' | ||
|
||
errMissingCredentials = 'Missing configuration in env variables' | ||
|
||
argv = | ||
clientId: process.env.SPHERE_CLIENT_ID | ||
clientSecret: process.env.SPHERE_CLIENT_SECRET | ||
projectKey: process.env.SPHERE_PROJECT_KEY | ||
logSilent: true | ||
|
||
testProductType = { | ||
"name": "top_and_shirts", | ||
"description": "Tops & Shirts", | ||
"attributes": [ | ||
{ | ||
"name": "designer", | ||
"label": { | ||
"de": "Designer", | ||
"en": "designer" | ||
}, | ||
"type": { | ||
"name": "text" | ||
}, | ||
"attributeConstraint": "SameForAll", | ||
"isRequired": false, | ||
"isSearchable": false, | ||
"inputHint": "SingleLine" | ||
}, | ||
{ | ||
"name": "materialComposition", | ||
"label": { | ||
"de": "Zusammensetzung Material", | ||
"en": "material composition" | ||
}, | ||
"type": { | ||
"name": "ltext" | ||
}, | ||
"attributeConstraint": "None", | ||
"isRequired": false, | ||
"isSearchable": false, | ||
"inputHint": "MultiLine" | ||
} | ||
] | ||
} | ||
|
||
describe 'ProductTypeImporter', -> | ||
importer = null | ||
sphereClient = null | ||
|
||
beforeEach (done) -> | ||
expect(argv.clientId).toBeDefined errMissingCredentials | ||
expect(argv.clientSecret).toBeDefined errMissingCredentials | ||
expect(argv.projectKey).toBeDefined errMissingCredentials | ||
|
||
config = | ||
config: | ||
'client_id': argv.clientId | ||
'client_secret': argv.clientSecret | ||
'project_key': argv.projectKey | ||
stats: | ||
includeHeaders: true | ||
maskSensitiveHeaderData: true | ||
timeout: 360000 | ||
|
||
sphereClient = new SphereClient config | ||
importer = new ProductTypeImporter | ||
importer.init(argv) | ||
.then -> | ||
sphereClient.productTypes.fetch() | ||
.then (res) -> | ||
console.log "Deleting old product types", res.body.results.length | ||
Promise.map res.body.results, (productType) -> | ||
sphereClient.productTypes.byId(productType.id) | ||
.delete(productType.version) | ||
.then -> | ||
done() | ||
.catch (e) -> | ||
done(e) | ||
|
||
it 'should import product type', (done) -> | ||
expect(importer).toBeDefined() | ||
expect(sphereClient).toBeDefined() | ||
|
||
sphereClient.productTypes.fetch() | ||
.then (res) -> | ||
expect(res.body.results.length).toEqual 0 | ||
console.log "Importing product type using importer" | ||
importer.import {productTypes: [testProductType]} | ||
.then -> | ||
console.log "Product type imported - verifiing result" | ||
sphereClient.productTypes.fetch() | ||
.then (res) -> | ||
expect(res.body.results.length).toEqual 1 | ||
|
||
done() | ||
.catch (e) -> | ||
done(e) | ||
|
||
it 'should not import wrong product type', (done) -> | ||
expect(importer).toBeDefined() | ||
expect(sphereClient).toBeDefined() | ||
|
||
delete testProductType.name | ||
importer.import {productTypes: [testProductType]} | ||
.then -> | ||
done "Importer wrong product type" | ||
.catch -> | ||
done() |