Skip to content

Commit

Permalink
Merge pull request #243 from Pinelab-studio/feat/invoices-longtext
Browse files Browse the repository at this point in the history
Feat(invoices): Increase length of invoice template field in DB
  • Loading branch information
martijnvdbrug authored Aug 15, 2023
2 parents 1092206 + 5e532ed commit a04952d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/vendure-plugin-invoices/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1.1.0 (2023-08-14)

- Allow specifying invoice template storage in DB by specifying DB engine: INVOICES_PLUGIN_DB_ENGINE=mysql ([#243](https://github.com/Pinelab-studio/pinelab-vendure-plugins/pull/243))
18 changes: 11 additions & 7 deletions packages/vendure-plugin-invoices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ plugins: [
7. Check the checkbox to `Enable invoice generation` for the current channel on order placement.
8. A default HTML template is set for you. Click the `Preview` button to view a sample PDF invoice.

If you are using Docker with node:16 or higher as base, you need to add this to your Dockerfile:

```shell
# PhantomJS fix https://github.com/bazelbuild/rules_closure/issues/351
ENV OPENSSL_CONF=/dev/null
```

## Adding invoices to your order-confirmation email

Add the following link to your email template:
Expand All @@ -59,6 +52,17 @@ Add the following link to your email template:
When the customer clicks the link, the server will check if the `ordercode`, `channelCode` and `customer emailaddress`
match with the requested order. If so, it will return the invoice.

## Increase invoice template DB storage

By default, the plugin uses TypeOrm's `text` to store the invoice template in the DB. This might not be enough, for example when you'd like to add base64 encoded images to your invoices. This will result in the error `ER_DATA_TOO_LONG: Data too long for column 'templateString'`. You can specify your DB engine with an env variable, and the plugin will resolve the correct column type:

```shell
# E.g. For mysql the column type 'longtext' will be used, which supports up to 4gb
INVOICES_PLUGIN_DB_ENGINE=longtext
```

Don't forget to run a DB migration after! Checkout https://orkhan.gitbook.io/typeorm/docs/entities for available databases and column types.

## Google Storage strategy

This plugin also includes a strategy for storing invoices in Google Storage:
Expand Down
5 changes: 3 additions & 2 deletions packages/vendure-plugin-invoices/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-plugin-invoices",
"version": "1.0.1",
"version": "1.1.0",
"description": "Vendure plugin for invoice generation",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand All @@ -14,7 +14,8 @@
"types": "dist/index.d.ts",
"files": [
"dist",
"README.md"
"README.md",
"CHANGELOG.md"
],
"scripts": {
"start": "yarn ts-node test/dev-server.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Column, Entity } from 'typeorm';
import { DeepPartial, VendureEntity } from '@vendure/core';
import { Column, Entity, ColumnType, getMetadataArgsStorage } from 'typeorm';
import { DeepPartial, Logger, VendureEntity } from '@vendure/core';
import { InvoicePlugin } from '../../invoice.plugin';
import { loggerCtx } from '../../constants';

@Entity('invoice_config')
export class InvoiceConfigEntity extends VendureEntity {
Expand All @@ -9,8 +11,38 @@ export class InvoiceConfigEntity extends VendureEntity {

@Column()
channelId!: string;

@Column({ default: false })
enabled: boolean = false;
@Column({ type: 'text', nullable: true })

@Column({ type: resolveTemplateColumnType(), nullable: true })
templateString?: string | null;
}

/**
* Resolve column type based on the DB engine
*/
function resolveTemplateColumnType(): ColumnType {
const dbEngine = process.env.INVOICES_PLUGIN_DB_ENGINE;
if (!dbEngine) {
return 'text';
} else if (dbEngine === 'mysql' || 'mariadb') {
return 'longtext'; // up to 4GB
} else if (dbEngine === 'postgres') {
return 'text'; // Up to 1GB
} else if (dbEngine === 'cockroachdb') {
return 'string';
} else if (dbEngine === 'mssql') {
return 'text';
} else if (dbEngine === 'sqlite') {
return 'text';
} else if (dbEngine === 'oracle') {
return 'clob';
} else {
Logger.warn(
`No large-text column type available for DB engine "${dbEngine}", using "text". ( Contributions welcome )`,
loggerCtx
);
}
return 'text';
}
6 changes: 4 additions & 2 deletions packages/vendure-plugin-invoices/src/invoice.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export interface InvoicePluginConfig {
resolvers: [InvoiceResolver],
},
compatibility: '^2.0.0',
configuration: (config: RuntimeVendureConfig) =>
InvoicePlugin.configure(config),
configuration: (config: RuntimeVendureConfig) => {
InvoicePlugin.configure(config);
return config;
},
})
export class InvoicePlugin {
static config: InvoicePluginConfig;
Expand Down

0 comments on commit a04952d

Please sign in to comment.