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
Branches
Tags
No related merge requests found
......@@ -32,7 +32,7 @@ class Session {
if ( 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;
}
} catch ( err ) { }
......
......@@ -27,7 +27,7 @@ class UserManager {
}
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;
}
......
......@@ -22,7 +22,7 @@ class SecurityMiddleware {
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) => {
if ( checkIfConnected ) {
if ( req.session.profile.userID === null ) {
if ( req.session.profile.userId === null ) {
return req.session.sendResponse(res, StatusCodes.UNAUTHORIZED);
}
}
......
......@@ -6,12 +6,12 @@ class EnonceStaff extends Model {
static tableName: string = 'EnonceStaff';
enonceName: string = null;
userID: number = null;
userId: number = null;
public async toJsonObject(): Promise<Object> {
const result = {
'enonceName': this.enonceName,
'userID' : this.userID
'userId' : this.userId
};
return result;
......@@ -19,13 +19,13 @@ class EnonceStaff extends Model {
public importFromJsonObject(jsonObject: any) {
this.enonceName = jsonObject.enonceName;
this.userID = jsonObject.userID;
this.userId = jsonObject.userId;
}
public toDb(): any {
return {
enonceName: this.enonceName,
userID : this.userID
userId : this.userId
};
}
......@@ -35,7 +35,7 @@ class EnonceStaff extends Model {
}
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 Config from '../config/Config';
import Toolbox from '../shared/Toolbox';
import Toolbox from '../shared/helpers/Toolbox';
import * as bcrypt from 'bcryptjs';
import Model from './Model';
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 {
static tableName: string = 'Users';
static tableName: string = 'User';
userID: number = null;
userId: number = null;
userFirstName: string = '';
userLastName: string = '';
userMail: string = '';
......@@ -23,9 +26,13 @@ class User extends Model {
currentSession: Session = null;
gitlabProfile = new LazyVal<GitlabUser>(() => {
return GitlabManager.getUserById(this.userGitlabId);
});
public async toJsonObject(): Promise<Object> {
const result = {
'id' : this.userID,
'id' : this.userId,
'firstName' : this.userFirstName,
'lastName' : this.userLastName,
'mail' : this.userMail,
......@@ -47,7 +54,7 @@ class User extends Model {
}
public importFromJsonObject(jsonObject: any) {
this.userID = jsonObject.id;
this.userId = jsonObject.id;
this.userFirstName = jsonObject.firstName;
this.userLastName = jsonObject.lastName;
this.userMail = jsonObject.mail;
......@@ -83,21 +90,21 @@ class User extends Model {
async create(): Promise<User> {
const id = await db(User.tableName).insert(this.toDb());
this.userID = id[0];
this.userId = id[0];
return this;
}
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> {
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> {
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 {
}).create();
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({
enonceName: enonce.enonceName,
userID : dojoUser.userID
userId : dojoUser.userId
}).create()));
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