Skip to content

Commit

Permalink
feat: load email templates from files instead of database
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvanherwijnen committed Sep 9, 2024
1 parent c9096d3 commit 9054bed
Show file tree
Hide file tree
Showing 23 changed files with 439 additions and 242 deletions.
249 changes: 124 additions & 125 deletions packages/api/src/kysely/seeds/data.ts

Large diffs are not rendered by default.

158 changes: 76 additions & 82 deletions packages/api/src/kysely/seeds/test.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,121 @@
import bcrypt from 'bcrypt'
import { c } from 'compress-tag'
import { db } from '../index.js'
import type { Insertable } from 'kysely'
import type {
Clients,
Companies,
EmailTemplates,
NumberPrefixes
} from '../types.js'
import type { Clients, Companies, NumberPrefixes } from '../types.js'

const ADMIN_PASSWORD = 'Sif5uEG5hcTH'

// Double backslash to escape compress-tag
const emailTemplates: Insertable<EmailTemplates>[] = [
{
name: 'sendInvoice',
locale: 'nl',
subject: `Factuur \\{{numberPrefix}}\\{{number}}`,
body: c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
// const emailTemplates: Insertable<EmailTemplates>[] = [
// {
// name: 'sendInvoice',
// locale: 'nl',
// subject: `Factuur \\{{numberPrefix}}\\{{number}}`,
// body: c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>

<p>Bijgevoegd treft u factuur \\{{numberPrefix}}\\{{number}} aan voor de geleverde diensten.</p>
// <p>Bijgevoegd treft u factuur \\{{numberPrefix}}\\{{number}} aan voor de geleverde diensten.</p>

<p>Gelieve het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} te betalen binnen {{paymentTermDays}} dagen (voor \\{{dueDate}}) op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>
// <p>Gelieve het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} te betalen binnen {{paymentTermDays}} dagen (voor \\{{dueDate}}) op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>

<p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.
// <p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.

<p>Mocht u nog vragen hebben over deze factuur, dan kunt u contact met ons opnemen.</p>
// <p>Mocht u nog vragen hebben over deze factuur, dan kunt u contact met ons opnemen.</p>

<p>Met vriendelijke groet,</p>
// <p>Met vriendelijke groet,</p>

<p>{{companyDetails.name}}</p>`
},
{
name: 'remindInvoice',
locale: 'nl',
subject: `Betalingsherinnering factuur \\{{numberPrefix}}\\{{number}}`,
body: c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
// <p>{{companyDetails.name}}</p>`
// },
// {
// name: 'remindInvoice',
// locale: 'nl',
// subject: `Betalingsherinnering factuur \\{{numberPrefix}}\\{{number}}`,
// body: c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>

<p>Uit onze administratie blijkt dat factuur \\{{numberPrefix}}\\{{number}} nog niet is voldaan.</p>
// <p>Uit onze administratie blijkt dat factuur \\{{numberPrefix}}\\{{number}} nog niet is voldaan.</p>

<p>Wij willen u verzoeken het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} alsnog binnen 7 dagen te betalen op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>
// <p>Wij willen u verzoeken het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} alsnog binnen 7 dagen te betalen op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>

<p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.
// <p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.

<p>Mocht u nog vragen hebben over deze factuur, dan kunt u contact met ons opnemen.</p>
// <p>Mocht u nog vragen hebben over deze factuur, dan kunt u contact met ons opnemen.</p>

<p>Met vriendelijke groet,</p>
// <p>Met vriendelijke groet,</p>

<p>{{companyDetails.name}}</p>`
},
{
name: 'exhortInvoice',
locale: 'nl',
subject: `Aanmaning factuur \\{{numberPrefix}}\\{{number}}`,
body: c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
// <p>{{companyDetails.name}}</p>`
// },
// {
// name: 'exhortInvoice',
// locale: 'nl',
// subject: `Aanmaning factuur \\{{numberPrefix}}\\{{number}}`,
// body: c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>

<p>Uit onze administratie blijkt dat factuur \\{{numberPrefix}}\\{{number}} nog niet is voldaan.</p>
// <p>Uit onze administratie blijkt dat factuur \\{{numberPrefix}}\\{{number}} nog niet is voldaan.</p>

<p>Wij willen u verzoeken het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} alsnog binnen 5 dagen te betalen op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>
// <p>Wij willen u verzoeken het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} alsnog binnen 5 dagen te betalen op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>

<p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.
// <p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.

<p>Indien de betaling uitblijft zijn we genoodzaakt verdere juridische stappen te nemen.</p>
// <p>Indien de betaling uitblijft zijn we genoodzaakt verdere juridische stappen te nemen.</p>

<p>Met vriendelijke groet,</p>
// <p>Met vriendelijke groet,</p>

<p>{{companyDetails.name}}</p>`
},
{
name: 'sendInvoice',
locale: 'en-US',
subject: `Invoice \\{{numberPrefix}}\\{{number}}`,
body: c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
// <p>{{companyDetails.name}}</p>`
// },
// {
// name: 'sendInvoice',
// locale: 'en-US',
// subject: `Invoice \\{{numberPrefix}}\\{{number}}`,
// body: c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>

<p>In the attachment you can find invoice \\{{numberPrefix}}\\{{number}} for our services.</p>
// <p>In the attachment you can find invoice \\{{numberPrefix}}\\{{number}} for our services.</p>

<p>We would like to ask you to pay the amount of {{totalIncludingTax}} within {{paymentTermDays}} days (before \\{{dueDate}}) on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>
// <p>We would like to ask you to pay the amount of {{totalIncludingTax}} within {{paymentTermDays}} days (before \\{{dueDate}}) on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>

<p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.
// <p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.

<p>If you have any questions about this invoice, you can contact us.</p>
// <p>If you have any questions about this invoice, you can contact us.</p>

<p>Kind regards,</p>
// <p>Kind regards,</p>

<p>{{companyDetails.name}}</p>`
},
{
name: 'remindInvoice',
locale: 'en-US',
subject: `Payment reminder invoice \\{{numberPrefix}}\\{{number}}`,
body: c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
// <p>{{companyDetails.name}}</p>`
// },
// {
// name: 'remindInvoice',
// locale: 'en-US',
// subject: `Payment reminder invoice \\{{numberPrefix}}\\{{number}}`,
// body: c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>

<p>According to our administration the payment for invoice \\{{numberPrefix}}\\{{number}} is overdue.</p>
// <p>According to our administration the payment for invoice \\{{numberPrefix}}\\{{number}} is overdue.</p>

<p>We would like to ask you to pay the open amount of {{totalIncludingTax}} within 7 days on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>
// <p>We would like to ask you to pay the open amount of {{totalIncludingTax}} within 7 days on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>

<p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.
// <p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.

<p>If you have any questions about this invoice, you can contact us.</p>
// <p>If you have any questions about this invoice, you can contact us.</p>

<p>Kind regards,</p>
// <p>Kind regards,</p>

<p>{{companyDetails.name}}</p>`
},
{
name: 'exhortInvoice',
locale: 'en-US',
subject: `Exhortation invoice \\{{numberPrefix}}\\{{number}}`,
body: c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
// <p>{{companyDetails.name}}</p>`
// },
// {
// name: 'exhortInvoice',
// locale: 'en-US',
// subject: `Exhortation invoice \\{{numberPrefix}}\\{{number}}`,
// body: c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>

<p>According to our administration the payment for invoice \\{{numberPrefix}}\\{{number}} is overdue.</p>
// <p>According to our administration the payment for invoice \\{{numberPrefix}}\\{{number}} is overdue.</p>

<p>We would like to ask you to pay the open amount of {{totalIncludingTax}} within 5 days on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>
// <p>We would like to ask you to pay the open amount of {{totalIncludingTax}} within 5 days on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>

<p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.
// <p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.

<p>If the payment is not received we will be required to take further legal steps.</p>
// <p>If the payment is not received we will be required to take further legal steps.</p>

<p>Kind regards,</p>
// <p>Kind regards,</p>

<p>{{companyDetails.name}}</p>`
}
]
// <p>{{companyDetails.name}}</p>`
// }
// ]

const numberPrefixes: Insertable<NumberPrefixes>[] = [
{
Expand Down Expand Up @@ -160,7 +154,7 @@ const clients: Insertable<Clients>[] = [
]

const seed = async () => {
await db.insertInto('emailTemplates').values(emailTemplates).execute()
// await db.insertInto('emailTemplates').values(emailTemplates).execute()
await db.insertInto('numberPrefixes').values(numberPrefixes).execute()

await db.insertInto('companies').values(companies).execute()
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export default async function (fastify: FastifyInstance) {
molliePaymentHandler = {
profiles
}
console.log(profiles)
}

let smartpinPaymentHandler: FastifyCheckoutPaymentHandler | undefined
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/templates/email/bill/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const billEmailTemplates = import.meta.glob<{
subject: string
body: string
}>('./**/*.ts')
16 changes: 16 additions & 0 deletions packages/api/src/templates/email/bill/send/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { c } from 'compress-tag'

const subject = `Outstanding bill`
const body = c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>In the attachment you can find the bill with an amount due of {{amountDue}}.</p>
<p>You can also view and pay the bill <a href="\\{{invoiceUrl}}">here</a>.
<p>If you have any questions about this bill, you can contact us.</p>
<p>Kind regards,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
17 changes: 17 additions & 0 deletions packages/api/src/templates/email/bill/send/nl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { c } from 'compress-tag'

const subject = `Openstaande rekening`
const body = c`
<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>Bijgevoegd treft u uw rekening aan met een openstaand bedrag ter hoogte van {{amountDue}}.</p>
<p>U kunt de rekening ook <a href="\\{{invoiceUrl}}">hier</a> bekijken en betalen.
<p>Mocht u nog vragen hebben over deze rekening, dan kunt u contact met ons opnemen.</p>
<p>Met vriendelijke groet,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
4 changes: 4 additions & 0 deletions packages/api/src/templates/email/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const emailTemplates = import.meta.glob<{
subject: string
body: string
}>('./**/*.ts')
18 changes: 18 additions & 0 deletions packages/api/src/templates/email/invoice/exhort/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { c } from 'compress-tag'

const subject = `Exhortation invoice \\{{numberPrefix}}\\{{number}}`
const body = c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>According to our administration the payment for invoice \\{{numberPrefix}}\\{{number}} is overdue.</p>
<p>We would like to ask you to pay the open amount of {{totalIncludingTax}} within 5 days on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>
<p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.
<p>If the payment is not received we will be required to take further legal steps.</p>
<p>Kind regards,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
17 changes: 17 additions & 0 deletions packages/api/src/templates/email/invoice/exhort/nl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { c } from 'compress-tag'
const subject = `Aanmaning factuur \\{{numberPrefix}}\\{{number}}`
const body = c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>Uit onze administratie blijkt dat factuur \\{{numberPrefix}}\\{{number}} nog niet is voldaan.</p>
<p>Wij willen u verzoeken het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} alsnog binnen 5 dagen te betalen op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>
<p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.
<p>Indien de betaling uitblijft zijn we genoodzaakt verdere juridische stappen te nemen.</p>
<p>Met vriendelijke groet,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
4 changes: 4 additions & 0 deletions packages/api/src/templates/email/invoice/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const invoiceEmailTemplates = import.meta.glob<{
subject: string
body: string
}>('./**/*.ts')
18 changes: 18 additions & 0 deletions packages/api/src/templates/email/invoice/remind/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { c } from 'compress-tag'

const subject = `Payment reminder invoice \\{{numberPrefix}}\\{{number}}`
const body = c`</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>According to our administration the payment for invoice \\{{numberPrefix}}\\{{number}} is overdue.</p>
<p>We would like to ask you to pay the open amount of {{totalIncludingTax}} within 7 days on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>
<p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.
<p>If you have any questions about this invoice, you can contact us.</p>
<p>Kind regards,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
19 changes: 19 additions & 0 deletions packages/api/src/templates/email/invoice/remind/nl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { c } from 'compress-tag'

const subject = `Betalingsherinnering factuur \\{{numberPrefix}}\\{{number}}`
const body = c`
<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>Uit onze administratie blijkt dat factuur \\{{numberPrefix}}\\{{number}} nog niet is voldaan.</p>
<p>Wij willen u verzoeken het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} alsnog binnen 7 dagen te betalen op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>
<p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.
<p>Mocht u nog vragen hebben over deze factuur, dan kunt u contact met ons opnemen.</p>
<p>Met vriendelijke groet,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
19 changes: 19 additions & 0 deletions packages/api/src/templates/email/invoice/send/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { c } from 'compress-tag'

const subject = `Invoice \\{{numberPrefix}}\\{{number}}`
const body = c`
</p>Dear {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>In the attachment you can find invoice \\{{numberPrefix}}\\{{number}} for our services.</p>
{{#unless paid}}
<p>We would like to ask you to pay the amount of {{totalIncludingTax}} within {{paymentTermDays}} days (before \\{{dueDate}}) on account number {{companyDetails.iban}} in the name of {{companyDetails.name}} with mention of the invoice number.</p>
{{/unless}}
<p>You can also view the invoice <a href="\\{{invoiceUrl}}">here</a>.
<p>If you have any questions about this invoice, you can contact us.</p>
<p>Kind regards,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
18 changes: 18 additions & 0 deletions packages/api/src/templates/email/invoice/send/nl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { c } from 'compress-tag'

const subject = `Factuur \\{{numberPrefix}}\\{{number}}`
const body = c`<p>Beste {{#if clientDetails.contactPersonName}}{{clientDetails.contactPersonName}}{{else}}{{clientDetails.companyName}}{{/if}},</p>
<p>Bijgevoegd treft u factuur \\{{numberPrefix}}\\{{number}} aan voor de geleverde diensten.</p>
{{#unless paid}}
<p>Gelieve het verschuldigde bedrag ter hoogte van {{totalIncludingTax}} te betalen binnen {{paymentTermDays}} dagen (voor \\{{dueDate}}) op rekeningnummer {{companyDetails.iban}} t.n.v. {{companyDetails.name}} onder vermelding van factuurnummer.</p>
{{/unless}}
<p>U kunt de factuur ook <a href="\\{{invoiceUrl}}">hier</a> bekijken.
<p>Mocht u nog vragen hebben over deze factuur, dan kunt u contact met ons opnemen.</p>
<p>Met vriendelijke groet,</p>
<p>{{companyDetails.name}}</p>`

export { subject, body }
4 changes: 4 additions & 0 deletions packages/api/src/templates/email/receipt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const receiptEmailTemplates = import.meta.glob<{
subject: string
body: string
}>('./**/*.ts')
Loading

0 comments on commit 9054bed

Please sign in to comment.