Skip to content

Commit

Permalink
Merge pull request #85 from City-of-Helsinki/feature/KYV-326-email-te…
Browse files Browse the repository at this point in the history
…mplate

Feature/kyv 326 email template
  • Loading branch information
eerohakio authored Oct 27, 2021
2 parents b614532 + 8e02dec commit 11ead86
Show file tree
Hide file tree
Showing 10 changed files with 671 additions and 91 deletions.
65 changes: 59 additions & 6 deletions packages/message-backend/src/email/create/HandleBar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parseOrderMetas } from '../send/service'
import { HandleBarTemplate } from './service'
import type { Order, OrderConfirmationEmailParameters } from './types'

Expand All @@ -10,7 +11,7 @@ describe('Create templates from parameters', () => {
items: [
{
productId: 'productId2',
productName: 'Kiinteähintainen tuote',
productName: 'Pysäköintivyöhyke K',
quantity: 1,
unit: '1',
rowPriceNet: '600',
Expand All @@ -20,12 +21,42 @@ describe('Create templates from parameters', () => {
priceNet: '300',
priceVat: '72',
priceGross: '372',
orderId: 'orderId',
orderItemId: 'orderItemId',
orderId: 'orderId1',
orderItemId: 'orderItemId1',
meta: [
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0',
value: 'when label is empty shows only value row',
visibleInCheckout: 'true',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId2',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 1',
value: 'meta value ordinal 1',
label: 'meta label ordinal 1',
visibleInCheckout: 'true',
ordinal: '1',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
label: 'Ajoneuvo',
key: 'XZY-123',
value: 'Scoda Octavia',
visibleInCheckout: 'true',
ordinal: '0',
},
],
},
{
productId: 'productId1',
productName: 'Kiinteähintainen tuote',
productName: 'Kiinteähintainen tuote 1',
quantity: 1,
unit: '1',
rowPriceNet: '600',
Expand All @@ -35,8 +66,28 @@ describe('Create templates from parameters', () => {
priceNet: '300',
priceVat: '72',
priceGross: '372',
orderId: 'orderId',
orderItemId: 'orderItemId',
orderId: 'orderId2',
orderItemId: 'orderItemId2',
meta: [
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0 show all',
value: 'meta value ordinal 0 show all',
label: '',
visibleInCheckout: 'true',
ordinal: '0',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0',
value: 'when label is empty shows only value row',
visibleInCheckout: 'true',
},
],
},
],
merchant: {
Expand Down Expand Up @@ -81,6 +132,8 @@ describe('Create templates from parameters', () => {
},
} as Order

parseOrderMetas(order)

const templateParams: OrderConfirmationEmailParameters = {
order: order,
}
Expand Down

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/message-backend/src/email/create/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export function HandleBarTemplate<T>(language: SUPPORTED_LANGUAGES) {
return str.toUpperCase()
})

Handlebars.registerHelper('eq', (a, b) => a == b)

Handlebars.registerHelper('Time', function (date) {
const dateObj = new Date(date)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
-premailer-width: 100%;
-premailer-cellpadding: 0;
-premailer-cellspacing: 0;
-premailer-cellspacing: 0;
border: 2px dashed #CBCCCF;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,70 @@
{{#each order.items}}
<tr>
<td width="80%" class="purchase_item">
<span class="f-fallback" style="font-size:14px; font-weight:bold;">
{{productName}}
</span>
<span class="f-fallback" style="font-weight:bolder;">
{{productName}}
</span>
</td>
<td class="align-right" width="20%" class="purchase_item">
<span class="f-fallback no-wrap">
{{rowPriceTotal}}
</span>
<span class="f-fallback no-wrap">
{{rowPriceTotal}}
</span>
</td>
</tr>

<tr>
<td width="80%" class="purchase_item">
<span class="f-fallback">
{{I18n 'orderConfirmation.receipt.alv'}} ({{vatPercentage}}%)
</span>
<span class="f-fallback">
{{I18n 'orderConfirmation.receipt.alv'}} ({{vatPercentage}}%)
</span>
</td>
<td class="align-right" width="20%" class="purchase_item pb-1">
<span class="f-fallback no-wrap">
{{priceVat}}
</span>
<span class="f-fallback no-wrap">
{{priceVat}}
</span>
</td>
</tr>
{{#each meta}}

{{#if label}}
<tr>
<td width="100%" class="purchase_item">
<span class="f-fallback no-wrap pb-1" style="font-size:14px; font-weight:bold;">
{{label}}
</span>
</td>
</tr>
{{/if}}

{{#if key}}
<tr>
<td width="100%" class="purchase_item">
<span class="f-fallback no-wrap pb-1" style="font-size:14px;">
{{key}}
</span>
</td>
</tr>
{{/if}}

{{#if value}}
<tr>
<td width="100%" class="purchase_item">
<span class="f-fallback no-wrap pb-1" style="font-size:14px;">
{{value}}
</span>
</td>
</tr>
{{/if}}
{{#if @last}}
<tr>
<td>
<span>&emsp;</span>
</td>
</tr>
{{/if}}

{{/each}}

{{/each}}
<tr>
<td width="80%" class="purchase_item" style="border-top: 1rem solid #ffffff">
Expand Down
13 changes: 13 additions & 0 deletions packages/message-backend/src/email/create/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ export type OrderType = 'subscription' | 'order'
export type OrderItem = OrderItemRequest & {
orderItemId: string
orderId: string
} & {
meta?: OrderItemMeta[]
}

export type OrderItemMeta = {
orderItemMetaId: string
orderItemId: string
orderId: string
key: string
value: string
label?: string
visibleInCheckout?: string
ordinal?: string
}

export interface Order {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ describe('sendEmailToCustomer.spec.ts', () => {
address: 'Esimerkkiosoite 1',
district: '123456 Esimerkkitoimipaikka',
},
meta: [
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'b513887b-db37-4249-a042-bf1425c340ac',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 1',
value: 'meta value ordinal 1',
label: 'meta label ordinal 1',
visibleInCheckout: 'true',
ordinal: '1',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'b513887b-db37-4249-a042-bf1425c340ac',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0',
value: 'meta value ordinal 0',
label: 'meta label ordinal 0',
visibleInCheckout: 'true',
ordinal: '0',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c112',
orderItemId: 'b513887b-db37-4249-a042-bf1425c340ac',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0',
value: 'meta value ordinal 0', // This should not show on email
label: '',
visibleInCheckout: 'true',
ordinal: '2',
},
],
status: 'draft',
type: 'order',
payment: {
Expand Down
100 changes: 100 additions & 0 deletions packages/message-backend/src/email/send/service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { parseOrderItemMetaVisibilityAndOrdinal } from './service'
import type { OrderItemMeta } from '../create/types'

describe('Test service.ts functions', () => {
it('Should parse order metas to correct order and show only certain fields', async () => {
const order = {
meta: [
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0 show all',
value: 'meta value ordinal 0 show all',
label: 'meta label ordinal 0 show all',
visibleInCheckout: 'true',
ordinal: '0',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key no ordinal',
value: 'when label is empty shows only value row',
visibleInCheckout: 'true',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId2',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 1',
value: 'meta value ordinal 1',
label: 'meta label ordinal 1',
visibleInCheckout: 'true',
ordinal: '1',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId3',
orderId: 'no-ordinal-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key no ordinal',
value: 'meta value no ordinal',
label: 'meta label no ordinal',
},
{
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId3',
orderId: 'no-ordinal-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key should not show',
value: 'meta value should not show',
label: 'meta label should not show',
visibleInCheckout: 'false',
},
] as OrderItemMeta[],
}

order.meta = parseOrderItemMetaVisibilityAndOrdinal(order.meta || [])

expect(order.meta[0]).toEqual({
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 0 show all',
value: 'meta value ordinal 0 show all',
label: 'meta label ordinal 0 show all',
visibleInCheckout: 'true',
ordinal: '0',
})

expect(order.meta[1]).toEqual({
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId2',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key ordinal 1',
value: 'meta value ordinal 1',
label: 'meta label ordinal 1',
visibleInCheckout: 'true',
ordinal: '1',
})

expect(order.meta[2]).toEqual({
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId1',
orderId: '76a9121f-3bb7-33b2-8ca8-bc6a23db24c1',
key: '', // This should be empty when label is not given.
value: 'when label is empty shows only value row',
visibleInCheckout: 'true',
})

expect(order.meta[3]).toEqual({
orderItemMetaId: 'ec627fb7-d557-4b7b-9c1c-61434322c109',
orderItemId: 'orderItemId3',
orderId: 'no-ordinal-3bb7-33b2-8ca8-bc6a23db24c1',
key: 'meta key no ordinal',
value: 'meta value no ordinal',
label: 'meta label no ordinal',
})
// Last meta should be ignored because visibleInCheckout: 'false'
expect(order.meta[4]).toBeUndefined()
})
})
Loading

0 comments on commit 11ead86

Please sign in to comment.