-
Notifications
You must be signed in to change notification settings - Fork 19
/
workpackageHelper.js
63 lines (62 loc) · 2.23 KB
/
workpackageHelper.js
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
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
export const workpackageHelper = {
async getColorAttributes(path, id) {
const url = generateUrl(path + id)
let response
try {
response = await axios.get(url)
} catch (e) {
response = e.response
}
return (response.status === 200 && response.data?.color)
? response.data.color
: ''
},
replaceHrefToGetId(href) {
// this is a helper method replaces the string like this "/api/v3/types/3" to get id
return href
? href.replace(/.*\//, '')
: null
},
async getAdditionalMetaData(workPackage) {
if (typeof workPackage._links.status.href !== 'string'
|| workPackage._links.status.href === ''
|| typeof workPackage._links.type.href !== 'string'
|| workPackage._links.type.href === ''
|| typeof workPackage.id !== 'number'
|| typeof workPackage.subject !== 'string'
|| workPackage.subject === ''
|| typeof workPackage._links.project.title !== 'string'
|| workPackage._links.project.title === ''
|| typeof workPackage._links.status.title !== 'string'
|| workPackage._links.status.title === ''
|| typeof workPackage._links.type.title !== 'string'
|| workPackage._links.type.title === ''
) {
throw new Error('missing data in workpackage object')
}
const statusId = this.replaceHrefToGetId(workPackage._links.status.href)
const typeId = this.replaceHrefToGetId(workPackage._links.type.href)
const userId = this.replaceHrefToGetId(workPackage._links.assignee.href)
const userName = workPackage._links.assignee.title
const avatarUrl = generateUrl('/apps/integration_openproject/avatar?')
+ encodeURIComponent('userId')
+ '=' + userId
+ '&' + encodeURIComponent('userName')
+ '=' + userName
const statusColor = await this.getColorAttributes('/apps/integration_openproject/statuses/', statusId)
const typeColor = await this.getColorAttributes('/apps/integration_openproject/types/', typeId)
return {
id: workPackage.id,
subject: workPackage.subject,
project: workPackage._links.project.title,
statusTitle: workPackage._links.status.title,
typeTitle: workPackage._links.type.title,
assignee: userName,
statusCol: statusColor,
typeCol: typeColor,
picture: avatarUrl,
}
},
}