Created
November 24, 2015 12:19
-
-
Save mwawrusch/d56d40668dccd3fd97d3 to your computer and use it in GitHub Desktop.
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
_ = require 'underscore' | |
Hoek = require 'hoek' | |
Boom = require 'boom' | |
async = require 'async' | |
helperProductFeeds = require './helper-product-feeds' | |
arnConstants = require './arn-constants' | |
stateConstants = require './state-constants' | |
helperProductFeedIdFromMessage = require './helper-product-feed-id-from-message' | |
helperInvokeTransloadit = require './helper-invoke-transloadit' | |
helperExtractImagesFromProductFeed = require './helper-extract-images-from-product-feed' | |
module.exports = (singleRawSqsMessage,plugin,options,cb = ->) -> | |
Hoek.assert _.isObject singleRawSqsMessage, "The required parameter 'singleRawSqsMessage' is missing or not an object." | |
Hoek.assert _.isObject plugin, "The required parameter 'plugin' is missing or not an object." | |
Hoek.assert _.isObject options, "The required parameter 'options' is missing or not an object." | |
Hoek.assert _.isFunction cb, "The required parameter 'cb' is missing or not a function." | |
Hoek.assert _.isObject options.transloadit, "The required parameter 'options.transloadit' is missing or not an object." | |
Hoek.assert _.isString options.transloadit.key, "The required parameter 'options.transloadit.key' is missing or not a string." | |
Hoek.assert _.isString options.transloadit.secret, "The required parameter 'options.transloadit.secret' is missing or not a string." | |
Hoek.assert _.isBoolean options.transloadit.testMode, "The required parameter 'options.transloadit.testMode' is missing or not a boolean." | |
Hoek.assert _.isObject options.pfImageLoader, "The required parameter 'options.pfImageLoader' is missing or not an object." | |
Hoek.assert _.isString options.pfImageLoader.transloaditCopyAssetTemplateId, "The required parameter 'options.pfImageLoader.transloaditCopyAssetTemplateId' is missing or not a string." | |
Hoek.assert _.isString options.pfImageLoader.notifyUrl, "The required parameter 'options.pfImageLoader.notifyUrl' is missing or not a string." | |
productFeeds = helperProductFeeds(plugin) | |
envyBackendSns = -> plugin.plugins['@envy/envy-backend-sns'] | |
Hoek.assert envyBackendSns(),"Could not find plugin '@envy/envy-backend-sns'." | |
productFeedId = helperProductFeedIdFromMessage(singleRawSqsMessage) | |
unless productFeedId | |
plugin.log ['envy-pf-imageloader','queue', 'envy-process-pf-product-feed-imageloader','error'], {message: "No product feed id - message ignored and deleted", data: singleRawSqsMessage} | |
return cb null | |
productFeeds().getOne productFeedId,{}, (err,productFeed) -> | |
return cb err if err # really | |
return cb Boom.notFound(productFeedId) unless productFeed | |
if productFeed.state is stateConstants.block | |
plugin.log ['envy-pf-imageloader','queue', 'envy-process-pf-product-feed-imageloader','warning'], {message: "Product feed with id #{productFeedId} is blocked - passing", productFeedId : productFeedId} | |
return cb null | |
if productFeed.substates?["imageLoader"] is stateConstants.complete | |
plugin.log ['envy-pf-imageloader','queue', 'envy-process-pf-product-feed-imageloader','warning'], {message: "Product feed with id #{productFeedId} already completed - passing", productFeedId : productFeedId} | |
return cb null | |
images = helperExtractImagesFromProductFeed productFeed | |
### | |
Here it gets funky. | |
1. we need to extract the images from both sources, and | |
ensure that we have no duplicates | |
2. for all of them, we need to invoke transloadit | |
3. we need to save the transloadit object, and the image objects for each one of those raw. | |
### | |
processedObjectData = | |
transloadit: | |
imageUrlsToProcess: images | |
totalImageCount: images.length | |
processingStartTime: Date.now() | |
images: [] | |
patchData = | |
state: "imageloader-processing" | |
"substates.imageLoader" : stateConstants.transloadit | |
processedObject: _.extend productFeed.processedObject || {}, processedObjectData || {} | |
console.log "ProductFeed: #{productFeedId} - Job imageLoader: Writing" | |
productFeeds().patchOne productFeedId,patchData,{}, (err,productData) -> | |
if err | |
plugin.log ['envy-pf-imageloader','queue', 'envy-process-pf-product-feed-imageloader','error','mongodb'], {message: "Mongodb error while patching #{productFeedId}", productFeedId : productFeedId,err: err} | |
return cb err | |
fnInvokeTransloadit = (imageUrl, cb )-> | |
console.log "Invoking Transloadit for #{imageUrl}" | |
helperInvokeTransloadit options.transloadit.testMode,options.transloadit.key,options.transloadit.secret,options.pfImageLoader.transloaditCopyAssetTemplateId,options.pfImageLoader.notifyUrl,imageUrl,productFeedId, (err) -> | |
if err | |
plugin.log ['envy-pf-imageloader','queue', 'envy-process-pf-product-feed-imageloader','error','transloadit'], {message: "Transloadit error while uploding assembly for product feed #{productFeedId}", productFeedId : productFeedId,err: err} | |
cb err | |
async.each images, fnInvokeTransloadit, (err) -> | |
return cb err if err | |
cb null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment