From 11ab0f1a9d69eeaa1ddcb1014f00ce8ddc9ef3fa Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Tue, 2 Jul 2024 18:03:01 -0300 Subject: [PATCH 1/5] feat: return object with operations performed --- src/imports/data/process.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/imports/data/process.js b/src/imports/data/process.js index 2e2ca92..be1f702 100644 --- a/src/imports/data/process.js +++ b/src/imports/data/process.js @@ -87,6 +87,7 @@ export async function processSubmit({ authTokenId, data, contextUser }) { success: true, processData: {}, errors: [], + changes: {}, }; if (isArray(data === false)) { @@ -140,6 +141,7 @@ export async function processSubmit({ authTokenId, data, contextUser }) { } result.processData = extend(result.processData, piecesReturn[piece.name].processData); + result.changes = extend(result.changes, piecesReturn[piece.name].changes); }); if (result.errors.length === 0) { @@ -183,6 +185,7 @@ export async function processGeneric({ document, name, data, contextUser }) { } response.processData[name] = saveResult.data[0]; + response.changes[document] = "created"; return response; } @@ -246,6 +249,7 @@ export async function processCampaignTarget({ data, contextUser }) { } response.processData['campaignTarget'] = saveResult.data[0]; + response.changes['campaignTarget'] = 'created'; return response; } @@ -294,6 +298,7 @@ export async function processOpportunity({ data, contextUser }) { opportunity = record.data[0]; opportunityId = record.data[0]._id; response.processData['opportunity'] = record.data[0]; + response.changes['opportunity'] = 'updated'; if (MetaObject.Namespace.alertUserOnExistingOpportunity) { const date = new Date(); @@ -479,6 +484,7 @@ export async function processOpportunity({ data, contextUser }) { } response.processData['opportunity'] = saveResult.data[0]; + response.changes['opportunity'] = 'created'; opportunityId = saveResult.data[0]._id; } @@ -569,6 +575,7 @@ export async function processOpportunity({ data, contextUser }) { response.processData['productsPerOpportunities'] = []; } + response.changes['productsPerOpportunities'] = 'created'; return response.processData['productsPerOpportunities'].push(saveProductResult.data[0]); } }); @@ -625,6 +632,7 @@ export async function processMessage({ data, contextUser }) { } response.processData['message'] = saveResult.data[0]; + response.changes['message'] = 'created'; return response; } @@ -680,6 +688,7 @@ export async function processActivity({ data, contextUser }) { } response.processData['activity'] = saveResult.data[0]; + response.changes['activity'] = 'created'; return response; } @@ -723,6 +732,7 @@ export async function processActivity({ data, contextUser }) { export async function processContact({ data, options, contextUser }) { let record, result; + const adminUser = (await MetaObject.Collections['User'].findOne({ username: 'admin' })) ?? contextUser; // const context = this; // meta = @meta @@ -791,7 +801,7 @@ export async function processContact({ data, options, contextUser }) { ], }, limit: 1, - contextUser, + contextUser: adminUser, }); if (has(record, 'data.0')) { @@ -814,7 +824,7 @@ export async function processContact({ data, options, contextUser }) { ], }, limit: 1, - contextUser, + contextUser: adminUser, }); if (has(record, 'data.0')) { @@ -843,7 +853,7 @@ export async function processContact({ data, options, contextUser }) { ], }, limit: 1, - contextUser, + contextUser: adminUser, }); if (has(record, 'data.0')) { @@ -865,7 +875,7 @@ export async function processContact({ data, options, contextUser }) { ], }, limit: 1, - contextUser, + contextUser: adminUser, }); if (has(record, 'data.0')) { @@ -1069,7 +1079,7 @@ export async function processContact({ data, options, contextUser }) { }, ], fields: '_id, _user', - contextUser, + contextUser: adminUser, }); some(get(record, 'data.0._user'), userFromOpportunity => { @@ -1120,7 +1130,7 @@ export async function processContact({ data, options, contextUser }) { }, ], fields: '_id, _user', - contextUser, + contextUser: adminUser, }); some(get(record, 'data.0._user'), userFromActivity => { @@ -1176,7 +1186,7 @@ export async function processContact({ data, options, contextUser }) { ], }, fields: '_id,targetQueue', - contextUser, + contextUser: adminUser, }); if (has(record, 'data.0.targetQueue')) { @@ -1235,6 +1245,8 @@ export async function processContact({ data, options, contextUser }) { delete contactData.referrerURL; } + let operationPerformed = 'created'; + // creates a contact if not found one if (contact == null) { const createRequest = { @@ -1260,6 +1272,7 @@ export async function processContact({ data, options, contextUser }) { } result = await create(createRequest); + response.changes["contact"] = 'created'; } else if (!isEmpty(contactData)) { const updateRequest = { document: 'Contact', @@ -1271,6 +1284,7 @@ export async function processContact({ data, options, contextUser }) { }; result = await update(updateRequest); + response.changes["contact"] = 'updated'; } else { result = { success: true, From c1dafd8047945d2e23cd77003f72648f21f30567 Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Tue, 2 Jul 2024 18:05:08 -0300 Subject: [PATCH 2/5] feat: return error source piece --- src/imports/data/process.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/imports/data/process.js b/src/imports/data/process.js index be1f702..d87a658 100644 --- a/src/imports/data/process.js +++ b/src/imports/data/process.js @@ -135,7 +135,7 @@ export async function processSubmit({ authTokenId, data, contextUser }) { if (get(piecesReturn, `${piece.name}.success`) !== true) { result.success = false; - result.errors = result.errors.concat(piecesReturn[piece.name].errors); + result.errors = result.errors.concat(piecesReturn[piece.name].errors.map(error => ({ ...error, piece: piece.name }))); logger.error(result, `Error processing piece ${piece.name}.`); return; } From fc81e4079de3e6bc552b5760777044a8552e1369 Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Tue, 2 Jul 2024 18:19:51 -0300 Subject: [PATCH 3/5] fix: expect contact phone to be a number as well --- src/imports/data/process.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/imports/data/process.js b/src/imports/data/process.js index d87a658..b0a585f 100644 --- a/src/imports/data/process.js +++ b/src/imports/data/process.js @@ -156,6 +156,7 @@ export async function processGeneric({ document, name, data, contextUser }) { success: true, processData: {}, errors: [], + changes: {}, }; const createRequest = { @@ -195,6 +196,7 @@ export async function processCampaignTarget({ data, contextUser }) { success: true, processData: {}, errors: [], + changes: {}, }; if (MetaObject.Namespace.skipCampaignTargetForActiveOpportunities === true) { @@ -260,6 +262,7 @@ export async function processOpportunity({ data, contextUser }) { success: true, processData: {}, errors: [], + changes: {}, }; let record = await find({ @@ -594,6 +597,7 @@ export async function processMessage({ data, contextUser }) { success: true, processData: {}, errors: [], + changes: {} }; const campaign = await findCampaign(data.campaign, contextUser); @@ -642,6 +646,7 @@ export async function processActivity({ data, contextUser }) { success: true, processData: {}, errors: [], + changes: {}, }; if (has(data, 'campaign.code') && !has(data, 'campaign._id')) { @@ -759,6 +764,7 @@ export async function processContact({ data, options, contextUser }) { success: true, processData: {}, errors: [], + changes: {}, }; let codeSent = false; @@ -767,7 +773,7 @@ export async function processContact({ data, options, contextUser }) { } let phoneSent = []; - if (data.phone && !isEmpty(data.phone)) { + if (data.phone && (!isEmpty(data.phone) || data.phone > 0)) { phoneSent = phoneSent.concat(data.phone); } @@ -779,7 +785,7 @@ export async function processContact({ data, options, contextUser }) { // validate if phone or email was passed if (codeSent === false && emailSent.length === 0 && phoneSent.length === 0) { response.success = false; - response.errors = [{ meaage: 'É obrigatório o preenchimento de ao menos um dos seguintes campos: code, email e telefone.' }]; + response.errors = [{ message: 'É obrigatório o preenchimento de ao menos um dos seguintes campos: code, email e telefone.' }]; delete response.processData; return response; } @@ -1245,8 +1251,6 @@ export async function processContact({ data, options, contextUser }) { delete contactData.referrerURL; } - let operationPerformed = 'created'; - // creates a contact if not found one if (contact == null) { const createRequest = { From 69acef39153eb5c884f00eb83efa5fcb56946ced Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Wed, 3 Jul 2024 11:14:29 -0300 Subject: [PATCH 4/5] fix: opportunity unchanged --- src/imports/data/process.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/imports/data/process.js b/src/imports/data/process.js index b0a585f..38a6ecc 100644 --- a/src/imports/data/process.js +++ b/src/imports/data/process.js @@ -301,7 +301,6 @@ export async function processOpportunity({ data, contextUser }) { opportunity = record.data[0]; opportunityId = record.data[0]._id; response.processData['opportunity'] = record.data[0]; - response.changes['opportunity'] = 'updated'; if (MetaObject.Namespace.alertUserOnExistingOpportunity) { const date = new Date(); From 9585ea04b370fbe423905f739a977f35ac4e7372 Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Wed, 3 Jul 2024 11:16:01 -0300 Subject: [PATCH 5/5] fix: error translation --- src/imports/data/process.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/imports/data/process.js b/src/imports/data/process.js index 38a6ecc..bd01c28 100644 --- a/src/imports/data/process.js +++ b/src/imports/data/process.js @@ -784,7 +784,7 @@ export async function processContact({ data, options, contextUser }) { // validate if phone or email was passed if (codeSent === false && emailSent.length === 0 && phoneSent.length === 0) { response.success = false; - response.errors = [{ message: 'É obrigatório o preenchimento de ao menos um dos seguintes campos: code, email e telefone.' }]; + response.errors = [{ message: 'É obrigatório o preenchimento de ao menos um dos seguintes campos: código, email e telefone.' }]; delete response.processData; return response; }