UPDATE: So I was missing the exports.checkValues function. Once this was added everything worked great. Below is what the validation before storing file looks like: (Mind you, all this does is validate that an invoice with doc type of Invoice, Invoice Number, and Vendor Number arent already in the cabinet being stored into. If there is a match, it notifies and won't let then store. Works great for not even allowing duplicate invoices in the system)
const DWparameters = require('./DWValidationSettings');
const validator = require('validator');
const unirest = require('unirest');
exports.checkValues = function (DWInputValues) {
var invoiceNo = this.getFieldValue(DWInputValues.Values, DWparameters.fieldNameDOCNUMBER);
var supplierID = this.getFieldValue(DWInputValues.Values, DWparameters.fieldNameSUPPLIER);
return new Promise((resolve, reject) => {
this.isDuplicateInvoice(DWInputValues.FileCabinetGuid, invoiceNo, supplierID)
.then(success => {
if (!success) {
throw new Error('There is already an invoice stored (#'+ invoiceNo +'SupplierID:' + supplierID + ')');
}
return success;
})
.then(success => resolve(true))
.catch(function (error){
reject(error);
})
});
}
exports.getFieldValue = function (DWIndexFieldCollection, fieldName) {
var field = DWIndexFieldCollection.find(x => x.FieldName == fieldName);
if (field === undefined) {
return;
}
return field.Item;
}
exports.isDuplicateInvoice = function (fileCabinetGUID, invoiceNo, supplierID) {
return new Promise((resolve, reject) => {
var CookieJar = unirest.jar(true);
//logon to DW PLATFORM and retrieve cookie;
unirest.post(DWparameters.DWPlatformUrl + '/Account/Logon')
.headers({'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'})
.jar(CookieJar)
.send({ 'UserName': DWparameters.DWPlatformUser, 'Password': DWparameters.DWPlatfromPassword, 'Organization': DWparameters.DWPlatformOrganization, 'RememberMe': false, 'RedirectToMyselfInCaseOfError': true })
.end(function (response) {
unirest.post(DWparameters.DWPlatformUrl + '/FileCabinets/'+ fileCabinetGUID +'/Query/DialogExpression?dialogId='+ DWparameters.DWSearchDialogGUIDForInvoiceSearch +'&format=table')
.headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
.jar(CookieJar)
.send({ 'Condition':[
{ 'DBName':DWparameters.fieldNameDOCNUMBER, 'Value': [invoiceNo] },
{ 'DBName':DWparameters.fieldNameSUPPLIER, 'Value': [supplierID, null] },
],
'SortOrder':[],'ForceRefresh':true,'FlagConditions':{'IncludeCheckedOut':false},'Operation':'And','AdditionalResultFields':[],'Start':0,'Count':1})
.end(function (response) {
if (response.error) {
return reject(new Error(response.error.message));
}
try {
var resultCount = response.body.Count.Value;
return resolve(resultCount == 0);
} catch (error) {
return reject(new Error("Unable to retrieve similar invoices. Error:" + error));
}
});
});
})
}