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

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

  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
39
40
                    project_title: mailinglistOverview[i].project_title,
                    keywords: mailinglistOverview[i].keywords
Rosanny Sihombing's avatar
Rosanny Sihombing committed
41
42
43
44
45
46
47
48
49
50
                });
            }

            res.render(lang+'/project/mailinglists', {
                isUserAuthenticated: req.isAuthenticated(),
                user: req.user,
                mailinglists: allMailingLists
            });
        }
    ])
Rosanny Sihombing's avatar
Rosanny Sihombing committed
51
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
52

Rosanny Sihombing's avatar
Rosanny Sihombing committed
53
  app.get('/', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
54
55
56
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
109
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
    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 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
162
          methods.getUserIdByEmail(req.user.email, function(id, err) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
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
            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.post('/updateprojectoverview', function (req, res) {
    // only their own project
  })

  app.get('/projectoverview', function(req, res){
    async.waterfall([
Rosanny Sihombing's avatar
Rosanny Sihombing committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
      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
235

Rosanny Sihombing's avatar
Rosanny Sihombing committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        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
251
252
  })

253
254
  // Projektdaten
  app.get('/projektdaten', async function(req, res){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
255
256
257
258
259
    let projectArr = []
    let isProject = true
    let firstId = 0

    while (isProject == true) {
260
      let projects = await gitlab.getRepos(100, firstId)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
261
262
263
264
265
266
267
      let projectData = projects.data

      if (projectData.length == 0) {
        isProject = false
      }
      else {
        for(let i = 0; i < projectData.length; i++){
268
269
270
271
272
273
274
          // 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
275
          }
276

277
278
279
280
281
282
          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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
283
          }
284
          projectArr.push(project)
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
        }
        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

301
302
303
304
305
306
307
308
309
    let runnerProjects = await gitlab.getProjectsFromRunners()
    if(runnerProjects.error) {
      // error response: to be updated
      res.status(500).render(lang+'/500', { error: err })
    } else {
      let runnerProjectsData = runnerProjects.data
      let runnerProjectsIds = [] 
      for(let i = 0; i < runnerProjectsData.length; i++){
        runnerProjectsIds.push(runnerProjectsData[i].id)
310
      }
311

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
      while (isProject == true) {
        let pages = await gitlab.getPages(100, firstId)
        let pagesData = pages.data

        if (pagesData.length == 0) {
          isProject = false
        } else {
          for(let i = 0; i < pagesData.length; i++){
            //  ONLY IF THE PROJECT IS AVAILABLE IN THE RUNNER
            if(runnerProjectsIds.indexOf(pagesData[i].id) > -1) {
              // M4_LAB logo for all projects that do not have logo
              if (pagesData[i].avatar_url == null) {
                pagesData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png"
              }
              // for all projects that have no description
              if (pagesData[i].description == "") {
                pagesData[i].description = "- no description -"
              }
              // customize website name
              if (pagesData[i].name == "Visualization") {
                pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/visualization"
              }
              else if (pagesData[i].name == "IN-Source") {
                pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/INsource"
              }
              else if (pagesData[i].name == "3DS_Visualization_Cesium") {
                pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/3ds_visualization_cesium"
              }
              else {
                pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/"+pagesData[i].name
              }
              // remove 'website' from tag list
              let index = pagesData[i].tag_list.indexOf('website')
              if (index > -1) {
                pagesData[i].tag_list.splice(index, 1)
              }

              // fill in pagesArr
              let pages = {
                logo: pagesData[i].avatar_url,
                name: pagesData[i].name,
                weburl: pagesData[i].web_url,
                desc: pagesData[i].description,
                keywords: pagesData[i].tag_list
              }
              pagesArr.push(pages)
358
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
359
          }
360
          firstId = pagesData[pagesData.length-1].id
Rosanny Sihombing's avatar
Rosanny Sihombing committed
361
        }
362
      }        
Rosanny Sihombing's avatar
Rosanny Sihombing committed
363
364
    }

365
    res.render(lang+'/project/pagesList', {
366
      pages: pagesArr
Rosanny Sihombing's avatar
Rosanny Sihombing committed
367
368
369
    })
  })

Rosanny Sihombing's avatar
Rosanny Sihombing committed
370
};