diff --git a/src/helpers/kafkaUtil.js b/src/helpers/kafkaUtil.js new file mode 100644 index 0000000000000000000000000000000000000000..88ea86aabe391139ee1343222946526bbbc2df32 --- /dev/null +++ b/src/helpers/kafkaUtil.js @@ -0,0 +1,43 @@ +const kafka = require('kafka-node') +const _ = require('lodash') +var logger = require('sb_logger_util_v2') +const envVariables = require("../envVariables"); + +const client = new kafka.KafkaClient({ + kafkaHost: envVariables.SUNBIRD_KAFKA_HOST, + maxAsyncRequests: 100 +}) + +const producer = new kafka.HighLevelProducer(client) +producer.on('ready', function () { + console.log('Kafka Producer is connected and ready.') + logger.info({msg: 'Kafka Producer is connected and ready.'}) +}) + +// For this demo we just log producer errors to the console. +producer.on('error', function (error) { + logger.error({msg: 'Error from Kafka producer', error}) + console.error(error) +}) + +const KafkaService = { + sendRecord: (data, callback = () => { }) => { + if (_.isEmpty(data)) { + logger.error({msg: 'Data must be provided to send Record', additionalInfo: {data}}) + return callback(new Error('Event Data must be provided.')) + } + + // Create a new payload + const record = [ + { + topic: envVariables.SUNBIRD_AUTO_CREATION_TOPIC, + messages: JSON.stringify(data) + } + ] + logger.info({msg: 'Kafka record', additionalInfo: {record}}) + // Send record to Kafka and log result/error + producer.send(record, callback) + } +} + +module.exports = KafkaService \ No newline at end of file diff --git a/src/helpers/publishHelper.js b/src/helpers/publishHelper.js new file mode 100644 index 0000000000000000000000000000000000000000..305ac824d95aa131dbc27eaa9cfd8fa9ee0ec5c4 --- /dev/null +++ b/src/helpers/publishHelper.js @@ -0,0 +1,72 @@ +const uuid = require("uuid/v1") +const _ = require("lodash"); +const envVariables = require("../envVariables"); +const axios = require("axios"); +const { from } = require("rxjs"); + +function getContentMetaData(contentId, reqHeaders){ + const url = `${envVariables.baseURL}/action/content/v3/read/${contentId}`; + const option = { + url: url, + method: "get", + headers: reqHeaders + }; + return from(axios(option)); +} + +function getPublishContentEvent(metadata, textbookId, units) { + metadata.pkgVersion = `${metadata.pkgVersion}.0`; + if(metadata.subject){ + metadata.subject = _.isArray(metadata.subject) ? metadata.subject : [metadata.subject]; + } + if(metadata.medium){ + metadata.medium = _.isArray(metadata.medium) ? metadata.medium : [metadata.medium]; + } + metadata = _.omit(metadata, [ + "downloadUrl", + "variants", + "previewUrl", + "streamingUrl", + "unitIdentifiers", + "itemSets" + ]); + var ets = Date.now(); + var dataObj = { + 'eid': 'BE_JOB_REQUEST', + 'ets': ets, + 'mid': `LP.${ets}.${uuid()}`, + 'actor': { + 'id': 'Auto Creator', + 'type': 'System' + }, + 'context': { + 'pdata': { + 'ver': '1.0', + 'id': 'org.ekstep.platform' + }, + 'channel': metadata.channel, + 'env': envVariables.PUBLISH_ENV + }, + 'object': { + 'ver': '1.0', + 'id': metadata.identifier + }, + 'edata': { + 'action': 'auto-create', + 'iteration': 1, + 'objectType': 'Content', + 'repository': `${envVariables.baseURL}/api/content/v1/read/${metadata.identifier}`, + 'metadata': metadata, + 'textbookInfo': { + 'identifier': textbookId, + 'unitIdentifiers': units + } + } + } + + return dataObj; + } + + +module.exports.getPublishContentEvent = getPublishContentEvent +module.exports.getContentMetaData = getContentMetaData \ No newline at end of file