/
appRequestsCron.ts
70 lines (61 loc) · 2.24 KB
/
appRequestsCron.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
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
import { SyncedCron } from 'meteor/littledata:synced-cron';
import { settings } from '../../settings/server';
import { Apps } from './orchestrator';
import { getWorkspaceAccessToken } from '../../cloud/server';
import { appRequestNotififyForUsers } from './marketplace/appRequestNotifyUsers';
export const appsNotifyAppRequests = Meteor.bindEnvironment(function _appsNotifyAppRequests() {
try {
const installedApps = Promise.await(Apps.installedApps({ enabled: true }));
if (!installedApps || installedApps.length === 0) {
return;
}
const workspaceUrl = settings.get<string>('Site_Url');
const token = Promise.await(getWorkspaceAccessToken());
const baseUrl = Apps.getMarketplaceUrl();
if (!baseUrl) {
Apps.debugLog(`could not load marketplace base url to send app requests notifications`);
return;
}
const options = {
headers: {
Authorization: `Bearer ${token}`,
},
};
const pendingSentUrl = `${baseUrl}/v1/app-request/sent/pending`;
const result = HTTP.get(pendingSentUrl, options);
const data = result.data?.data;
const filtered = installedApps.filter((app) => data.indexOf(app.getID()) !== -1);
filtered.forEach((app) => {
const appId = app.getID();
const appName = app.getName();
const usersNotified = Promise.await<(string | Error)[]>(
appRequestNotififyForUsers(baseUrl, workspaceUrl, appId, appName)
.then((response) => {
// Mark all app requests as sent
HTTP.post(`${baseUrl}/v1/app-request/markAsSent/${appId}`, options);
return response;
})
.catch((err) => {
Apps.debugLog(`could not send app request notifications for app ${appId}. Error: ${err}`);
return err;
}),
);
const errors = usersNotified.filter((batch) => batch instanceof Error);
if (errors.length > 0) {
Apps.debugLog(`Some batches of users could not be notified for app ${appId}. Errors: ${errors}`);
}
});
} catch (err) {
Apps.debugLog(err);
}
});
// Scheduling as every 12 hours to avoid multiple instances hiting the marketplace at the same time
SyncedCron.add({
name: 'Apps-Request-End-Users:notify',
schedule: (parser) => parser.text('every 12 hours'),
job() {
appsNotifyAppRequests();
},
});