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 4, 2017
1 parent a85360d commit fe4bc4a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
30 changes: 21 additions & 9 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 All @@ -34,11 +35,11 @@ const placeholders = [
format: () => '<img src="cid:signature" alt="Signature" />'
},
{
id: 'items',
label: 'Donation Receipt Information',
id: 'receipt',
label: 'Donation Receipt',
type: placeholderTypes.EMAIL,
required: true,
format: value => table(value)
format: value => renderReceipt(value)
},
]

Expand Down Expand Up @@ -66,14 +67,25 @@ function getPlaceholders(types) {
return placeholders
}

function table(value) {
let tableFinal = `<table><tr><td><p>Item</p></td><td><p>Value</p></td></tr>`
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 < value.length; i++) {
tableFinal += `<tr><td><p>"${value[i].name}"</p></td><td><p>"${value[i].value}"</p></td></tr>`
total += value[i].value
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 tableFinal + `<tr><td><p>Total:</p></td><td><p>"${total}"</p></td></tr></table>`

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

export {
Expand Down
11 changes: 9 additions & 2 deletions server/lib/mail/mail-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {readFileSync} from 'fs'
import {resolve} from 'path'
import striptags from 'striptags'
import transformHtml, {h, getAttr, hasClass} from './html-transform'
import {trim, uniq} from 'lodash'
import {trim, uniq, isUndefined} from 'lodash'

import config from '../../config'
import Page from '../../models/page'
Expand Down Expand Up @@ -74,11 +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') {
return h(placeholder.format(bindings))
}

if (!placeholder) throw new Error('Invalid placeholder', id)
if (!bindings[id] && placeholder.type !== placeholderTypes.ATTACHMENT)

if (isUndefined(bindings[id]) && placeholder.type !== placeholderTypes.ATTACHMENT)
throw new Error('Missing binding for placeholder', id)

if (Array.isArray(attachments) && placeholder.type === placeholderTypes.ATTACHMENT)
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
2 changes: 1 addition & 1 deletion server/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const SettingsSchema = new Schema({
trim: true,
default: '$'
},
EIN: {
ein: {
type: String,
trim: true,
default: ''
Expand Down

0 comments on commit fe4bc4a

Please sign in to comment.