forked from cloud-gov/pages-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (156 loc) · 5.95 KB
/
index.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
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
const moment = require('moment');
const PromisePool = require('@supercharge/promise-pool');
const { BuildTasksQueue, MailQueue, SiteBuildsQueue } = require('../queues');
const Templates = require('../services/mailer/templates');
const { truncateString } = require('../utils');
const {
app: { hostname, appEnv, appName },
} = require('../../config');
class QueueJobs {
constructor(connection) {
this.siteBuildsQueue = new SiteBuildsQueue(connection);
this.buildTasksQueue = new BuildTasksQueue(connection);
this.mailQueue = new MailQueue(connection);
}
/**
* Adds a send alert email job to the Mailer Queue
* to org users about their sandbox sites being removed
* @async
* @method sendAlert
* @param {string} reason - The reason for the alert
* @param {string} errors: The errors associated to the alert,
* @return {Promise<{Object}>} The bullmq's queue add job response
*/
async sendAlert(reason, errors) {
await this.mailQueue.waitUntilReady();
return this.mailQueue.add('alert', {
to: ['federalist-alerts@gsa.gov'],
subject: `${appName} ${appEnv} Alert | ${reason}`,
html: Templates.alert({ errors, reason }),
});
}
/**
* Adds a send Sandbox Reminder email job to the Mailer Queue
* to org users about their sandbox sites being removed
* @async
* @method sendSandboxReminder
* @param {Object} organization - The organization
* @param {string} organization.id: organizationId,
* @param {string} organization.name: organizationName,
* @param {Object[]} organization.Sites: Array of org sites
* @param {Object[]} organization.Users: Array of org users
* @return {Promise<{Object}>} The bullmq's queue add job response
*/
async sendSandboxReminder(organization) {
const {
id: organizationId,
name: organizationName,
Sites: sites,
Users: users,
} = organization;
const dateStr = moment(organization.sandboxNextCleaningAt).format('MMMM DD, YYYY');
// eslint-disable-next-line max-len
const subject = `Your Pages sandbox organization's sites will be removed in ${organization.daysUntilSandboxCleaning} days`;
await this.mailQueue.waitUntilReady();
const { results, errors } = await PromisePool.for(users).process((user) => {
if (!user.UAAIdentity?.email) {
throw new Error('User lacks UAA email');
}
return this.mailQueue.add('sandbox-reminder', {
to: [user.UAAIdentity.email],
subject,
html: Templates.sandboxReminder({
organizationName,
dateStr,
organizationId,
sites: sites.map(({ id, owner, repository }) => ({
id,
owner,
repository,
})),
hostname,
}),
});
});
if (errors.length) {
const errMsg = [
// eslint-disable-next-line max-len
`Failed to queue ${errors.length === 1 ? 'a sandbox reminder' : 'sandbox reminders'} ` +
`for organization@id=${organizationId} members:\n`,
errors.map((e) => ` user@id=${e.item.id}: ${e.message}`).join('\n'),
].join();
throw new Error(errMsg);
}
return results;
}
/**
* Adds a send UAA Invite job to the Mailer Queue
* @async
* @method sendUAAInvite
* @param {string} email - The email to send the invite to
* @param {string} link - The invite link
* @param {string} origin - The UAA origin of the email
* @param {string} orgName - The organization name the email is being invited to
* @return {Promise<{Object}>} The bullmq's queue add job response
*/
async sendUAAInvite(email, link, origin, orgName) {
await this.mailQueue.waitUntilReady();
if (origin === 'cloud.gov' || origin === 'uaa') {
return this.mailQueue.add('uaa-invite', {
to: [email],
subject: 'Invitation to join cloud.gov Pages',
html: Templates.uaaInvite({ link }),
});
}
return this.mailQueue.add('uaa-invite', {
to: [email],
subject: 'Invitation to join cloud.gov Pages',
html: Templates.uaaIDPInvite({
link: hostname,
orgName,
}),
});
}
/**
* Adds a site build task job to the Build Tasks Queue
* The build's branch, site owner, and site repository attributes
* are used to creat the name of the job added to the queue
* @async
* @method startBuildTask
* @param {Object} buildTask - An instance of the model Build Taks
* @param {number} build.id - The build task primary key
* @param {Object} build.BuildTaskType - The instance's related build task type instance
* @param {string} build.BuildTaskType.name - The build task type instance name
* @return {Promise<{Object}>} The bullmq's queue add job response
*/
async startBuildTask(buildTask, priority) {
const {
id: buildTaskId,
BuildTaskType: { name },
} = buildTask;
await this.buildTasksQueue.waitUntilReady();
return this.buildTasksQueue.add(name, { buildTaskId }, { priority });
}
/**
* Adds a site build job to the Site Builds Queue
* The build's branch, site owner, and site repository attributes
* are used to creat the name of the job added to the queue
* @async
* @method startSiteBuild
* @param {Object} build - An instance of the model Build
* @param {number} build.id - The build primary key
* @param {string} build.branch - The git branch for the site build
* @param {Object} build.Site - The build instance's related site
* @param {string} build.Site.owner - The site's owner
* @param {string} build.Site.repository - The site's repository
* @return {Promise<{Object}>} The bullmq's queue add job response
*/
async startSiteBuild(build, priority) {
const { branch, id: buildId, Site } = build;
const { owner, repository } = Site;
const jobName = `${owner}/${repository}: ${truncateString(branch)}`;
await this.siteBuildsQueue.waitUntilReady();
return this.siteBuildsQueue.add(jobName, { buildId }, { priority });
}
}
module.exports = QueueJobs;