n8n-nodes-smartsuite
Version:
n8n community node for SmartSuite
66 lines • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute = execute;
const n8n_workflow_1 = require("n8n-workflow");
const utils_1 = require("../../helpers/utils");
const smartSuiteApi_1 = require("../../transport/smartSuiteApi");
const validation_1 = require("../../helpers/validation");
async function execute() {
// Get the table ID (application ID)
const tableId = await validation_1.getTableId.call(this, 0);
(0, utils_1.debugLog)('[UpsertRecord] execute', { tableId }, 2);
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
// 1) Matching criteria
const matchingField = this.getNodeParameter('matchingField', i);
const condition = this.getNodeParameter('condition', i);
const matchValue = this.getNodeParameter('matchValue', i);
if (!matchingField || !condition || !matchValue) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Matching Field, Condition, and Match Value are required for Upsert Record', { itemIndex: i });
}
// 2) Fields to upsert
const fieldsArr = this.getNodeParameter('fieldsUiUpdate.fieldsValues', i, []);
if (!fieldsArr.length) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'You must provide at least one field to upsert', { itemIndex: i });
}
// 3) Prevent reserved fields
for (const { field } of fieldsArr) {
const slug = (0, utils_1.asIdString)(field);
if ((0, utils_1.isReservedField)(slug)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Cannot upsert system field “${slug}”.`, { itemIndex: i });
}
}
// 4) Build filter for existing-record lookup
const filter = {
operator: 'and',
fields: [{ field: matchingField, comparison: condition, value: matchValue }],
};
(0, utils_1.debugLog)('[UpsertRecord] searching for existing record', filter, 3);
// 5) List with limit=1 to find an existing record
const query = { limit: 1, offset: 0 };
const listResponse = (await smartSuiteApi_1.apiRequest.call(this, 'POST', `/applications/${tableId}/records/list/`, { filter }, query));
const results = listResponse.items || [];
// 6) Prepare payload
const payload = fieldsArr.reduce((obj, { field, value }) => {
obj[(0, utils_1.asIdString)(field)] = value;
return obj;
}, {});
let response;
if (results.length > 0) {
// 7a) Update existing
const existingId = results[0].id;
(0, utils_1.debugLog)(`[UpsertRecord] updating record ${existingId}`, payload, 2);
response = (await smartSuiteApi_1.apiRequest.call(this, 'PATCH', `/applications/${tableId}/records/${existingId}/`, payload));
}
else {
// 7b) Create new
(0, utils_1.debugLog)('[UpsertRecord] creating new record', payload, 2);
response = (await smartSuiteApi_1.apiRequest.call(this, 'POST', `/applications/${tableId}/records/`, payload));
}
(0, utils_1.debugLog)('[UpsertRecord] response', response, 2);
returnData.push(...this.helpers.returnJsonArray([response]));
}
return returnData;
}
//# sourceMappingURL=upsertRecord.operation.js.map