-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetProjects.ts
174 lines (166 loc) · 5.97 KB
/
getProjects.ts
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
import {
IHttp,
IModify,
IPersistence,
IRead
} from '@rocket.chat/apps-engine/definition/accessors';
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { getAccessTokenForUser } from '../storage/users';
import { Subscription } from '../sdk/webhooks.sdk';
import { storedRoomData } from '../definition';
import {
botMessageChannel,
botNotifyCurrentUser,
sendMessage
} from './messages';
import { TextObjectType } from '@rocket.chat/apps-engine/definition/uikit';
import { blockAction } from '../enums/enums';
import { getProjectFilesUrl } from './const';
export async function getProjects(
modify: IModify,
context,
persistence: IPersistence,
read: IRead,
data,
room: IRoom,
user: IUser,
http: IHttp
) {
const subscriptionStorage = new Subscription(
persistence,
read.getPersistenceReader()
);
const token = await getAccessTokenForUser(read, user);
const headers: { Authorization: string } = {
Authorization: `Bearer ${token?.token}`
};
function removeDuplicates(arr: string[]) {
const unique: string[] = [];
arr.forEach((element: string) => {
if (!unique.includes(element)) {
unique.push(element);
}
});
return unique;
}
subscriptionStorage
.getAllSubscriptions()
.then(async (subscriptions) => {
if (!subscriptions) {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Could not find any project subscribed in this room'
);
}
const room_projects_ids: string[] = [];
for (const subscription of subscriptions) {
const roomData: storedRoomData[] = subscription.room_data;
for (const room_data of roomData) {
if (
room_data.room_Id === room.id &&
room_data.project_Ids
) {
room_projects_ids.push(...room_data.project_Ids);
}
}
}
if (room_projects_ids.length === 0) {
console.log('send error message');
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Could not find any projects subscribed by you in this room'
);
}
const projectDataRequestUrl = removeDuplicates(
room_projects_ids
).map(
(projectId) =>
getProjectFilesUrl(projectId)
);
try {
await Promise.all(
projectDataRequestUrl.map((url) =>
http.get(url, {
headers
})
)
)
.then(async (project_response) => {
if (project_response.length > 0) {
const projectDetails = project_response.map(
(response) => response.data
);
const projectName = projectDetails.map(
(project) => project.name
);
const block = modify.getCreator().getBlockBuilder();
block.addSectionBlock({
text: {
type: TextObjectType.PLAINTEXT,
text: 'Projects Subscribed in this room'
}
});
projectName.map((projectName) => {
block.addSectionBlock({
text: {
type: TextObjectType.MARKDOWN,
text: `> ${projectName}`
}
});
});
botNotifyCurrentUser(
read,
modify,
user,
room,
'',
block
);
return;
} else {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Could not find any projects subscribed by you in this room'
);
}
})
.catch(async () => {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Error in fetching files details'
);
});
} catch (e) {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Error in fetching projects. Please Report this issue'
);
}
})
.catch(async (error) => {
console.log('error: getting all subscriptions - ', error);
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'error getting all subscriptions'
);
});
}