Howto validate reservation number in additional fields with extension #1372
-
Hello, What I would like to achieve: Create ticket 'parings'. What I started doing: Problems:
Questions:
Thanks in advance for an answer and some more insights on how to write an extension. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @SilentResonance , thank you for using alf.io! Extensions run in a controlled environment (a sandbox) and cannot query / update the underlying database. That being said, what you can do is:
From what I understand, you need to access / validate additional fields, not additional services. here's an example of a working script: var ticketIds = Object.keys(form.tickets);
for(var i=0; i < ticketIds.length; i++) {
var ticketId = ticketIds[i];
log.info("validating ticket {}", ticketId);
var ticket = form.tickets[ticketId];
if(ticket.additional["fieldName"]) {
//... your checks here
// in case of a failed validation, you can reject the field content
bindingResult.rejectValue("tickets['"+ticketId+"'].additional['"+fieldName+"'][0]", 'error.restrictedValue', null);
}
}
this is not possible at the moment.
You don't need to notify a successful validation. That's the default (happy) path. Celestino |
Beta Was this translation helpful? Give feedback.
-
For someone trying to build something similar, here ist my now working solution: function executeScript(scriptEvent) {
var ticketIds = Object.keys(form.tickets);
var validationFailure = 0;
log.info('hello from script with event: ' + scriptEvent);
log.info('extension parameters are: ' + extensionParameters);
log.info('the reservation number is: ' + reservationId);
for(var i=0; i < ticketIds.length; i++) {
var ticketId = ticketIds[i];
log.info("validating ticket {}", ticketId);
var ticket = form.tickets[ticketId];
if(ticket.additional !== null){
var additionalFields = Object.keys(ticket.additional);
for(var j=0; j<additionalFields.length; j++) {
var fieldName = additionalFields[j];
//console.log("Field: ", ticket.additional[fieldName]);
if(fieldName === 'partner_latin' || fieldName === 'partner_standard') {
if(ticket.additional[fieldName].toString() == '[null]' || ticket.additional[fieldName].isEmpty() || ticket.additional[fieldName].toString() == '[]') {
console.log("Empty: ", fieldName);
} else {
log.info(ticket.additional[fieldName].toString());
//log.info("Partner: {}",fieldName,ticket.additional[fieldName]);
// SQL Query: Select split_part(id, '-', 1) as reservation from tickets_reservation where id like '" + ticket.additional[fieldName][0].toLowerCase() + "%';
var reservation = simpleHttpClient.get("http://postgrest:3000/tickets_reservation?select=id&id=ilike." + ticket.additional[fieldName][0] + "%25");
//console.log('Reservation response', reservation.getJsonBody());
if(reservation.getJsonBody()[0] === undefined) {
console.log('No matching reservation found');
bindingResult.rejectValue("tickets['"+ticketId+"'].additional['"+fieldName+"'][0]", 'error.restrictedValue', null);
break;
} else {
console.log('reservation found:', reservation.getJsonBody()[0]['id'].split("-")[0]);
if(reservation.getJsonBody()[0]['id'].split("-")[0] != ticket.additional[fieldName][0].toLowerCase()) {
bindingResult.rejectValue("tickets['"+ticketId+"'].additional['"+fieldName+"'][0]", 'error.restrictedValue', null);
}
}
}
}
}
}
}
} Any comments to shorten the code are appreciated. I don't actually like to compare with toString() to detect null values. |
Beta Was this translation helpful? Give feedback.
For someone trying to build something similar, here ist my now working solution: