Skip to content

Commit 5d84bc5

Browse files
Merge pull request #320 from joshglazer/issue/199/bulk-emails
Issue/199/bulk emails
2 parents b6c9c97 + 7f42abe commit 5d84bc5

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

src/routes/contact.js

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -156,50 +156,57 @@ router.post('/send', async (req, res) => {
156156
const emails = []
157157
const {entityIds, contactIds, relationshipTitle, entityType} = req.body
158158

159-
if (entityIds === undefined && contactIds === undefined) {
160-
const whereClause = {
161-
include: [{
162-
model: models.Entity,
163-
as: 'entities',
164-
required: true,
165-
through: {
166-
model: models.EntityContact,
167-
as: 'entityContacts',
168-
}
169-
}]
170-
}
159+
const whereClause = {
160+
include: [{
161+
model: models.Entity,
162+
as: 'entities',
163+
where: {},
164+
required: true,
165+
through: {
166+
model: models.EntityContact,
167+
as: 'entityContacts',
168+
}
169+
}]
170+
}
171171

172-
if (relationshipTitle) {
173-
whereClause.include[0].through.where = {relationshipTitle: relationshipTitle}
174-
}
172+
if (entityIds) {
173+
whereClause.include[0].where.id = entityIds
174+
}
175175

176-
if (entityType) {
177-
whereClause.include[0].where = {type: entityType}
178-
}
176+
if (contactIds) {
177+
whereClause.where = {id: contactIds}
178+
}
179179

180-
const contacts = await models.Contact.findAll(whereClause)
180+
if (relationshipTitle) {
181+
whereClause.include[0].through.where = {relationshipTitle: relationshipTitle}
182+
}
181183

182-
if (contacts.length < 1) {
183-
response.setCode(400)
184-
response.setMessage('No contacts to email')
185-
return res.status(response.getCode()).send(response.getMessage())
186-
}
184+
if (entityType) {
185+
whereClause.include[0].where.type = entityType
186+
}
187187

188-
for (const contact of contacts) {
189-
for (const entity of contact.entities) {
190-
if (contact.email !== null) {
191-
// short-lived temporary token that only lasts one hour
192-
const temporaryToken = await utils.getToken(contact.id, contact.email[0].address, 'contact')
193-
emails.push({
194-
email: contact.email[0].address,
195-
name: contact.name,
196-
entityName: entity.name,
197-
entityId: entity.id,
198-
entityType: entity.type,
199-
relationshipTitle: entity.entityContacts.dataValues.relationshipTitle,
200-
token: temporaryToken
201-
})
202-
}
188+
const contacts = await models.Contact.findAll(whereClause)
189+
190+
if (contacts.length < 1) {
191+
response.setCode(400)
192+
response.setMessage('No contacts to email')
193+
return res.status(response.getCode()).send(response.getMessage())
194+
}
195+
196+
for (const contact of contacts) {
197+
for (const entity of contact.entities) {
198+
if (contact.email !== null) {
199+
// short-lived temporary token that only lasts one hour
200+
const temporaryToken = await utils.getToken(contact.id, contact.email[0].address, 'contact')
201+
emails.push({
202+
email: contact.email[0].address,
203+
name: contact.name,
204+
entityName: entity.name,
205+
entityId: entity.id,
206+
entityType: entity.type,
207+
relationshipTitle: entity.entityContacts.dataValues.relationshipTitle,
208+
token: temporaryToken
209+
})
203210
}
204211
}
205212
}
@@ -208,10 +215,14 @@ router.post('/send', async (req, res) => {
208215
email.sendContactCheckInEmail(e)
209216
})
210217

218+
const uniqueEntities = [...new Set(emails.map(email => email.entityId))]
219+
220+
211221
response.setMessage({
212222
results: {
213223
message: 'Contacts emailed',
214-
total: emails.length
224+
totalContacts: emails.length,
225+
totalEntities: uniqueEntities.length,
215226
}
216227
})
217228
} catch (e) {

swagger.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,11 @@
15741574
"results": {
15751575
"type": "object",
15761576
"properties": {
1577-
"total": {
1577+
"totalContacts": {
1578+
"type": "number",
1579+
"example": 1
1580+
},
1581+
"totalEntities": {
15781582
"type": "number",
15791583
"example": 1
15801584
},

0 commit comments

Comments
 (0)