Skip to content
Snippets Groups Projects
Commit 4352e5ca authored by michael.minelli's avatar michael.minelli
Browse files

User => Rename userID to userId + Add gitlabProfile getter

parent a81ab102
No related branches found
No related tags found
No related merge requests found
...@@ -32,7 +32,7 @@ class Session { ...@@ -32,7 +32,7 @@ class Session {
if ( jwtData.profile ) { if ( jwtData.profile ) {
this.profile.importFromJsonObject(jwtData.profile); this.profile.importFromJsonObject(jwtData.profile);
this.profile = await UserManager.getById(this.profile.userID); this.profile = await UserManager.getById(this.profile.userId);
this.profile.currentSession = this; this.profile.currentSession = this;
} }
} catch ( err ) { } } catch ( err ) { }
......
...@@ -27,7 +27,7 @@ class UserManager { ...@@ -27,7 +27,7 @@ class UserManager {
} }
async getById(id: number): Promise<User | undefined> { async getById(id: number): Promise<User | undefined> {
const raw = await db<User>(User.tableName).where('userID', id).first(); const raw = await db<User>(User.tableName).where('userId', id).first();
return raw ? this.createObjectFromRawSql(raw) : undefined; return raw ? this.createObjectFromRawSql(raw) : undefined;
} }
......
...@@ -22,7 +22,7 @@ class SecurityMiddleware { ...@@ -22,7 +22,7 @@ class SecurityMiddleware {
check(checkIfConnected: boolean, ...checkTypes: Array<SecurityCheckType>): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void { check(checkIfConnected: boolean, ...checkTypes: Array<SecurityCheckType>): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void {
return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => { return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => {
if ( checkIfConnected ) { if ( checkIfConnected ) {
if ( req.session.profile.userID === null ) { if ( req.session.profile.userId === null ) {
return req.session.sendResponse(res, StatusCodes.UNAUTHORIZED); return req.session.sendResponse(res, StatusCodes.UNAUTHORIZED);
} }
} }
......
...@@ -6,12 +6,12 @@ class EnonceStaff extends Model { ...@@ -6,12 +6,12 @@ class EnonceStaff extends Model {
static tableName: string = 'EnonceStaff'; static tableName: string = 'EnonceStaff';
enonceName: string = null; enonceName: string = null;
userID: number = null; userId: number = null;
public async toJsonObject(): Promise<Object> { public async toJsonObject(): Promise<Object> {
const result = { const result = {
'enonceName': this.enonceName, 'enonceName': this.enonceName,
'userID' : this.userID 'userId' : this.userId
}; };
return result; return result;
...@@ -19,13 +19,13 @@ class EnonceStaff extends Model { ...@@ -19,13 +19,13 @@ class EnonceStaff extends Model {
public importFromJsonObject(jsonObject: any) { public importFromJsonObject(jsonObject: any) {
this.enonceName = jsonObject.enonceName; this.enonceName = jsonObject.enonceName;
this.userID = jsonObject.userID; this.userId = jsonObject.userId;
} }
public toDb(): any { public toDb(): any {
return { return {
enonceName: this.enonceName, enonceName: this.enonceName,
userID : this.userID userId : this.userId
}; };
} }
...@@ -35,7 +35,7 @@ class EnonceStaff extends Model { ...@@ -35,7 +35,7 @@ class EnonceStaff extends Model {
} }
del(): Promise<void> { del(): Promise<void> {
return db(EnonceStaff.tableName).where('enonceName', this.enonceName).andWhere('userID', this.userID).del(); return db(EnonceStaff.tableName).where('enonceName', this.enonceName).andWhere('userId', this.userId).del();
} }
} }
......
import Session from '../controllers/Session'; import Session from '../controllers/Session';
import Config from '../config/Config'; import Config from '../config/Config';
import Toolbox from '../shared/Toolbox'; import Toolbox from '../shared/helpers/Toolbox';
import * as bcrypt from 'bcryptjs'; import * as bcrypt from 'bcryptjs';
import Model from './Model'; import Model from './Model';
import db from '../helpers/DatabaseHelper'; import db from '../helpers/DatabaseHelper';
import LazyVal from '../shared/helpers/LazyVal';
import GitlabUser from '../shared/types/Gitlab/GitlabUser';
import GitlabManager from '../managers/GitlabManager';
class User extends Model { class User extends Model {
static tableName: string = 'Users'; static tableName: string = 'User';
userID: number = null; userId: number = null;
userFirstName: string = ''; userFirstName: string = '';
userLastName: string = ''; userLastName: string = '';
userMail: string = ''; userMail: string = '';
...@@ -23,9 +26,13 @@ class User extends Model { ...@@ -23,9 +26,13 @@ class User extends Model {
currentSession: Session = null; currentSession: Session = null;
gitlabProfile = new LazyVal<GitlabUser>(() => {
return GitlabManager.getUserById(this.userGitlabId);
});
public async toJsonObject(): Promise<Object> { public async toJsonObject(): Promise<Object> {
const result = { const result = {
'id' : this.userID, 'id' : this.userId,
'firstName' : this.userFirstName, 'firstName' : this.userFirstName,
'lastName' : this.userLastName, 'lastName' : this.userLastName,
'mail' : this.userMail, 'mail' : this.userMail,
...@@ -47,7 +54,7 @@ class User extends Model { ...@@ -47,7 +54,7 @@ class User extends Model {
} }
public importFromJsonObject(jsonObject: any) { public importFromJsonObject(jsonObject: any) {
this.userID = jsonObject.id; this.userId = jsonObject.id;
this.userFirstName = jsonObject.firstName; this.userFirstName = jsonObject.firstName;
this.userLastName = jsonObject.lastName; this.userLastName = jsonObject.lastName;
this.userMail = jsonObject.mail; this.userMail = jsonObject.mail;
...@@ -83,21 +90,21 @@ class User extends Model { ...@@ -83,21 +90,21 @@ class User extends Model {
async create(): Promise<User> { async create(): Promise<User> {
const id = await db(User.tableName).insert(this.toDb()); const id = await db(User.tableName).insert(this.toDb());
this.userID = id[0]; this.userId = id[0];
return this; return this;
} }
update(): Promise<void> { update(): Promise<void> {
return db(User.tableName).where('userID', this.userID).update(this.toDb()); return db(User.tableName).where('userId', this.userId).update(this.toDb());
} }
updatePassword(): Promise<void> { updatePassword(): Promise<void> {
return db(User.tableName).where('userID', this.userID).update({ 'userPassword': this.userPassword }); return db(User.tableName).where('userId', this.userId).update({ 'userPassword': this.userPassword });
} }
del(): Promise<void> { del(): Promise<void> {
return db(User.tableName).where('userID', this.userID).update({ 'userDeleted': true }); return db(User.tableName).where('userId', this.userId).update({ 'userDeleted': true });
} }
} }
......
...@@ -100,10 +100,10 @@ class EnonceRoutes implements RoutesManager { ...@@ -100,10 +100,10 @@ class EnonceRoutes implements RoutesManager {
}).create(); }).create();
let dojoUsers: Array<User> = [ req.session.profile, ...(await UserManager.getFromGitlabUsers(params.members, true) as Array<User>) ]; let dojoUsers: Array<User> = [ req.session.profile, ...(await UserManager.getFromGitlabUsers(params.members, true) as Array<User>) ];
dojoUsers = dojoUsers.reduce((unique, user) => (unique.findIndex(uniqueUser => uniqueUser.userID === user.userID) !== -1 ? unique : [ ...unique, user ]), Array<User>()); dojoUsers = dojoUsers.reduce((unique, user) => (unique.findIndex(uniqueUser => uniqueUser.userId === user.userId) !== -1 ? unique : [ ...unique, user ]), Array<User>());
await Promise.all(dojoUsers.map(dojoUser => EnonceStaff.createFromSql({ await Promise.all(dojoUsers.map(dojoUser => EnonceStaff.createFromSql({
enonceName: enonce.enonceName, enonceName: enonce.enonceName,
userID : dojoUser.userID userId : dojoUser.userId
}).create())); }).create()));
return req.session.sendResponse(res, StatusCodes.OK, enonce.toJsonObject()); return req.session.sendResponse(res, StatusCodes.OK, enonce.toJsonObject());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment