Skip to content

Commit

Permalink
Add unique constraint to identifier column in createUserTable functio…
Browse files Browse the repository at this point in the history
…n and handle error in add function
  • Loading branch information
Arikkusan committed Mar 21, 2024
1 parent cb3ced4 commit 801d334
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion API/data/TableCreation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function createUserTable(query) {
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
identifier VARCHAR(255) NOT NULL,
identifier VARCHAR(255) NOT NULL UNIQUE,
archived_date Datetime default NULL
)`;

Expand Down
19 changes: 9 additions & 10 deletions API/model/Managers/UserManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ async function add(user) {
assert(user.email, '[UserManager.add] The email is required');
assert(/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(user.email), '[UserManager.add] The email is not valid');

return await UserQueries.add(user);
try {
return await UserQueries.add(user);
} catch (e) {
return e;
}

}

Expand Down Expand Up @@ -175,12 +179,11 @@ async function unarchive(id) {
*/
async function generateSigningToken(data, documentId) {

let userId;
const user = await UserQueries.getByEmail(data.email);

// check for the user / add it if needed
if (!await UserQueries.getByEmail(data.email)) {
userId = await UserQueries.add(data);
}
assert(user, '[UserManager.generateSigningToken] The user is required'); // may check the addUser from the front

const userId = user.id;

assert(data.email, '[UserManager.generateSigningToken] The email is required');
assert(documentId, '[UserManager.generateSigningToken] The documentId is required');
Expand All @@ -193,10 +196,6 @@ async function generateSigningToken(data, documentId) {
token = utils.generateRandomToken(5, false);
}

// Get the user and version IDs
if (!userId) {
userId = (await UserQueries.getByEmail(data.email)).id;
}
const versionId = (await VersionQueries.getLatest(documentId)).id;

// Add the token to the database
Expand Down
5 changes: 4 additions & 1 deletion FRONT/js/model/popups/SigningLinkPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ class SigningLinkPopup extends Popup {

// actions to be taken when the popup is clicked
const actions = {
'signing-link-copy': async (_, link) => await Utils.copyToClipboard(link),
'signing-link-copy': async (_, link) => {
await Utils.copyToClipboard(link)
alert('Lien copié dans le presse-papier');
},
'signing-link-mailto': async (user, link) => {
// send the mail
window.location.href = `mailto:${user.identifier}?subject=Signature du document ${docName}&body=Bonjour ${user.first_name} ${user.last_name},%0D%0AMerci de signer ce document à l'adresse suivante : ${link}.%0D%0ACordialement.`;
Expand Down

0 comments on commit 801d334

Please sign in to comment.