routes-project.js 15.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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
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
108
    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
109
  app.get('/', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
110
111
112
113
114
115
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
257
258
259
260
261
262
263
    res.render(lang+'/project/project-simplified', {
       isUserAuthenticated: req.isAuthenticated(),
       user: req.user
    });
  })

  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
            })
          }
        });
      }

      /* 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.
      */
      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
314
315
316
            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
  })

  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
  })

Rosanny Sihombing's avatar
Rosanny Sihombing committed
359
360
361
362
363
364
  async function getProjectsFromGitlab(perPage, idAfter) {
    // public projects
    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)
  }

365
366
  // Projektdaten
  app.get('/projektdaten', async function(req, res){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    let projectArr = []
    let isProject = true
    let firstId = 0

    while (isProject == true) {
      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
          }
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
          // only repo
          if (!projectData[i].tag_list.includes('website')) {
            // 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 -"
            }

            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)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
403
          }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
        }

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

    res.render(lang+'/project/projectList', {
      project: projectArr
    })
  })

  // Projektinformationen
  app.get('/projektinformationen', async function(req, res){
    let pagesArr = []
    let isProject = true
    let firstId = 0

    while (isProject == true) {
      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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
433
          }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
434
435
          // websites
          if (projectData[i].tag_list.includes('website')) {
436
437
438
439
440
441
442
443
            // 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 -"
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
444
445
            // customize website name
            if (projectData[i].name == "Visualization") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
446
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/visualization"
Rosanny Sihombing's avatar
Rosanny Sihombing committed
447
448
            }
            else if (projectData[i].name == "IN-Source") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
449
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/INsource"
Rosanny Sihombing's avatar
Rosanny Sihombing committed
450
451
            }
            else if (projectData[i].name == "3DS_Visualization_Cesium") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
452
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/3ds_visualization_cesium"
Rosanny Sihombing's avatar
Rosanny Sihombing committed
453
454
            }
            else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
455
              projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/"+projectData[i].name
Rosanny Sihombing's avatar
Rosanny Sihombing committed
456
            }
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
            // 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)

Rosanny Sihombing's avatar
Rosanny Sihombing committed
473
474
475
476
477
478
479
          }
        }

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

480
    res.render(lang+'/project/pagesList', {
481
      pages: pagesArr
Rosanny Sihombing's avatar
Rosanny Sihombing committed
482
483
484
    })
  })

Rosanny Sihombing's avatar
Rosanny Sihombing committed
485
};