Skip to content

Commit

Permalink
(freeCodeCamp#119-3)To make it look a bit more receipt like, table wi…
Browse files Browse the repository at this point in the history
…th EIN
  • Loading branch information
ronaldblanco committed Oct 2, 2017
1 parent a85360d commit c6ad08e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
29 changes: 29 additions & 0 deletions common/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const placeholders = [
{id: 'url', label: 'Foodbank Website'},
{id: 'clientIntakeNumber', label: 'Client Intake Number'},
{id: 'supportNumber', label: 'Support Number'},
{id: 'ein', label: 'Employer Identification Number'},

{id: 'firstName', label: 'User First Name', type: placeholderTypes.EMAIL},
{id: 'lastName', label: 'User Last Name', type: placeholderTypes.EMAIL},
Expand Down Expand Up @@ -40,6 +41,13 @@ const placeholders = [
required: true,
format: value => table(value)
},
{
id: 'receipt',
label: 'Donation Receipt',
type: placeholderTypes.EMAIL,
required: true,
format: value => renderReceipt(value)
},
]

function getPagePlaceholders() {
Expand Down Expand Up @@ -76,6 +84,27 @@ function table(value) {
return tableFinal + `<tr><td><p>Total:</p></td><td><p>"${total}"</p></td></tr></table>`
}

function renderReceipt(values) {
const ids = ['organization', 'address', 'url', 'supportNumber', 'ein']
let table = '<table>'

for (let i = 0; i < ids.length; i++) {
const placeholder = placeholders.find(p => p.id === ids[i])
table += `<tr><td>${placeholder.label}:</td><td> ${values[ids[i]]}</td></tr>`
}

table += `<tr><td><p>Here you have a list of the donations our organization recibed from you:</p></td></tr>`
const items = values.items
table += `<tr><td><p>Item</p></td><td><p>Value</p></td></tr>`
let total = 0
for (let i = 0; i < items.length; i++) {
table += `<tr><td><p>"${items[i].name}"</p></td><td><p>"${items[i].value}"</p></td></tr>`
total += items[i].value
}

return table + `<tr><td><p>Total:</p></td><td><p>"${total}"</p></td></tr></table>`
}

export {
placeholders as default,
placeholderTypes,
Expand Down
2 changes: 1 addition & 1 deletion server/config/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ export default async function sendEmail(
path: '/v3/mail/send',
body: mail.toJSON()
})

//console.log(content)//Only for tests
return sg.API(request)
}
9 changes: 9 additions & 0 deletions server/lib/mail/mail-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,18 @@ function transformSubject(subject, bindings) {
}

function bindPlaceholder(node, bindings, attachments) {

const id = getAttr(node, 'data-id')
const placeholder = placeholders.find(pl => pl.id === id)

//Receipt Placeholder only
if (id === 'receipt') {
let receipt = {organization: bindings.organization, address: bindings.address, url: bindings.url, clientIntakeNumber: bindings.clientIntakeNumber, supportNumber: bindings.supportNumber, ein: bindings.ein, items: bindings.items}
return typeof placeholder.format === 'function' ?
h(placeholder.format(receipt)) :
h('#text', receipt)
}

if (!placeholder) throw new Error('Invalid placeholder', id)
if (!bindings[id] && placeholder.type !== placeholderTypes.ATTACHMENT)
throw new Error('Missing binding for placeholder', id)
Expand Down
5 changes: 3 additions & 2 deletions server/lib/seed/seed-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ export const pages = [{
type: pageTypes.EMAIL,
subject: '<p>Receipt for Your Donation to <span class="ql-placeholder-content" data-id="organization" data-label="Foodbank Name"></span></p>',
body: `<p>Dear <span class="ql-placeholder-content" data-id="fullName" data-label="User Full Name"></span>,</p>
<p>Here you have a list of the donations our organization recibed from you:</p>
<p><span class="ql-placeholder-content" data-id="items" data-label="Donation Receipt information" data-required="true"></span></p>`
<p>Thanks for your donations to:</p>
<p><span class="ql-placeholder-content" data-id="receipt" data-label="Donation Receipt" data-required="true"></span></p>
`
}]

const commonFields = [
Expand Down
4 changes: 2 additions & 2 deletions server/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ const SettingsSchema = new Schema({
trim: true,
default: '$'
},
EIN: {
ein: {
type: String,
trim: true,
default: ''
default: '0'
},
})

Expand Down

0 comments on commit c6ad08e

Please sign in to comment.