controller.ts 8.19 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
8
9
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
42
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
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
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
import helpers from '../functions/helpers'
import gitlab from '../functions/gitlab'
import https from 'https'

const dbconn = require('../config/dbconn')
const lang = 'DE'

const controller = {
  getAllMailinglists: async function () {
    try {
      const rows: any = await dbconn.project.promise().query('CALL getAllLists')
      console.log(rows)
      if (rows[0][0]) {
        return rows[0][0]
      } else { return null }
    } catch (err) {
      console.error(err)
    }
  },
  getProjectOverviewById: async function (projectId: number) {
    try {
      const rows: any = await dbconn.project.promise().query('CALL GetProjectInformationByProjectID(' + projectId + ')')
      if (rows[0][0]) {
        return rows[0][0]
      } else { return null }
    } catch (err) {
      console.error(err)
    }
  },
  getProjectImagesById: async function (projectId: number) {
    try {
      const rows: any = await dbconn.project.promise().query('CALL getImagesByProjectID(' + projectId + ')')
      if (rows[0][0]) {
        return rows[0][0]
      } else { return null }
    } catch (err) {
      console.error(err)
    }
    return null
  },
  showHome: function (res: any) {
    res.render(lang + '/project/project-simplified')
  },
  showMailingList: async function (res: any) {
    const mailList = await controller.getAllMailinglists()
    const allMailingLists: any = [] // JSON object
    if (mailList) {
      for (let i = 0; i < mailList.length; i++) {
        // add data to JSON object
        allMailingLists.push({
          id: mailList[i].id,
          name: mailList[i].name,
          src: mailList[i].src,
          projectstatus: mailList[i].projectstatus,
          project_title: mailList[i].project_title,
          keywords: mailList[i].keywords
        })
      }
    }
    res.render(lang + '/project/mailinglists', {
      mailinglists: allMailingLists
    })
  },
  showProjOverview: async function (req: any, res: any, next: any) {
    const projectId = req.query.projectID
    const projectOverview = await controller.getProjectOverviewById(projectId)

    if (!projectId || projectOverview.length === 0) {
      next()
    } else {
      const partnerWebsites = helpers.stringToArray(projectOverview[0].partner_website)
      const partnerNames = helpers.stringToArray(projectOverview[0].partner_name)
      const awardSites = helpers.stringToArray(projectOverview[0].award_website)
      const awardNames = helpers.stringToArray(projectOverview[0].award_name)
      const sponsorWebsites = helpers.stringToArray(projectOverview[0].sponsor_website)
      const sponsorImgs = helpers.stringToArray(projectOverview[0].sponsor_img)
      const sponsorNames = helpers.stringToArray(projectOverview[0].sponsor_name)

      const projectImages = await controller.getProjectImagesById(projectId)
      res.render(lang + '/project/projectOverview', {
        projectOV: projectOverview,
        projectImgs: projectImages,
        partnerWS: partnerWebsites,
        partnerN: partnerNames,
        awardWS: awardSites,
        awardN: awardNames,
        sponsorWS: sponsorWebsites,
        sponsorIMG: sponsorImgs,
        sponsorN: sponsorNames
      })
    }
  },
  showProjData: async function (req: any, res: any) {
    const projectArr: any = []
    let isProject = true
    let firstId = 0
    const orderKeyword = req.query.sort

    while (isProject) {
      const projects: any = await gitlab.getProjects(100, firstId)
      const projectData = projects.data[0]

      if (projectData.length === 0) {
        isProject = false
      } else {
        for (let i = 0; i < projectData.length; i++) {
          // 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/body/M4_LAB_LOGO_NO_TEXT.png'
          }
          // for all projects that have no description
          if (projectData[i].description === '') {
            projectData[i].description = '- no description -'
          }

          const project = {
            logo: projectData[i].avatar_url,
            name: projectData[i].name,
            weburl: projectData[i].web_url,
            desc: projectData[i].description,
            keywords: projectData[i].tag_list,
            createdAt: projectData[i].created_at,
            lastUpdatedAt: projectData[i].last_activity_at
          }
          projectArr.push(project)
        }
        firstId = projectData[projectData.length - 1].id
      }

      // MLAB-576
      if (orderKeyword === 'created_at') {
        projectArr.sort((a: any, b: any) => {
          const aDate: any = new Date(a.createdAt)
          const bDate: any = new Date(b.createdAt)
          return bDate - aDate
        })
      } else if (orderKeyword === 'updated_at') {
        projectArr.sort((a: any, b: any) => {
          const aDate: any = new Date(a.lastUpdatedAt)
          const bDate: any = new Date(b.lastUpdatedAt)
          return bDate - aDate
        })
      } else { // default, sorted by name
        projectArr.sort((a: any, b: any) => {
          const fa = a.name.toLowerCase()
          const fb = b.name.toLowerCase()

          if (fa < fb) return -1
          if (fa > fb) return 1
          return 0
        })
      }
    }

    res.render(lang + '/project/projectList', {
      project: projectArr
    })
  },
  showProjInformations: async function (req: any, res: any) {
    const pagesArr: any = []
    let isProject = true
    let firstId = 0
    const orderKeyword = req.query.sort

    while (isProject) {
      const projects: any = await gitlab.getProjects(100, firstId)
      const pagesData = projects.data[1]

      if (pagesData.length === 0) {
        isProject = false
      } else {
        for (let i = 0; i < pagesData.length; i++) {
          const status = await gitlab.getLatestPipelineStatus(pagesData[i].id)
          if (status) {
            // 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/body/M4_LAB_LOGO_NO_TEXT.png'
            }
            // for all projects that have no description
            if (pagesData[i].description === '') {
              pagesData[i].description = '- no description -'
            }
            // https://transfer.hft-stuttgart.de/pages/EIGENTUEMER/PROJEKTNAME/
            pagesData[i].web_url = 'https://transfer.hft-stuttgart.de/pages/' + String(pagesData[i].namespace.path) + '/' + String(pagesData[i].name) + '/'
            // remove 'website' from tag list
            const index = pagesData[i].tag_list.indexOf('website')
            if (index > -1) {
              pagesData[i].tag_list.splice(index, 1)
            }

            // fill in pagesArr
            const pages = {
              logo: pagesData[i].avatar_url,
              name: pagesData[i].name,
              weburl: pagesData[i].web_url,
              desc: pagesData[i].description,
              keywords: pagesData[i].tag_list,
              createdAt: pagesData[i].created_at,
              lastUpdatedAt: pagesData[i].last_activity_at
            }
            https.get(pagesData[i].web_url, function (response: any) {
              if (response.statusCode >= 200 && response.statusCode <= 299) {
                pagesArr.push(pages)
              }
            })
          }
        }
        firstId = pagesData[pagesData.length - 1].id
      }

      // MLAB-576
      if (orderKeyword === 'created_at') {
        pagesArr.sort((a: any, b: any) => {
          const aDate: any = new Date(a.createdAt)
          const bDate: any = new Date(b.createdAt)
          return bDate - aDate
        })
      } else if (orderKeyword === 'updated_at') {
        pagesArr.sort((a: any, b: any) => {
          const aDate: any = new Date(a.lastUpdatedAt)
          const bDate: any = new Date(b.lastUpdatedAt)
          return bDate - aDate
        })
      } else { // default, sorted by name
        pagesArr.sort((a: any, b: any) => {
          const fa = a.name.toLowerCase()
          const fb = b.name.toLowerCase()

          if (fa < fb) return -1
          if (fa > fb) return 1
          return 0
        })
      }
    }

    res.render(lang + '/project/pagesList', {
      pages: pagesArr
    })
  }
}

export = controller