routes-project.js 17.6 KB
Newer Older
1
const fs = require('fs')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
2
//const SamlStrategy = require('passport-saml').Strategy
Rosanny Sihombing's avatar
Rosanny Sihombing committed
3
const dbconn = require('./dbconn')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
4
const methods = require('./methods')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
6
7
8
9
// pwd encryption
//const bcrypt = require('bcryptjs');
//const saltRounds = 10;
//const salt = 64; // salt length
// forgot pwd
Rosanny Sihombing's avatar
Rosanny Sihombing committed
10
const async = require('async')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
11
12
//const crypto = require('crypto')
//const mailer = require('./mailer')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
13

Rosanny Sihombing's avatar
Rosanny Sihombing committed
14
const helpers = require('./helpers')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
15
const pictSizeLimit = 1000000 // 1 MB
16
const axios = require('axios')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
17

Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
module.exports = function (app) {
19
 
Rosanny Sihombing's avatar
Rosanny Sihombing committed
20
21
  // ======== APP ROUTES - PROJECT ====================
  var lang = 'DE'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

  app.get('/mailinglists', function (req, res) {
    async.waterfall([
        function(done) {
            methods.getAllMailinglists(function(mailinglistOverview, err) {
                if (!err) {
                    done(err, mailinglistOverview)
                }
            })
        },
        // create JSON object of mailinglists for front-end
        function(mailinglistOverview, done) {
            var allMailingLists = []  // JSON object
            for (let i = 0; i < mailinglistOverview.length; i++) {
                // add data to JSON object
                allMailingLists.push({
                    id: mailinglistOverview[i].id,
                    name: mailinglistOverview[i].name,
                    src: mailinglistOverview[i].src,
                    projectstatus: mailinglistOverview[i].projectstatus,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
42
43
                    project_title: mailinglistOverview[i].project_title,
                    keywords: mailinglistOverview[i].keywords
Rosanny Sihombing's avatar
Rosanny Sihombing committed
44
45
46
47
48
49
50
51
52
53
                });
            }

            res.render(lang+'/project/mailinglists', {
                isUserAuthenticated: req.isAuthenticated(),
                user: req.user,
                mailinglists: allMailingLists
            });
        }
    ])
Rosanny Sihombing's avatar
Rosanny Sihombing committed
54
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
55
/*
Rosanny Sihombing's avatar
Rosanny Sihombing committed
56
  app.get('/project_', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    async.waterfall([
      // get all projects from projectdb
      function(done) {
        methods.getAllProjects(function(projectsOverview, err) {
          if (!err) {
            done(err, projectsOverview)
          }
        })
      },
      // create JSON object for front-end
      function(projectsOverview, done) {
        var activeProjects = []
        var nonActiveProjects = []

        for (var i = 0; i < projectsOverview.length; i++) {
          var project = {
            id: projectsOverview[i].id,
            logo: projectsOverview[i].logo,
            akronym: projectsOverview[i].pname,
            title: projectsOverview[i].title,
            summary: projectsOverview[i].onelinesummary,
            category: projectsOverview[i].category,
            cp: projectsOverview[i].contact_email,
            gitlab: projectsOverview[i].gitlab
          }
          if (projectsOverview[i].projectstatus == 0) {
            nonActiveProjects.push(project)
          }
          else if (projectsOverview[i].projectstatus == 1) {
            activeProjects.push(project)
          }
        }

        // render the page
        if (req.isAuthenticated()) {
          res.render(lang+'/project/projects', {
            isUserAuthenticated: true,
            nonActive: nonActiveProjects,
            active: activeProjects
          });
        }
        else {
          res.render(lang+'/project/projects', {
            isUserAuthenticated: false,
            nonActive: nonActiveProjects,
            active: activeProjects
          });
        }
      }
    ])
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
*/
Rosanny Sihombing's avatar
Rosanny Sihombing committed
109
  app.get('/', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
110
111
112
113
114
    res.render(lang+'/project/project-simplified', {
       isUserAuthenticated: req.isAuthenticated(),
       user: req.user
    });
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
115
/*
Rosanny Sihombing's avatar
Rosanny Sihombing committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  app.get('/addprojectoverview', function (req, res) {
    if (req.isAuthenticated()) {
      res.render(lang+'/project/addProjectOverview')
    }
    else {
      res.redirect('/login')
    }
  })
  
  app.post('/addprojectoverview__', function (req, res) {
    if (req.isAuthenticated()) {
      var wiki = 0
      if (req.body.wiki)
        wiki = 1

      var projectTerm = req.body.termForm + " - " + req.body.termTo
      var projectOverviewData = {
        pname: req.body.pname,
        title: req.body.title,
        onelinesummary: req.body.summary,
        category: req.body.category,
        logo: req.body.logo,
        gitlab: req.body.gitlabURL,
        wiki: wiki,
        overview: req.body.overview,
        question: req.body.question,
        approach: req.body.approach,
        result: req.body.result,
        keywords: req.body.keywords,
        announcement: req.body.announcement,
        term: projectTerm,
        further_details: req.body.furtherDetails,
        website: req.body.website,
        src: req.body.src,
        caption: req.body.caption,
        contact_lastname: req.body.contactName,
        contact_email: req.body.contactEmail,
        leader_lastname: req.body.leaderName,
        leader_email: req.body.leaderEmail
      }
      
      methods.addProjectOverview(projectOverviewData, function(err){
        if (err) {
          //req.flash('error', "Failed")
          req.flash('error', "Fehlgeschlagen")
          res.redirect('/addProjectOverview');
        }
        else {
          req.flash('success', 'Your project has been created.')
          res.redirect('/project');
        }
      })
    }
  })

  app.post('/addprojectoverview', function (req, res) {
    if (req.isAuthenticated()) {
      var wiki = 0
      if (req.body.wiki)
        wiki = 1

      var projectLogo = req.files.logo
      var projectPicture = req.files.src
      var projectLogoPath, projectPicturePath
      
      if (projectLogo) {
        // raise error if size limit is exceeded
        if (projectLogo.size === pictSizeLimit) {
          req.flash('error', 'Projektlogo exceeds 1 MB');
          res.redirect('/addprojectoverview');
        }
        else {
          // TEST PATH FOR DEVELOPMENT (LOCALHOST)
          projectLogoPath = './folder-in-server-to-save-projektlogo/'+req.body.pname+'/'+projectLogo.name
          // PATH FOR TEST/LIVE SERVER
          // var projectLogoPath = to-be-defined
        }
      }
      if (projectPicture) {
        // raise error if size limit is exceeded
        if (projectPicture.size === pictSizeLimit) {
          req.flash('error', 'Projektbild exceeds 1 MB');
          res.redirect('/addprojectoverview');
        }
        else {
          // TEST PATH FOR DEVELOPMENT (LOCALHOST)
          projectPicturePath = './folder-in-server-to-save-projektbild/'+req.body.pname+'/'+projectPicture.name
          // PATH FOR TEST/LIVE SERVER
          // var projectPicturePath = to-be-defined
        }
        
      }
      
      var projectTerm = req.body.termForm + " - " + req.body.termTo
      var projectOverviewData = {
        pname: req.body.pname,
        title: req.body.title,
        onelinesummary: req.body.summary,
        category: req.body.category,
        logo: projectLogoPath,
        gitlab: req.body.gitlabURL,
        wiki: wiki,
        overview: req.body.overview,
        question: req.body.question,
        approach: req.body.approach,
        result: req.body.result,
        keywords: req.body.keywords,
        announcement: req.body.announcement,
        term: projectTerm,
        further_details: req.body.furtherDetails,
        website: req.body.website,
        src: projectPicturePath,
        caption: req.body.caption,
        contact_lastname: req.body.contactName,
        contact_email: req.body.contactEmail,
        leader_lastname: req.body.leaderName,
        leader_email: req.body.leaderEmail
      }
      
      // save pictures
      if (projectLogo) {
        projectLogo.mv(projectLogoPath, function(err) {
          if (err) {
            console.error(err)
            res.status(500).render(lang+'/500', {
              error: err
            })
          }
        });
      }
      if (projectPicture) {
        projectPicture.mv(projectPicturePath, function(err) {
          if (err) {
            console.error(err)
            res.status(500).render(lang+'/500', {
              error: err
            })
          }
        });
      }

Rosanny Sihombing's avatar
Rosanny Sihombing committed
257
258
259
      // RS: Temporary solution while Project DB is still in early phase.
      //        When User DB and Project DB are integrated and quite stabil, this operation should be done in 1 transaction.
      
Rosanny Sihombing's avatar
Rosanny Sihombing committed
260
261
262
263
      var userId // todo: make this global variable?
      async.waterfall([
        // get userId by email from userdb
        function(done) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
264
          methods.getUserIdByEmail(req.user.email, function(id, err) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
            if (!err) {
              userId = id
              done(err)
            }
          })
        },
        // add project overview
        function(done) {
          methods.addProjectOverview(projectOverviewData, function(data, err){
            if (err) {
              res.status(500).render(lang+'/500', {
                error: err
              })
            }
            else {
              done(err, data.insertId)
            }
          })
        },
        // assign the created overview to logged-in user
        function(projectOverviewId, done) {
          var userProjectRoleData = {
            project_id: projectOverviewId,
            user_id: userId,
            role_id: 3 // OVERVIEW_CREATOR
          }
          methods.addUserProjectRole(userProjectRoleData, function(userProjects, err) {
            if (err) {
              //req.flash('error', "Failed")
              req.flash('error', "Fehlgeschlagen")
              res.redirect('/addProjectOverview');
            }
            else {
              req.flash('success', 'Your project has been created.')
              res.redirect('/project');
            }
          })
        }
      ])
    }
  })

  app.get('/updateprojectoverview', function (req, res) {
    // only their own project
  })

  app.post('/updateprojectoverview', function (req, res) {
    // only their own project
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
314
*/
Rosanny Sihombing's avatar
Rosanny Sihombing committed
315
316
  app.get('/projectoverview', function(req, res){
    async.waterfall([
Rosanny Sihombing's avatar
Rosanny Sihombing committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
      function(done) {
        methods.getProjectOverviewById(req.query.projectID, function(projectOverview, err) {
          if (!err) {
            done(err, projectOverview)
          }
        })
      },
      function(projectOverview,done){
        methods.getProjectImagesById(req.query.projectID, function(projectImages, err) {
          if (!err) {
            done(err, projectImages, projectOverview)
          }
        })
      },
      // render projectOverview page
      function(projectImages, projectOverview, done) {
        console.log(projectImages)
        partnerWebsites = helpers.stringToArray(projectOverview[0].partner_website)
        partnerNames = helpers.stringToArray(projectOverview[0].partner_name)
        awardSites = helpers.stringToArray(projectOverview[0].award_website)
        awardNames = helpers.stringToArray(projectOverview[0].award_name)
        sponsorWebsites = helpers.stringToArray(projectOverview[0].sponsor_website)
        sponsorImgs = helpers.stringToArray(projectOverview[0].sponsor_img)
        sponsorNames = helpers.stringToArray(projectOverview[0].sponsor_name)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
341

Rosanny Sihombing's avatar
Rosanny Sihombing committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
        res.render(lang+'/project/projectOverview', {
          isUserAuthenticated: req.isAuthenticated(),
          user: req.user,
          projectOV: projectOverview,
          projectImgs: projectImages,
          partnerWS: partnerWebsites,
          partnerN: partnerNames,
          awardWS: awardSites,
          awardN: awardNames,
          sponsorWS: sponsorWebsites,
          sponsorIMG: sponsorImgs,
          sponsorN: sponsorNames
        });
      }
    ])
Rosanny Sihombing's avatar
Rosanny Sihombing committed
357
358
  })

359
  async function getProjectsFromGitlab(perPage, idAfter) {
360
    // public projects
Rosanny Sihombing's avatar
Rosanny Sihombing committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    return await axios.get('https://transfer.hft-stuttgart.de/gitlab/api/v4/projects?visibility=public&pagination=keyset&per_page='+
      perPage+'&order_by=id&sort=asc&id_after='+idAfter)
  }

  async function getProjectsRunnersFromGitlab() {
    // runners
    // https://github.com/axios/axios/issues/638
    /*
    return await axios.get('https://transfer.hft-stuttgart.de/gitlab/api/v4/runners/7',
      {
        params:{},
        headers: { 'Authorization': '33eBjByS2p-Pye8tzY7j' } 
      })*/

    axios.get('https://transfer.hft-stuttgart.de/gitlab/api/v4/runners/7', { headers: { Authorization: '33eBjByS2p-Pye8tzY7j' } }).then(response => {
      // If request is good...
      console.log(response.data);
    })
    .catch((error) => {
      console.log('error 3 ' + error);
    });
382
  }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
383

384
385

  app.get('/projectlist-onelistforall', async function(req, res){
386
    let projectArr = []
387
    let isProject = true
388
    let firstId = 0
389
390

    while (isProject == true) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
391
392
393
394
      //
      //let runners = await getProjectsRunnersFromGitlab()
      //console.log(runners)
      //
395
396
397
      let projects = await getProjectsFromGitlab(10, firstId)
      let projectData = projects.data

398
399
      if (projectData.length == 0) {
        isProject = false
Rosanny Sihombing's avatar
Rosanny Sihombing committed
400
      }
401
402
      else {
        for(let i = 0; i < projectData.length; i++){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
403
404
405
406
407
          // skip template project
          if (projectData[i].name == "template_gitlab_page") {
            continue
          }
          // M4_LAB logo for all projects that do not have logo
408
409
410
          if (projectData[i].avatar_url == null) {
            projectData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png"
          }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
411
412
413
414
          // for all projects that have no description
          if (projectData[i].description == "") {
            projectData[i].description = "- no description -"
          }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
415
          // websites
Rosanny Sihombing's avatar
Rosanny Sihombing committed
416
          if (projectData[i].tag_list.includes('website')) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
417
418
            // customize website name
            if (projectData[i].name == "Visualization") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
419
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/visualization"
Rosanny Sihombing's avatar
Rosanny Sihombing committed
420
421
            }
            else if (projectData[i].name == "IN-Source") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
422
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/INsource"
Rosanny Sihombing's avatar
Rosanny Sihombing committed
423
424
            }
            else if (projectData[i].name == "3DS_Visualization_Cesium") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
425
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/3ds_visualization_cesium"
Rosanny Sihombing's avatar
Rosanny Sihombing committed
426
427
            }
            else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
428
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/"+projectData[i].name
Rosanny Sihombing's avatar
Rosanny Sihombing committed
429
            }
430
431

            // fill in pagesArr
Rosanny Sihombing's avatar
Rosanny Sihombing committed
432
            
433
          }
434
435
436
          else {
            // fill in projectArr
          }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
437
438
439
440
          let project = {
            logo: projectData[i].avatar_url,
            name: projectData[i].name,
            weburl: projectData[i].web_url,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
441
442
            desc: projectData[i].description,
            keywords: projectData[i].tag_list
443
          }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
444
          projectArr.push(project)  
445
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
446
447
448
449

        firstId = projectData[projectData.length-1].id
      }
    }
450
451

    res.render(lang+'/project/projectList', {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
452
      project: projectArr
Rosanny Sihombing's avatar
Rosanny Sihombing committed
453
      // http://pagination.js.org
Rosanny Sihombing's avatar
Rosanny Sihombing committed
454
455
456
    })
  })

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
  app.get('/projectlist', async function(req, res){
    let projectArr = []
    let pagesArr = []
    let isProject = true
    let firstId = 0
    let webname = "";

    while (isProject == true) {
      //
      //let runners = await getProjectsRunnersFromGitlab()
      //console.log(runners)
      //
      let projects = await getProjectsFromGitlab(10, firstId)
      let projectData = projects.data

      if (projectData.length == 0) {
        isProject = false
      }
      else {
        for(let i = 0; i < projectData.length; i++){
          // skip template project
          if (projectData[i].name == "template_gitlab_page") {
            continue
          }
          // M4_LAB logo for all projects that do not have logo
          if (projectData[i].avatar_url == null) {
            projectData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png"
          }
          // for all projects that have no description
          if (projectData[i].description == "") {
            projectData[i].description = "- no description -"
          }
          // websites
          if (projectData[i].tag_list.includes('website')) {
            // customize website name
            if (projectData[i].name == "Visualization") {
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/visualization"
            }
            else if (projectData[i].name == "IN-Source") {
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/INsource"
            }
            else if (projectData[i].name == "3DS_Visualization_Cesium") {
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/3ds_visualization_cesium"
            }
            else {
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/"+projectData[i].name
            }
            // remove 'website' from tag list
            const index = projectData[i].tag_list.indexOf('website');
            if (index > -1) {
              projectData[i].tag_list.splice(index, 1);
            }

            // fill in pagesArr
            let pages = {
              logo: projectData[i].avatar_url,
              name: projectData[i].name,
              weburl: projectData[i].web_url,
              desc: projectData[i].description,
              keywords: projectData[i].tag_list
            }
            pagesArr.push(pages)
            
          }
          else {
            let project = {
              logo: projectData[i].avatar_url,
              name: projectData[i].name,
              weburl: projectData[i].web_url,
              desc: projectData[i].description,
              keywords: projectData[i].tag_list
            }
            projectArr.push(project) 
          }
           
        }

        firstId = projectData[projectData.length-1].id
      }
    }

    res.render(lang+'/project/projectList', {
      project: projectArr,
      pages: pagesArr
      // http://pagination.js.org
    })
  })

Rosanny Sihombing's avatar
Rosanny Sihombing committed
545
};