|
| 1 | +/* eslint-disable no-param-reassign, no-unused-vars, class-methods-use-this */ |
| 2 | +const { messages } = require('elasticio-node'); |
| 3 | +const { Upsert } = require('@elastic.io/oih-standard-library/lib/actions/upsert'); |
| 4 | +const { AttachmentProcessor } = require('@elastic.io/component-commons-library'); |
| 5 | +const { callJSForceMethod } = require('../helpers/wrapper'); |
| 6 | +const { createProperty, getLookupFieldsModelWithTypeOfSearch } = require('../helpers/utils'); |
| 7 | + |
| 8 | +module.exports.getObjectTypes = async function getObjectTypes(configuration) { |
| 9 | + return callJSForceMethod.call(this, configuration, 'getObjectTypes'); |
| 10 | +}; |
| 11 | + |
| 12 | +module.exports.getLookupFieldsModel = async function getLookupFieldsModel(configuration) { |
| 13 | + const meta = await callJSForceMethod.call(this, configuration, 'describe'); |
| 14 | + return getLookupFieldsModelWithTypeOfSearch(meta, configuration.typeOfSearch); |
| 15 | +}; |
| 16 | + |
| 17 | +module.exports.getMetaModel = async function getMetaModel(configuration) { |
| 18 | + const meta = await callJSForceMethod.call(this, configuration, 'describe'); |
| 19 | + const fields = meta.fields.filter((field) => !field.deprecatedAndHidden |
| 20 | + && field.updateable |
| 21 | + && field.createable); |
| 22 | + |
| 23 | + const result = { |
| 24 | + in: { |
| 25 | + type: 'object', |
| 26 | + properties: {}, |
| 27 | + }, |
| 28 | + out: { |
| 29 | + type: 'object', |
| 30 | + properties: { |
| 31 | + id: { |
| 32 | + type: 'string', |
| 33 | + required: true, |
| 34 | + title: 'Unique identifier from CRM', |
| 35 | + }, |
| 36 | + success: { |
| 37 | + type: 'boolean', |
| 38 | + required: true, |
| 39 | + title: 'Request result', |
| 40 | + }, |
| 41 | + errors: { |
| 42 | + type: 'array', |
| 43 | + required: true, |
| 44 | + title: 'Founded errors', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + result.in.properties[configuration.lookupField] = { |
| 51 | + type: 'string', |
| 52 | + title: `lookup by - ${configuration.lookupField}`, |
| 53 | + required: configuration.lookupField !== 'Id', |
| 54 | + }; |
| 55 | + |
| 56 | + fields.forEach((field) => { |
| 57 | + result.in.properties[field.name] = createProperty(field); |
| 58 | + if (field.type === 'base64') result.in.properties[field.name].title += ' - use URL to file'; |
| 59 | + }); |
| 60 | + return result; |
| 61 | +}; |
| 62 | + |
| 63 | +class UpsertObject extends Upsert { |
| 64 | + constructor(context) { |
| 65 | + super(context); |
| 66 | + this.logger = context.logger; |
| 67 | + } |
| 68 | + |
| 69 | + getCriteria(_msg, _cfg) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + async lookupObject(_criteria, _type, cfg, msg) { |
| 74 | + let existingObj; |
| 75 | + if (cfg.lookupField === 'Id' && (!msg.body.Id)) { |
| 76 | + existingObj = undefined; |
| 77 | + } else { |
| 78 | + existingObj = await callJSForceMethod.call(this, cfg, 'sobjectLookup', msg); |
| 79 | + if (existingObj.length > 1) { |
| 80 | + throw new Error('Found more than 1 Object'); |
| 81 | + } else if (existingObj.length === 0) { |
| 82 | + existingObj = undefined; |
| 83 | + } |
| 84 | + } |
| 85 | + return existingObj; |
| 86 | + } |
| 87 | + |
| 88 | + async getObjectFromMessage(msg, cfg) { |
| 89 | + const meta = await callJSForceMethod.call(this, cfg, 'describe'); |
| 90 | + await meta.fields.forEach(async (field) => { |
| 91 | + if (field.type === 'base64') { |
| 92 | + if (msg.body[field.name]) { |
| 93 | + const attachmentProcessor = new AttachmentProcessor(); |
| 94 | + const attachment = await attachmentProcessor.getAttachment(msg.body[field.name], 'arraybuffer'); |
| 95 | + msg.body[field.name] = attachment.data.toString('base64'); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + return msg; |
| 100 | + } |
| 101 | + |
| 102 | + async updateObject(_criteria, _type, object, cfg, _msg, existingObject) { |
| 103 | + object.body.Id = existingObject[0].Id; |
| 104 | + const result = await callJSForceMethod.call(this, cfg, 'sobjectUpdate', object); |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + async createObject(object, cfg, _msg) { |
| 109 | + const result = callJSForceMethod.call(this, cfg, 'sobjectCreate', object); |
| 110 | + return result; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +module.exports.process = async function upsertObject(msg, cfg) { |
| 115 | + const upsert = new UpsertObject(this); |
| 116 | + return upsert.process(msg, cfg, {}); |
| 117 | +}; |
0 commit comments