Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: process submit #158

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/imports/data/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export async function processSubmit({ authTokenId, data, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {},
};

if (isArray(data === false)) {
Expand Down Expand Up @@ -134,12 +135,13 @@ 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;
}

result.processData = extend(result.processData, piecesReturn[piece.name].processData);
result.changes = extend(result.changes, piecesReturn[piece.name].changes);
});

if (result.errors.length === 0) {
Expand All @@ -154,6 +156,7 @@ export async function processGeneric({ document, name, data, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {},
};

const createRequest = {
Expand Down Expand Up @@ -183,6 +186,7 @@ export async function processGeneric({ document, name, data, contextUser }) {
}

response.processData[name] = saveResult.data[0];
response.changes[document] = "created";

return response;
}
Expand All @@ -192,6 +196,7 @@ export async function processCampaignTarget({ data, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {},
};

if (MetaObject.Namespace.skipCampaignTargetForActiveOpportunities === true) {
Expand Down Expand Up @@ -246,6 +251,7 @@ export async function processCampaignTarget({ data, contextUser }) {
}

response.processData['campaignTarget'] = saveResult.data[0];
response.changes['campaignTarget'] = 'created';

return response;
}
Expand All @@ -256,6 +262,7 @@ export async function processOpportunity({ data, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {},
};

let record = await find({
Expand Down Expand Up @@ -294,6 +301,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();
Expand Down Expand Up @@ -479,6 +487,7 @@ export async function processOpportunity({ data, contextUser }) {
}

response.processData['opportunity'] = saveResult.data[0];
response.changes['opportunity'] = 'created';

opportunityId = saveResult.data[0]._id;
}
Expand Down Expand Up @@ -569,6 +578,7 @@ export async function processOpportunity({ data, contextUser }) {
response.processData['productsPerOpportunities'] = [];
}

response.changes['productsPerOpportunities'] = 'created';
return response.processData['productsPerOpportunities'].push(saveProductResult.data[0]);
}
});
Expand All @@ -587,6 +597,7 @@ export async function processMessage({ data, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {}
};

const campaign = await findCampaign(data.campaign, contextUser);
Expand Down Expand Up @@ -625,6 +636,7 @@ export async function processMessage({ data, contextUser }) {
}

response.processData['message'] = saveResult.data[0];
response.changes['message'] = 'created';

return response;
}
Expand All @@ -634,6 +646,7 @@ export async function processActivity({ data, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {},
};

if (has(data, 'campaign.code') && !has(data, 'campaign._id')) {
Expand Down Expand Up @@ -680,6 +693,7 @@ export async function processActivity({ data, contextUser }) {
}

response.processData['activity'] = saveResult.data[0];
response.changes['activity'] = 'created';

return response;
}
Expand Down Expand Up @@ -723,6 +737,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

Expand All @@ -749,6 +764,7 @@ export async function processContact({ data, options, contextUser }) {
success: true,
processData: {},
errors: [],
changes: {},
};

let codeSent = false;
Expand All @@ -757,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);
}

Expand All @@ -769,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.' }];
7sete7 marked this conversation as resolved.
Show resolved Hide resolved
delete response.processData;
return response;
}
Expand All @@ -791,7 +807,7 @@ export async function processContact({ data, options, contextUser }) {
],
},
limit: 1,
contextUser,
contextUser: adminUser,
});

if (has(record, 'data.0')) {
Expand All @@ -814,7 +830,7 @@ export async function processContact({ data, options, contextUser }) {
],
},
limit: 1,
contextUser,
contextUser: adminUser,
});

if (has(record, 'data.0')) {
Expand Down Expand Up @@ -843,7 +859,7 @@ export async function processContact({ data, options, contextUser }) {
],
},
limit: 1,
contextUser,
contextUser: adminUser,
});

if (has(record, 'data.0')) {
Expand All @@ -865,7 +881,7 @@ export async function processContact({ data, options, contextUser }) {
],
},
limit: 1,
contextUser,
contextUser: adminUser,
});

if (has(record, 'data.0')) {
Expand Down Expand Up @@ -1069,7 +1085,7 @@ export async function processContact({ data, options, contextUser }) {
},
],
fields: '_id, _user',
contextUser,
contextUser: adminUser,
});

some(get(record, 'data.0._user'), userFromOpportunity => {
Expand Down Expand Up @@ -1120,7 +1136,7 @@ export async function processContact({ data, options, contextUser }) {
},
],
fields: '_id, _user',
contextUser,
contextUser: adminUser,
});

some(get(record, 'data.0._user'), userFromActivity => {
Expand Down Expand Up @@ -1176,7 +1192,7 @@ export async function processContact({ data, options, contextUser }) {
],
},
fields: '_id,targetQueue',
contextUser,
contextUser: adminUser,
});

if (has(record, 'data.0.targetQueue')) {
Expand Down Expand Up @@ -1260,6 +1276,7 @@ export async function processContact({ data, options, contextUser }) {
}

result = await create(createRequest);
response.changes["contact"] = 'created';
} else if (!isEmpty(contactData)) {
const updateRequest = {
document: 'Contact',
Expand All @@ -1271,6 +1288,7 @@ export async function processContact({ data, options, contextUser }) {
};

result = await update(updateRequest);
response.changes["contact"] = 'updated';
} else {
result = {
success: true,
Expand Down