Skip to content

Commit

Permalink
Ajoute la demande de numéro de sécu pour le mandat SMS (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
niladic authored Apr 20, 2021
1 parent eae174f commit d4a58c6
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 142 deletions.
4 changes: 2 additions & 2 deletions app/services/SmsService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class SmsService @Inject() (
s"En répondant OUI, vous attestez sur l'honneur que " +
s"les informations communiquées ($usagerInfos) sont exactes " +
s"et vous autorisez $userInfos$groupInfos, à utiliser vos données personnelles " +
s"pour la durée d'instruction de votre demande. " +
s"Conformément aux CGU d'Administration+."
"dont votre numéro de sécurité sociale si nécessaire " +
s"pour la durée d'instruction de votre demande."
api.sendSms(body, recipient)
}

Expand Down
138 changes: 0 additions & 138 deletions public/javascripts/main-page-bottom.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,145 +120,7 @@ function setupApplicationForm() {

setupApplicationForm();

//
// SMS Mandat Card
//

var mandatSmsPhoneInputName = "mandat-sms-phone"
var mandatSmsSendButtonId = "mandat-sms-send-button"
var mandatSmsSuccessId = "mandat-sms-success"
var mandatSmsValidationFailedId = "mandat-sms-validation-failed"
var mandatSmsErrorServerId = "mandat-sms-error-server"
var mandatSmsErrorBrowserId = "mandat-sms-error-browser"
var linkedMandatInputId = "linkedMandat"

function setupMandatSmsForm() {
var inputPrenom = document.getElementById("usagerPrenom");
var inputNom = document.getElementById("usagerNom");
var inputBirthDate = document.getElementById("usagerBirthDate");
var inputPhoneNumber = document.getElementById(mandatSmsPhoneInputName);
var sendButton = document.getElementById(mandatSmsSendButtonId);
var successMessage = document.getElementById(mandatSmsSuccessId);
var validationFailedMessage = document.getElementById(mandatSmsValidationFailedId);
var serverErrorMessage = document.getElementById(mandatSmsErrorServerId);
var browserErrorMessage = document.getElementById(mandatSmsErrorBrowserId);
var linkedMandatInput = document.getElementById(linkedMandatInputId);
var mandatTypeSmsRadio = document.getElementById("mandatType_sms");

// Returns null|string
function validateNonEmptyInput(input) {
var data = input.value;
if (data) {
input.parentNode.classList.remove("is-invalid");
return data;
} else {
input.parentNode.classList.add("is-invalid");
return null;
}
}

function validatePhoneNumber(input) {
var data = input.value.replace(/\s/g,'');
if (/^\d{10}$/.test(data)) {
inputPhoneNumber.parentNode.classList.remove("is-invalid");
return data;
} else {
inputPhoneNumber.parentNode.classList.add("is-invalid");
return null;
}
}

function validateForm() {
var prenom = validateNonEmptyInput(inputPrenom);
var nom = validateNonEmptyInput(inputNom);
var birthDate = validateNonEmptyInput(inputBirthDate);
var phoneNumber = validatePhoneNumber(inputPhoneNumber);
var isValid = prenom && nom && birthDate && phoneNumber;

return {
isValid: isValid,
data: {
prenom: prenom,
nom: nom,
birthDate: birthDate,
phoneNumber: phoneNumber
}
};
}

function sendForm(data, callbackSuccess, callbackServerError, callbackBrowserError) {
try {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4
try {
if (Math.floor(xhr.status / 100) === 2) {
callbackSuccess(JSON.parse(xhr.responseText));
} else {
callbackServerError();
}
} catch (error) {
console.error(error);
callbackBrowserError(error);
}
}
};
xhr.open("POST", "/mandats/sms", true);
xhr.setRequestHeader("Content-Type", "application/json");
// Play recommends putting the CSRF token, even for AJAX request
// and cites browser plugins as culprits
// https://www.playframework.com/documentation/2.8.x/ScalaCsrf#Plays-CSRF-protection
var token = document.querySelector("input[name=csrfToken]").value;
xhr.setRequestHeader("Csrf-Token", token);
xhr.send(JSON.stringify(data));
} catch (error) {
console.error(error);
callbackBrowserError(error);
}
}

if (sendButton) {
sendButton.onclick = function(event) {
event.preventDefault();
successMessage.classList.add("hidden");
validationFailedMessage.classList.add("hidden");
serverErrorMessage.classList.add("hidden");
browserErrorMessage.classList.add("hidden");
var formData = validateForm();
if (formData.isValid) {
sendButton.disabled = true;
sendForm(
formData.data,
// Success
function(mandat) {
var link = successMessage.querySelector("a");
link.href = "/mandats/" + mandat.id;
linkedMandatInput.value = mandat.id;
successMessage.classList.remove("hidden");
// Note: mandatTypeSmsRadio.checked = true does not show the radio as checked
mandatTypeSmsRadio.click();
},
// Server error (= logged by Sentry)
function() {
// Wait 30s
setTimeout(function() { sendButton.disabled = false; }, 30000);
serverErrorMessage.classList.remove("hidden");
},
// Browser error (= not logged by Sentry)
function() {
// Wait 30s
setTimeout(function() { sendButton.disabled = false; }, 30000);
browserErrorMessage.classList.remove("hidden");
}
);
} else {
validationFailedMessage.classList.remove("hidden");
}
}
}
}

setupMandatSmsForm();

//
// Dialog
Expand Down
155 changes: 153 additions & 2 deletions typescript/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ const usagerInfosInputClass = 'aplus-application-form-user-infos-input';
const categoryFilterClass = 'aplus-application-form-category-filter-button';
const removeCategoryFilterClass = 'aplus-application-form-remove-category-filter-button';

const inputPrenomId = "usagerPrenom";
const inputNomId = "usagerNom";
const inputBirthDateId = "usagerBirthDate";
const mandatTypeSmsRadioId = "mandatType_sms";

const mandatSmsPhoneInputName = "mandat-sms-phone";
const mandatSmsSendButtonId = "mandat-sms-send-button";
const mandatSmsSuccessId = "mandat-sms-success";
const mandatSmsValidationFailedId = "mandat-sms-validation-failed";
const mandatSmsErrorServerId = "mandat-sms-error-server";
const mandatSmsErrorBrowserId = "mandat-sms-error-browser";
const linkedMandatInputId = "linkedMandat";



setupDynamicUsagerInfosButtons();
setupInvitedGroups();
setupMandatSmsForm();



function addInvitedGroupInfos(groupName: string) {
Expand Down Expand Up @@ -250,5 +269,137 @@ function setupInvitedGroups() {



setupDynamicUsagerInfosButtons();
setupInvitedGroups();
//
// SMS Mandat Card
//

function setupMandatSmsForm() {
const inputPrenom = <HTMLInputElement>document.getElementById(inputPrenomId);
const inputNom = <HTMLInputElement>document.getElementById(inputNomId);
const inputBirthDate = <HTMLInputElement>document.getElementById(inputBirthDateId);
const inputPhoneNumber = <HTMLInputElement>document.getElementById(mandatSmsPhoneInputName);

const sendButton = <HTMLButtonElement | null>document.getElementById(mandatSmsSendButtonId);
const successMessage = document.getElementById(mandatSmsSuccessId);
const validationFailedMessage = document.getElementById(mandatSmsValidationFailedId);
const serverErrorMessage = document.getElementById(mandatSmsErrorServerId);
const browserErrorMessage = document.getElementById(mandatSmsErrorBrowserId);
const linkedMandatInput = <HTMLInputElement>document.getElementById(linkedMandatInputId);
const mandatTypeSmsRadio = document.getElementById(mandatTypeSmsRadioId);


// Returns null|string
function validateNonEmptyInput(input: HTMLInputElement) {
const data = input.value;
const parent = <HTMLElement>input.parentNode;
if (data) {
parent.classList.remove("is-invalid");
return data;
} else {
parent.classList.add("is-invalid");
return null;
}
}

function validatePhoneNumber(input: HTMLInputElement) {
const data = input.value.replace(/\s/g, '');
const parent = <HTMLElement>inputPhoneNumber.parentNode;
if (/^\d{10}$/.test(data)) {
parent.classList.remove("is-invalid");
return data;
} else {
parent.classList.add("is-invalid");
return null;
}
}

function validateForm() {
const prenom = validateNonEmptyInput(inputPrenom);
const nom = validateNonEmptyInput(inputNom);
const birthDate = validateNonEmptyInput(inputBirthDate);
const phoneNumber = validatePhoneNumber(inputPhoneNumber);
const isValid = prenom && nom && birthDate && phoneNumber;

return {
isValid: isValid,
data: {
prenom: prenom,
nom: nom,
birthDate: birthDate,
phoneNumber: phoneNumber
}
};
}

function sendForm(data, callbackSuccess, callbackServerError, callbackBrowserError) {
try {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4
try {
if (Math.floor(xhr.status / 100) === 2) {
callbackSuccess(JSON.parse(xhr.responseText));
} else {
callbackServerError();
}
} catch (error) {
console.error(error);
callbackBrowserError(error);
}
}
};
xhr.open("POST", "/mandats/sms", true);
xhr.setRequestHeader("Content-Type", "application/json");
// Play recommends putting the CSRF token, even for AJAX request
// and cites browser plugins as culprits
// https://www.playframework.com/documentation/2.8.x/ScalaCsrf#Plays-CSRF-protection
const tokenInput = <HTMLInputElement>document.querySelector("input[name=csrfToken]");
const token = tokenInput.value;
xhr.setRequestHeader("Csrf-Token", token);
xhr.send(JSON.stringify(data));
} catch (error) {
console.error(error);
callbackBrowserError(error);
}
}

if (sendButton) {
sendButton.onclick = function(event) {
event.preventDefault();
successMessage.classList.add("hidden");
validationFailedMessage.classList.add("hidden");
serverErrorMessage.classList.add("hidden");
browserErrorMessage.classList.add("hidden");
const formData = validateForm();
if (formData.isValid) {
sendButton.disabled = true;
sendForm(
formData.data,
// Success
function(mandat) {
const link = successMessage.querySelector("a");
link.href = "/mandats/" + mandat.id;
linkedMandatInput.value = mandat.id;
successMessage.classList.remove("hidden");
// Note: mandatTypeSmsRadio.checked = true does not show the radio as checked
mandatTypeSmsRadio.click();
},
// Server error (= logged by Sentry)
function() {
// Wait 30s
setTimeout(function() { sendButton.disabled = false; }, 30000);
serverErrorMessage.classList.remove("hidden");
},
// Browser error (= not logged by Sentry)
function() {
// Wait 30s
setTimeout(function() { sendButton.disabled = false; }, 30000);
browserErrorMessage.classList.remove("hidden");
}
);
} else {
validationFailedMessage.classList.remove("hidden");
}
}
}
}

0 comments on commit d4a58c6

Please sign in to comment.