Skip to content

Commit 0f8d07e

Browse files
authored
Merge branch 'Langres-App:main' into main
2 parents 60e6441 + 9231cfb commit 0f8d07e

15 files changed

+79
-36
lines changed

API/controllers/AuthController.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
const express = require('express');
7-
const { login } = require('../data/queries/AuthorizedUserQueries');
87
const { handle } = require('./functionHandler');
98
const AuthManager = require('../model/Managers/AuthManager');
109
const router = express.Router();

API/data/queries/DocumentsQueries.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,16 @@ async function rename(id, title) {
181181
assert(id, '[DocumentsQueries.rename] The document ID is required');
182182
assert(title, '[DocumentsQueries.rename] The file name is required');
183183

184+
// get the old name for to change the folder and files names
185+
const old = await getById(id);
186+
const oldTitle = old.name;
187+
184188
// Update the document name
185189
const documentQuery = 'UPDATE document SET file_name = ? WHERE id = ?';
186190
await query(documentQuery, [title, id]);
187191

188192
// Update the version file paths
189-
await versionQueries.updatePathes(id, title);
193+
await versionQueries.updatePathes(id, oldTitle, title);
190194
});
191195

192196
}

API/data/queries/VersionQueries.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,19 @@ async function getPdfPath(id, date) {
121121
* @param {string} newName - The new name to replace in the file paths.
122122
* @returns {Promise} A promise that resolves with the result of the update operation.
123123
*/
124-
async function updatePathes(docId, newName) {
124+
async function updatePathes(docId, oldTitle, newName) {
125125

126126
// Check if the required fields are present
127127
assert(docId, '[VersionQueries.updatePathes] The document ID is required');
128128
assert(newName, '[VersionQueries.updatePathes] The new name is required');
129-
129+
130130
return await executeWithCleanup(async (query) => {
131131

132+
console.log('oldTitle: ', oldTitle);
133+
console.log('newName: ', newName);
134+
132135
let queryStr = 'UPDATE version SET file_path = REPLACE(file_path, ?, ?) WHERE doc_id = ?';
133-
return await query(queryStr, [newName, docId, newName]);
136+
return await query(queryStr, [oldTitle, newName, docId]);
134137

135138
});
136139

API/model/Managers/AuthManager.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,19 @@ async function register(data) {
8787
data.username = data.username.trim();
8888
data.password = data.password.trim();
8989

90-
await queries.createUser(data);
91-
92-
// Check if the user exists, if not throw an error with 'Bad credentials' so that no information is leaked
93-
assert(await userExist(), 'Bad credentials');
90+
try {
91+
await userExist();
92+
} catch (e) {
93+
if (e.message.includes('not found')) {
94+
await queries.createUser(data);
95+
}
96+
}
9497

95-
return await login(data);
9698

99+
// Check if the user exists, if not throw an error
100+
assert(await userExist(), 'Oops, something went wrong. Please try again.');
97101

102+
return login(data);
98103

99104
}
100105

API/model/Managers/DocumentManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ async function updateTitle(id, title) {
132132
id = parseInt(id);
133133
assert(Number(id), '[DocumentManager.updateTitle] Document ID must be a number');
134134

135-
changeFolderAndFilesNames(path.join(__dirname, '../Docs', oldName), title);
135+
changeFolderAndFilesNames(path.join(__dirname, '../../Docs', oldName), title);
136136
await queries.rename(id, title);
137137

138138
}

FRONT/js/data/dao/AuthDao.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class AuthDao extends Dao {
2626
*/
2727
async login(user) {
2828
let resp = await this.post('/login', user);
29-
return await resp.text();
29+
if (resp.status === 200) {
30+
return await resp.text();
31+
}
3032
}
3133

3234
/**
@@ -44,7 +46,9 @@ class AuthDao extends Dao {
4446
*/
4547
async register(user) {
4648
let resp = await this.post('/register', user);
47-
return await resp.json();
49+
if (resp.status === 201) {
50+
return await resp.text();
51+
}
4852
}
4953

5054
/**

FRONT/js/model/Canvas.js

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ class Canvas {
138138
return this.#canvas.toDataURL("image/png");
139139
}
140140

141+
/**
142+
* Exports the canvas as a Blob object.
143+
* @returns {Promise<Blob>} A promise that resolves with the Blob object representing the canvas.
144+
*/
141145
exportToBlob() {
142146
return new Promise((resolve) => {
143147
this.#canvas.toBlob((blob) => {

FRONT/js/model/Instantiator.js

+14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class Instantiator {
144144
return new DocumentManager();
145145
}
146146

147+
/**
148+
* Retrieves the user manager instance.
149+
* @returns {UserManager} The user manager instance.
150+
*/
147151
static async getUserManager() {
148152
await Promise.all([
149153
this.#loadSequentially([this.#pathes.data.access.Dao, this.#pathes.data.access.UserDao]),
@@ -220,6 +224,11 @@ class Instantiator {
220224

221225
}
222226

227+
/**
228+
* Creates an instance of the MainUserDocSignedTemplateManager class.
229+
* @param {Container} container - The container object.
230+
* @returns {MainUserDocSignedTemplateManager} The MainUserDocSignedTemplateManager instance.
231+
*/
223232
static async mainUserDocSignedTemplateManager(container) {
224233
await this.#loadSequentially([
225234
this.#pathes.template.manager,
@@ -229,6 +238,11 @@ class Instantiator {
229238
return new MainUserDocSignedTemplateManager(container);
230239
}
231240

241+
/**
242+
* Creates a new instance of the MainUserDocWaitingTemplateManager class.
243+
* @param {Container} container - The container object.
244+
* @returns {MainUserDocWaitingTemplateManager} The new instance of MainUserDocWaitingTemplateManager.
245+
*/
232246
static async mainUserDocWaitingTemplateManager(container) {
233247
await this.#loadSequentially([
234248
this.#pathes.template.manager,

FRONT/js/model/auth/AuthManager.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AuthManager {
3939
response = await this.dao.checkForExistingUser();
4040
toReturn.userExists = response.status === 200;
4141

42-
if (toReturn) {
42+
if (toReturn.userExists) {
4343
let responseBody = await response.json();
4444
if (responseBody.logged) {
4545
document.dispatchEvent(new Event('USER_LOGGED_IN'));
@@ -68,6 +68,7 @@ class AuthManager {
6868
*/
6969
async login(user) {
7070
let response = await this.dao.login(user);
71+
if (!response) throw new Error('Login failed');
7172
this.#setToken(response);
7273
}
7374

@@ -78,6 +79,7 @@ class AuthManager {
7879
*/
7980
async register(user) {
8081
let response = await this.dao.register(user);
82+
if (!response) throw new Error('Login failed');
8183
this.#setToken(response);
8284
}
8385

FRONT/js/model/data/DocumentManager.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class DocumentManager extends DataManager {
7878
}
7979
}
8080

81-
81+
8282
let toReturn = [];
8383
let data;
8484

@@ -146,8 +146,10 @@ class DocumentManager extends DataManager {
146146
console.log(e);
147147
}
148148

149-
// depending on what appened we either reload the page or return a message
150-
if (data) { } // TODO: message popup that reload the view
149+
if (data) {
150+
window.location.reload();
151+
}
152+
151153
}
152154

153155
/**
@@ -166,7 +168,9 @@ class DocumentManager extends DataManager {
166168
}
167169

168170
// depending on the api call, either tell the element was added, or return an error message
169-
if (data) { } // TODO: message popup that reload the view / error message
171+
if (data) {
172+
window.location.reload();
173+
}
170174
}
171175

172176

@@ -178,6 +182,8 @@ class DocumentManager extends DataManager {
178182
let data;
179183
try {
180184
data = await super.update(object);
185+
alert('Document renommé avec succès !');
186+
window.location.reload();
181187
} catch (e) {
182188
console.log(e);
183189
}

FRONT/js/model/popups/DocumentClickedPopup.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,20 @@ class DocumentClickedPopup extends Popup {
102102
admin_only: true,
103103
action: async (dataMap, docId) => {
104104
const documentManager = dataMap['manager'];
105-
const popupManager = dataMap['popupManager'];
106105

107106
super.close();
108107

109108
let messageContent;
110109
try {
111110
// archive the document
112111
await documentManager.archive(docId);
113-
messageContent = `Le document ${this.document.getFileName()} a été archivé avec succès.
114-
Vous pouvez le retrouver dans la liste des documents archivés. (connexion requise)`;
112+
messageContent = `Le document ${this.document.getFileName()} a été archivé avec succès.\nsVous pouvez le retrouver dans la liste des documents archivés. (connexion requise)`;
115113
} catch (e) {
116114
console.log(e);
117115
messageContent = `Une erreur est survenue lors de l'archivage du document ${this.document.getFileName()}. Veuillez réessayer.`;
118116
} finally {
119-
popupManager.open('message-popup', {
120-
title: 'Archivage du document',
121-
message: messageContent
122-
});
117+
alert(messageContent);
118+
window.location.reload();
123119
}
124120
},
125121
archive_button: false
@@ -138,16 +134,13 @@ class DocumentClickedPopup extends Popup {
138134
try {
139135
// archive the document
140136
await documentManager.unarchive(docId);
141-
messageContent = `Le document ${this.document.getFileName()} a été désarchivé avec succès.
142-
Vous pouvez le retrouver dans la liste des documents non archivés.`;
137+
messageContent = `Le document ${this.document.getFileName()} a été désarchivé avec succès.\nVous pouvez le retrouver dans la liste des documents non archivés.`;
143138
} catch (e) {
144139
console.log(e);
145140
messageContent = `Une erreur est survenue lors du désarchivage du document ${this.document.getFileName()}. Veuillez réessayer.`;
146141
} finally {
147-
popupManager.open('message-popup', {
148-
title: 'Désarchivage du document',
149-
message: messageContent
150-
});
142+
alert(messageContent);
143+
window.location.reload();
151144
}
152145
},
153146
archive_button: true

FRONT/js/model/template/MainUserTemplateManager.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ class MainUserTemplateManager extends TemplateManager {
4040
// values is a map that contains the values to be replaced in the template,
4141
// the key is the name of the variable in the template and the value is the value to replace
4242
let values = [];
43-
43+
4444
values['id'] = user.id;
4545

4646
if (!user.archived_date) {
4747
const signedCount = user.docs_signatures.length;
4848
const outdatedCount = user.docs_signatures.filter(doc => doc.toUpdate === true).length;
4949
const waitingCount = user.docs_waiting.length;
5050

51-
values['DisplayName'] = user.display_name;
51+
values['DisplayName'] = user.display_name;
5252
values['Signed'] = signedCount < 10 ? '0' + signedCount : signedCount;
5353
values['Outdated'] = outdatedCount < 10 ? '0' + outdatedCount : outdatedCount;
5454
values['Waiting'] = waitingCount < 10 ? '0' + waitingCount : waitingCount;

FRONT/js/view/HeaderManager.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ class HeaderManager {
3333
// add the event listener to log out
3434
accountButton.addEventListener('click', (e) => {
3535
e.preventDefault();
36-
document.dispatchEvent(new CustomEvent('USER_NOT_LOGGED_IN', { detail: { userExist: true } }));
37-
36+
3837
// remove the user from the local storage
3938
window.localStorage.removeItem('userToken');
39+
40+
document.dispatchEvent(new CustomEvent('USER_LOGGED_OUT', { detail: { userExist: true } }));
41+
4042
});
4143
});
4244

FRONT/js/view/signedList.js

+7
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@ class SignedListView extends View {
3636
async #init(authManager) {
3737
this.#setPageTitle();
3838
await this.initTemplateManager();
39+
this.isLogged = false;
3940

4041
// Wait for auth Check
4142
document.addEventListener('USER_LOGGED_IN', () => {
4243
this.#displayUsers().then(() => {
44+
this.isLogged = true;
4345
this.addButtons();
4446
this.#manageSearch();
4547
});
4648
});
4749

4850
// if the user is not logged in, we redirect to the index page
4951
document.addEventListener('USER_NOT_LOGGED_IN', () => {
52+
this.isLogged = false;
5053
this.#displayUsers().then(() => {
5154
this.removeButtons();
5255
this.#manageSearch();
@@ -172,5 +175,9 @@ class SignedListView extends View {
172175
let userList = users.filter(user => user.getDisplayName().toLowerCase().includes(searchValue.toLowerCase()));
173176
await this.templateManager.addSignedUsers(userList);
174177

178+
if (!this.isLogged) {
179+
this.removeButtons();
180+
}
181+
175182
}
176183
}

FRONT/visual/pages/login.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h2 class="smallcaps">Connexion</h2>
2929
<form action="#">
3030
<div class="field">
3131
<label for="user-identifier" class="lightText">Identifiant :</label>
32-
<input type="text" placeholder="Identifiant " name="identifier" id="user-identifier" required>
32+
<input type="text" placeholder="prénom.nom" name="identifier" id="user-identifier" required>
3333
</div>
3434
<div class="field">
3535
<label for="user-password" class="lightText">Mot de passe :</label>

0 commit comments

Comments
 (0)