routes-project.js 8.75 KB
Newer Older
1
2
const methods = require('./methods')
const async = require('async')
3
const pictSizeLimit = 1000000 // 1 MB
4
5
6
7
8
9

module.exports = function (app) {
 
  // ======== APP ROUTES - PROJECT ====================
  var lang = 'DE'

Rosanny Sihombing's avatar
Rosanny Sihombing committed
10
11
12
13
14
15
16
17
18
19
20
21
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,
                    project_title: mailinglistOverview[i].project_title
                });
            }

            res.render(lang+'/project/mailinglists', {
                isUserAuthenticated: req.isAuthenticated(),
                user: req.user,
                mailinglists: allMailingLists
            });
        }
    ])
  });

42
  app.get('/project', function (req, res) {
43
44
45
46
47
48
49
50
51
52
53
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
    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
          });
        }
      }
    ])
  })

95
  app.get('/project_', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
96
97
98
    res.render(lang+'/project/project-simplified');
  })

99
100
101
102
103
104
105
106
107
  app.get('/addprojectoverview', function (req, res) {
    if (req.isAuthenticated()) {
      res.render(lang+'/project/addProjectOverview')
    }
    else {
      res.redirect('/login')
    }
  })
  
Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
  app.post('/addprojectoverview__', function (req, res) {
109
110
111
112
113
    if (req.isAuthenticated()) {
      var wiki = 0
      if (req.body.wiki)
        wiki = 1

114
      var projectTerm = req.body.termForm + " - " + req.body.termTo
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      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,
129
        term: projectTerm,
130
131
132
133
        further_details: req.body.furtherDetails,
        website: req.body.website,
        src: req.body.src,
        caption: req.body.caption,
134
        contact_lastname: req.body.contactName,
135
        contact_email: req.body.contactEmail,
136
        leader_lastname: req.body.leaderName,
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        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');
        }
      })
    }
  })
153

Rosanny Sihombing's avatar
Rosanny Sihombing committed
154
155
156
157
158
159
  app.post('/addprojectoverview', function (req, res) {
    if (req.isAuthenticated()) {
      var wiki = 0
      if (req.body.wiki)
        wiki = 1

160
161
162
163
      var projectLogo = req.files.logo
      var projectPicture = req.files.src
      var projectLogoPath = './folder-in-server-to-save-projektlogo/'+req.body.pname+'/'+projectLogo.name
      var projectPicturePath = './folder-in-server-to-save-projektbild/'+req.body.pname+'/'+projectPicture.name
Rosanny Sihombing's avatar
Rosanny Sihombing committed
164
      var projectTerm = req.body.termForm + " - " + req.body.termTo
165
      
Rosanny Sihombing's avatar
Rosanny Sihombing committed
166
167
168
169
170
      var projectOverviewData = {
        pname: req.body.pname,
        title: req.body.title,
        onelinesummary: req.body.summary,
        category: req.body.category,
171
        logo: projectLogoPath,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
172
173
174
175
176
177
178
179
180
181
182
        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,
183
        src: projectPicturePath,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
184
185
186
187
188
189
190
        caption: req.body.caption,
        contact_lastname: req.body.contactName,
        contact_email: req.body.contactEmail,
        leader_lastname: req.body.leaderName,
        leader_email: req.body.leaderEmail
      }
      
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
      // raise error if limit is exceeded
      if (projectLogo && projectLogo.size === pictSizeLimit) {
        req.flash('error', 'Projektlogo exceeds 1 MB');
        res.redirect('/addprojectoverview');
      }
      if (projectPicture && projectPicture.size === pictSizeLimit) {
        req.flash('error', 'Projektbild exceeds 1 MB');
        res.redirect('/addprojectoverview');
      }

      // save pictures
      projectLogo.mv('./folder-in-server-to-save-projektlogo/'+req.body.pname+'/'+projectLogo.name, function(err) {
        if (err) {
          console.error(err)
          res.status(500).render(lang+'/500', {
            error: err
          })
        }
      });
      projectPicture.mv('./folder-in-server-to-save-projektbild/'+req.body.pname+'/'+projectPicture.name, function(err) {
        if (err) {
          console.error(err)
          res.status(500).render(lang+'/500', {
            error: err
          })
        }
      });

Rosanny Sihombing's avatar
Rosanny Sihombing committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
      /* 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) {
          methods.getUserIdByEmail(req.user.email, function(id, err) {
            if (!err) {
              userId = id
              done(err)
            }
          })
        },
        // add project overview
        function(done) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
235
236
237
238
239
240
241
242
          methods.addProjectOverview(projectOverviewData, function(data, err){
            if (err) {
              res.status(500).render(lang+'/500', {
                error: err
              })
            }
            else {
              done(err, data.insertId)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
            }
          })
        },
        // 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');
            }
          })
        }
      ])
    }
  })

269
270
271
272
273
274
275
  app.get('/updateprojectoverview', function (req, res) {
    // only their own project
  })

  app.post('/updateprojectoverview', function (req, res) {
    // only their own project
  })
276
277
  
};