|
1 |
| -/* eslint-disable no-param-reassign, no-unused-vars, class-methods-use-this */ |
| 1 | +/* eslint-disable no-param-reassign, no-unused-vars, class-methods-use-this, no-restricted-syntax */ |
2 | 2 | const { messages } = require('elasticio-node');
|
3 |
| -const { Upsert } = require('@elastic.io/oih-standard-library/lib/actions/upsert'); |
4 | 3 | const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
|
5 | 4 | const { callJSForceMethod } = require('../helpers/wrapper');
|
6 | 5 | const { createProperty, getLookupFieldsModelWithTypeOfSearch } = require('../helpers/utils');
|
@@ -51,69 +50,55 @@ module.exports.getMetaModel = async function getMetaModel(configuration) {
|
51 | 50 |
|
52 | 51 | result.in.properties[configuration.lookupField] = {
|
53 | 52 | type: 'string',
|
54 |
| - title: `lookup by - ${configuration.lookupField}`, |
55 |
| - required: configuration.lookupField !== 'Id', |
| 53 | + title: configuration.lookupField, |
| 54 | + required: false, |
56 | 55 | };
|
57 | 56 |
|
58 | 57 | fields.forEach((field) => {
|
59 | 58 | result.in.properties[field.name] = createProperty(field);
|
60 | 59 | if (field.type === 'base64') result.in.properties[field.name].title += ' - use URL to file';
|
61 | 60 | });
|
| 61 | + |
| 62 | + result.in.properties[configuration.lookupField].title = `lookup by - ${result.in.properties[configuration.lookupField].title}`; |
| 63 | + |
62 | 64 | return result;
|
63 | 65 | };
|
64 | 66 |
|
65 |
| -class UpsertObject extends Upsert { |
66 |
| - constructor(context) { |
67 |
| - super(context); |
68 |
| - this.logger = context.logger; |
69 |
| - } |
| 67 | +let meta; |
70 | 68 |
|
71 |
| - getCriteria(_msg, _cfg) { |
72 |
| - return true; |
73 |
| - } |
| 69 | +module.exports.process = async function upsertObject(msg, cfg) { |
| 70 | + this.logger.info(`Starting processing Upsert "${cfg.sobject}" object action`); |
74 | 71 |
|
75 |
| - async lookupObject(_criteria, _type, cfg, msg) { |
76 |
| - let existingObj; |
77 |
| - if (cfg.lookupField === 'Id' && (!msg.body.Id)) { |
78 |
| - existingObj = undefined; |
79 |
| - } else { |
80 |
| - existingObj = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectLookup', msg); |
81 |
| - if (existingObj.length > 1) { |
82 |
| - throw new Error('Found more than 1 Object'); |
83 |
| - } else if (existingObj.length === 0) { |
84 |
| - existingObj = undefined; |
85 |
| - } |
| 72 | + if (!meta) meta = await callJSForceMethod.call(this, cfg, 'describe'); |
| 73 | + for (const field in meta.fields) { |
| 74 | + if (field.type === 'base64' && msg.body[field.name]) { |
| 75 | + const { data } = await new AttachmentProcessor().getAttachment(msg.body[field.name], 'arraybuffer'); |
| 76 | + msg.body[field.name] = data.toString('base64'); |
86 | 77 | }
|
87 |
| - return existingObj; |
88 | 78 | }
|
89 | 79 |
|
90 |
| - async getObjectFromMessage(msg, cfg) { |
91 |
| - const meta = await callJSForceMethod.call(this, cfg, 'describe'); |
92 |
| - await meta.fields.forEach(async (field) => { |
93 |
| - if (field.type === 'base64') { |
94 |
| - if (msg.body[field.name]) { |
95 |
| - const attachmentProcessor = new AttachmentProcessor(); |
96 |
| - const attachment = await attachmentProcessor.getAttachment(msg.body[field.name], 'arraybuffer'); |
97 |
| - msg.body[field.name] = attachment.data.toString('base64'); |
98 |
| - } |
99 |
| - } |
100 |
| - }); |
101 |
| - return msg; |
102 |
| - } |
103 |
| - |
104 |
| - async updateObject(_criteria, _type, object, cfg, _msg, existingObject) { |
105 |
| - object.body.Id = existingObject[0].Id; |
106 |
| - const result = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectUpdate', object); |
107 |
| - return result; |
108 |
| - } |
109 |
| - |
110 |
| - async createObject(object, cfg, _msg) { |
111 |
| - const result = callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectCreate', object); |
112 |
| - return result; |
| 80 | + let result; |
| 81 | + if (!msg.body[cfg.lookupField]) { |
| 82 | + this.logger.info(`"${cfg.lookupField}" not specified, creating new object`); |
| 83 | + result = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectCreate', msg); |
| 84 | + } else if (cfg.typeOfSearch === 'externalIds') { |
| 85 | + this.logger.info(`Going to upsert "${cfg.sobject}" object using "${cfg.lookupField}" field`); |
| 86 | + const extIdField = cfg.lookupField; |
| 87 | + result = await callJSForceMethod.call(this, { ...cfg, timeOut, extIdField }, 'sobjectUpsert', msg); |
| 88 | + } else { |
| 89 | + this.logger.info('Starting to get object for an upsert...'); |
| 90 | + const existingObj = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectLookup', msg); |
| 91 | + if (existingObj.length > 1) { |
| 92 | + throw new Error('Found more than 1 Object'); |
| 93 | + } else if (existingObj.length === 0) { |
| 94 | + this.logger.info('An object does not exist, creating a new one'); |
| 95 | + result = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectCreate', msg); |
| 96 | + } else { |
| 97 | + this.logger.info('An object already exists, updating it'); |
| 98 | + msg.body.Id = existingObj[0].Id; |
| 99 | + result = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectUpdate', msg); |
| 100 | + } |
113 | 101 | }
|
114 |
| -} |
115 | 102 |
|
116 |
| -module.exports.process = async function upsertObject(msg, cfg) { |
117 |
| - const upsert = new UpsertObject(this); |
118 |
| - return upsert.process(msg, cfg, {}); |
| 103 | + return messages.newMessageWithBody(result); |
119 | 104 | };
|
0 commit comments