diff --git a/ExpressAPI/src/managers/EnonceManager.ts b/ExpressAPI/src/managers/EnonceManager.ts index 704a17278abce7b51e906b419464ac2d3a4dbfee..03022d84827911a5f4e5c44bba8f902698c4fba2 100644 --- a/ExpressAPI/src/managers/EnonceManager.ts +++ b/ExpressAPI/src/managers/EnonceManager.ts @@ -16,7 +16,12 @@ class EnonceManager { } createObjectFromRawSql(raw: any): Enonce { - return Enonce.createFromSql(raw); + const enonce = Enonce.createFromSql(raw); + + enonce.enonceGitlabCreationInfo = raw.enonceGitlabCreationInfo; + enonce.enonceGitlabLastInfo = raw.enonceGitlabLastInfo; + + return enonce; } async getByName(name: string): Promise<Enonce | undefined> { diff --git a/ExpressAPI/src/models/Enonce.ts b/ExpressAPI/src/models/Enonce.ts index 43f3089dc79c0a7997eb64e4d6befc9dc3243866..07272be07630de4762071f60ebacfbc63f770f0c 100644 --- a/ExpressAPI/src/models/Enonce.ts +++ b/ExpressAPI/src/models/Enonce.ts @@ -1,5 +1,9 @@ -import Model from './Model'; -import db from '../helpers/DatabaseHelper'; +import Model from './Model'; +import db from '../helpers/DatabaseHelper'; +import GitlabRepository from '../shared/types/Gitlab/GitlabRepository'; +import LazyVal from '../shared/helpers/LazyVal'; +import UserManager from '../managers/UserManager'; +import User from './User'; class Enonce extends Model { @@ -8,17 +12,47 @@ class Enonce extends Model { enonceName: string = ''; enonceGitlabId: number = null; enonceGitlabLink: string = ''; - enonceGitlabCreationInfo: string = ''; - enonceGitlabLastInfo: string = ''; + private _enonceGitlabCreationInfo: string = '{}'; + private _enonceGitlabLastInfo: string = '{}'; enonceGitlabLastInfoTs: number = null; + get enonceGitlabCreationInfo(): GitlabRepository { + return JSON.parse(this._enonceGitlabCreationInfo); + } + + set enonceGitlabCreationInfo(value: any) { + if ( typeof value === 'string' ) { + this._enonceGitlabCreationInfo = value; + return; + } + + this._enonceGitlabCreationInfo = JSON.stringify(value); + } + + get enonceGitlabLastInfo(): GitlabRepository { + return JSON.parse(this._enonceGitlabLastInfo); + } + + set enonceGitlabLastInfo(value: any) { + if ( typeof value === 'string' ) { + this._enonceGitlabLastInfo = value; + return; + } + + this._enonceGitlabLastInfo = JSON.stringify(value); + } + + staff = new LazyVal<Array<User>>(() => { + return UserManager.getStaffOfEnonce(this.enonceName); + }); + public async toJsonObject(): Promise<Object> { const result = { 'name' : this.enonceName, 'gitlabId' : this.enonceGitlabId, 'gitlabLink' : this.enonceGitlabLink, - 'gitlabCreationInfo': JSON.parse(this.enonceGitlabCreationInfo), - 'gitlabLastInfo' : JSON.parse(this.enonceGitlabLastInfo), + 'gitlabCreationInfo': this.enonceGitlabCreationInfo, + 'gitlabLastInfo' : this.enonceGitlabLastInfo, 'gitlabLastInfoTs' : this.enonceGitlabLastInfoTs }; @@ -39,8 +73,8 @@ class Enonce extends Model { enonceName : this.enonceName, enonceGitlabId : this.enonceGitlabId, enonceGitlabLink : this.enonceGitlabLink, - enonceGitlabCreationInfo: this.enonceGitlabCreationInfo, - enonceGitlabLastInfo : this.enonceGitlabLastInfo, + enonceGitlabCreationInfo: this._enonceGitlabCreationInfo, + enonceGitlabLastInfo : this._enonceGitlabLastInfo, enonceGitlabLastInfoTs : this.enonceGitlabLastInfoTs }; } diff --git a/ExpressAPI/src/routes/EnonceRoutes.ts b/ExpressAPI/src/routes/EnonceRoutes.ts index dd6320d4b7c0ab8aff0fe244d2d722778362d1f2..6757e9815b2ca1747fdfb27ba06e4532883ac929 100644 --- a/ExpressAPI/src/routes/EnonceRoutes.ts +++ b/ExpressAPI/src/routes/EnonceRoutes.ts @@ -20,6 +20,7 @@ import EnonceStaff from '../models/EnonceStaff'; import { AxiosError } from 'axios'; import logger from '../shared/logging/WinstonLogger'; import DojoValidators from '../helpers/DojoValidators'; +import EnonceManager from '../managers/EnonceManager'; class EnonceRoutes implements RoutesManager { @@ -90,14 +91,14 @@ class EnonceRoutes implements RoutesManager { } })); - const enonce: Enonce = await Enonce.createFromSql({ - enonceName : repository.name, - enonceGitlabId : repository.id, - enonceGitlabLink : repository.web_url, - enonceGitlabCreationInfo: JSON.stringify(repository), - enonceGitlabLastInfo : JSON.stringify(repository), - enonceGitlabLastInfoTs : new Date().getTime() - }).create(); + const enonce: Enonce = await EnonceManager.createObjectFromRawSql({ + enonceName : repository.name, + enonceGitlabId : repository.id, + enonceGitlabLink : repository.web_url, + enonceGitlabCreationInfo: JSON.stringify(repository), + enonceGitlabLastInfo : JSON.stringify(repository), + enonceGitlabLastInfoTs : new Date().getTime() + }).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>());