Skip to content

Commit

Permalink
Merge branch 'pr/asad-rafter/5'
Browse files Browse the repository at this point in the history
# Conflicts:
#	metadata/cloudinary-metadata.zip
  • Loading branch information
yuval-cloudinary committed Feb 11, 2024
2 parents 8963bd7 + ed1bc44 commit fa6befc
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

// API includes
var jobLogger = require('dw/system').Logger.getLogger('Cloudinary', 'CREATE');
var Status = require('dw/system/Status');

module.exports.Start = function (args) {
var cloudinaryConstants = require('*/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryMetadataSvc = require('*/cartridge/scripts/service/cldMetadata');
var jobStepHelpers = require('*/cartridge/scripts/helpers/jobStepHelpers');

if (!cloudinaryConstants.CLD_ENABLED) {
return new Status(Status.ERROR, 'ERROR', 'The Cloudinary cartridge is disabled');
}

if (jobStepHelpers.isStepDisabled(args)) {
return new Status(Status.OK, 'OK', 'Step disabled, skip it...');
}

const schema = [
{ "label": "sfcc-product-name", "external_id": "sfcc-product-name", "type": "string" },
{ "label": "sfcc-view-type", "external_id": "sfcc-view-type", "type": "string" },
{ "label": "sfcc-product-brand", "external_id": "sfcc-product-brand", "type": "string" },
{ "label": "sfcc-product-identifier", "external_id": "sfcc-product-identifier", "type": "string" },
{ "label": "sfcc-is-main", "external_id": "sfcc-is-main", "type": "enum", "datasource": { "values": [{ "external_id": "True", "value": "True" }, { "external_id": "False", "value": "False" }] } },
{ "label": "sfcc-gallery-position", "external_id": "sfcc-gallery-position", "type": "integer" },
{ "label": "sfcc-alt-text", "external_id": "sfcc-alt-text", "type": "string" }
];

try {
cloudinaryMetadataSvc.createMetadataSchemeCld(schema);
} catch (e) {
jobLogger.error('Error occurred while creating metadata, message : {0}', e.message);
}

return new Status(Status.OK);
};
49 changes: 46 additions & 3 deletions cartridges/int_cloudinary/cartridge/scripts/service/cldMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var Prefs = require('*/cartridge/scripts/util/cloudinaryConstants');
* @returns {string} result - The API service response (JSON)
*/
function fetchMetadataFromCld() {
var cldResponse = { ok: true, message: '', result: { } };
var cldResponse = { ok: true, message: '', result: {} };
var configArgs = {};
var result = [];

Expand All @@ -33,7 +33,7 @@ function fetchMetadataFromCld() {
if (result.error === Prefs.ERROR_CODES.UNAUTHORIZED) {
logger.error('Error occurred while connecting to the service due to invalid credentials, message: {0}', result.errorMessage);
} else {
logger.error('Error occured while retrieving metadata fields from cloudinary');
logger.error('Error occurred while retrieving metadata fields from cloudinary');
}
cldResponse.ok = false;
cldResponse.message = result.errorMessage;
Expand Down Expand Up @@ -64,9 +64,52 @@ function fetchMetadata() {
return metadataFields;
}


/**
* This method uses the service to create metadata fields in cld.
*
* @param {Array} schema - Schema to be created in Cloudinary
* @returns {string} result - The API service response (JSON)
*/
function createMetadataSchemeCld(schema) {
var cldResponse = { ok: true, message: '', result: {} };
var configArgs = {};
var result = [];

configArgs.method = 'POST';
configArgs.endPoint = Prefs.CLD_CLOUDNAME + '/metadata_fields';;
var service = cldWebService.getService(Prefs.CLD_UPLOAD_SVC, cldWebService.getServiceConfigs(configArgs));
service.setCredentialID(Prefs.CLD_REST_SERVICE_CREDENTIALS);

schema.forEach(function (field) {
try {
result = service.call(field);
if (result.ok && result.error === 0) {
cldResponse.ok = true;
cldResponse.result[field.label] = result.object;
logger.info('Metadata fields: {0} is created successfully.', result.object.label);
} else {
if (result.error === Prefs.ERROR_CODES.UNAUTHORIZED) {
logger.error('Error occurred while connecting to the service due to invalid credentials, message: {0}', result.errorMessage);
} else {
logger.error('Error occurred while creating metadata fields from cloudinary: {0}', JSON.parse(result.errorMessage).error.message);
}
cldResponse.ok = false;
cldResponse.message += '\n' + field.label + ': ' + result.errorMessage;
}
} catch (e) {
cldResponse.ok = false;
cldResponse.message += '\n' + field.label + ': ' + e.message;
logger.error(Prefs.CLD_GENERAL_ERROR + e.message);
}
});
return cldResponse;
}

/*
* Module exposed methods
*/
module.exports = {
fetchMetadata: fetchMetadata
fetchMetadata: fetchMetadata,
createMetadataSchemeCld: createMetadataSchemeCld
};
19 changes: 19 additions & 0 deletions cartridges/int_cloudinary/steptypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@
"@required": false
}]
}
},
{
"@type-id": "custom.cloudinary.Structured.Metadata",
"@supports-parallel-execution": "false",
"@supports-site-context": "true",
"@supports-organization-context": "false",
"description": "Create the essential structured metadata in Cloudinary.",
"module": "int_cloudinary/cartridge/scripts/jobs/steps/cldStructuredMetadata.js",
"function": "Start",
"transactional": "false",
"parameters": {
"parameter": [
{
"@name": "Disabled",
"description": "Mark the step as disabled. This will skip the step and returns a OK status",
"@type": "boolean",
"@required": false
}]
}
}
]
}
Expand Down
Binary file modified documentation/Cloudinary SFRA LINK Documentation 1.0.0.docx
Binary file not shown.
Binary file not shown.
Binary file added metadata/Sample content/cloudinary-metadata.zip
Binary file not shown.

0 comments on commit fa6befc

Please sign in to comment.