Skip to content

Commit

Permalink
Merge pull request #11 from Print-one/feat/PO-1477-batch-csv
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRill00 authored Mar 11, 2024
2 parents e85e14c + 0c76223 commit 745014e
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 9 deletions.
59 changes: 59 additions & 0 deletions docs/Batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,62 @@ await batch.update({
ready: true,
});
```

---

## `.createCsvOrder(data)`

Create a new csv order.

**Parameters**

| Name | Type | Description |
| ------ | -------- | ------------------------------------------------------------------------------------------------- |
| `data` | `object` | The data to create the order with. See [`CsvOrder`](./CsvOrder#createcsvorderdata) for more info. |

**Returns: [`Promise<CsvOrder>`](./CsvOrder)**

**Example**

```js
const batch: Batch;

const order = await batch.createCsvOrder({
mapping: {
recipient: {
city: "{{City}}",
name: "{{FirstName}} {{LastName}}",
address: "{{Street}} {{HouseNr}}",
country: "{{Country}}",
postalCode: "{{ZIP}}",
},
mergeVariables: {
name: "{{FirstName}}",
coupon: "{{Coupon}}",
},
},
file: file,
});
```

---

## `.getCsvOrder(id)`

Get a csv order by its ID.

**Parameters**

| Name | Type | Description |
| ---- | -------- | ------------------------------- |
| `id` | `string` | The ID of the csv order to get. |

**Returns: [`Promise<CsvOrder>`](./CsvOrder)**

**Example**

```js
const batch: Batch;

const csvOrder = await batch.getCsvOrder("example-order-id");
```
39 changes: 39 additions & 0 deletions src/models/Batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { PaginatedResponse } from "./PaginatedResponse";
import { Order } from "./Order";
import { Address } from "./Address";
import { IOrder } from "./_interfaces/IOrder";
import { CreateBatchCsvOrder, CsvOrder } from "./CsvOrder";
import { ICsvOrder } from "./_interfaces/ICsvOrder";

export type CreateBatch = {
name: string;
Expand Down Expand Up @@ -144,6 +146,43 @@ export class Batch {
return new Order(this._protected, data);
}

public async createCsvOrder(data: CreateBatchCsvOrder): Promise<CsvOrder> {
const formData = new FormData();
formData.append(
"file",
new Blob([data.file], { type: "text/csv" }),
"upload.csv",
);
formData.append("mapping", JSON.stringify(data.mapping));

const response = await this._protected.client.POST<{ id: string }>(
`/batches/${this.id}/orders/csv`,
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
},
);

const id = response.id;
const csvInfo = await this._protected.client.GET<ICsvOrder>(
`batches/${this.id}/orders/csv/${id}`,
);

return new CsvOrder(this._protected, csvInfo);
}

/**
* Get a csv order by its id.
* @param { string } id The id of the csv order.
* @param basePath The basePath to use for this request
* @throws { PrintOneError } If the order could not be found.
*/
public async getCsvOrder(id: string): Promise<CsvOrder> {
return this._protected.printOne.getCsvOrder(id, `batches/${this.id}`);
}

/**
* Update the batch
*
Expand Down
2 changes: 2 additions & 0 deletions src/models/CsvOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type CreateCsvOrder = {
sender?: Address;
};

export type CreateBatchCsvOrder = Pick<CreateCsvOrder, "file" | "mapping">;

export class CsvOrder {
private _data: ICsvOrder;

Expand Down
116 changes: 112 additions & 4 deletions test/Batch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
Batch,
CreateBatchCsvOrder,
CsvOrder,
Finish,
Format,
Order,
Expand All @@ -8,6 +10,8 @@ import {
} from "../src";
import { client } from "./client";
import { BatchStatus } from "../src/enums/BatchStatus";
import * as fs from "fs";
import * as path from "path";

let batch: Batch = null as unknown as Batch;
let template: Template = null as unknown as Template;
Expand All @@ -29,10 +33,6 @@ beforeEach(async function () {
});
});

afterAll(async function () {
await template.delete();
});

async function addOrders(count: number): Promise<void> {
await Promise.all(
Array.from(Array(count)).map(() =>
Expand Down Expand Up @@ -85,6 +85,114 @@ describe("createOrder", function () {
}, 20000);
});

describe("createCsvOrder", function () {
let file: ArrayBuffer = null as unknown as ArrayBuffer;

const mapping: CreateBatchCsvOrder["mapping"] = {
recipient: {
name: "{{FirstName}} {{LastName}}",
addressLine2: "Financial Dpt.",
address: "{{Street}} {{HouseNr}}",
postalCode: "{{ZIP}}",
city: "{{City}}",
country: "{{Country}}",
},
};

beforeAll(() => {
file = fs.readFileSync(path.join(__dirname, "assets/test.csv"));
});

it("should create a csv order with all fields", async function () {
// arrange

// act
const csvOrder = await batch.createCsvOrder({
mapping: mapping,
file: file,
});

// assert
expect(csvOrder).toBeDefined();
expect(csvOrder).toEqual(expect.any(CsvOrder));

expect(csvOrder.id).toEqual(expect.any(String));
expect(csvOrder.status).toEqual(expect.any(String));
expect(csvOrder.createdAt).toEqual(expect.any(Date));
expect(csvOrder.updatedAt).toEqual(expect.any(Date));
// if sendDate is undefined, it should be today
expect(csvOrder.sendDate.getDay()).toEqual(new Date().getDay());
expect(csvOrder.friendlyStatus).toEqual(expect.any(String));
expect(csvOrder.sender).toEqual(undefined);
expect(csvOrder.recipientMapping).toEqual(mapping.recipient);
expect(csvOrder.templateId).toEqual(template.id);
expect(csvOrder.mergeVariableMapping).toEqual(mapping.mergeVariables);
expect(csvOrder.billingId).toEqual(
expect.toBeOneOf([undefined, expect.any(String)]),
);
expect(csvOrder.finish).toEqual(expect.any(String));
expect(csvOrder.format).toEqual(expect.any(String));
expect(csvOrder.isBillable).toEqual(expect.any(Boolean));
expect(csvOrder.estimatedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.failedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.processedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.totalOrderCount).toEqual(expect.any(Number));
});
});

describe("getCsvOrder", function () {
let csvOrderId: string = null as unknown as string;
const mapping: CreateBatchCsvOrder["mapping"] = {
recipient: {
name: "{{FirstName}} {{LastName}}",
address: "{{Street}} {{HouseNr}}",
postalCode: "{{ZIP}}",
city: "{{City}}",
country: "{{Country}}",
},
};

beforeAll(async () => {
const file = fs.readFileSync(path.join(__dirname, "assets/test.csv"));

const csvOrder = await batch.createCsvOrder({
mapping: mapping,
file: file,
});

csvOrderId = csvOrder.id;
});

it("should get a csv order with all fields", async function () {
// arrange

// act
const csvOrder = await client.getCsvOrder(csvOrderId);

// assert
expect(csvOrder).toBeDefined();
expect(csvOrder.id).toEqual(expect.any(String));
expect(csvOrder.status).toEqual(expect.any(String));
expect(csvOrder.createdAt).toEqual(expect.any(Date));
expect(csvOrder.updatedAt).toEqual(expect.any(Date));
// if sendDate is undefined, it should be today
expect(csvOrder.sendDate.getDay()).toEqual(new Date().getDay());
expect(csvOrder.friendlyStatus).toEqual(expect.any(String));
expect(csvOrder.sender).toEqual(undefined);
expect(csvOrder.recipientMapping).toEqual(mapping.recipient);
expect(csvOrder.templateId).toEqual(template.id);
expect(csvOrder.mergeVariableMapping).toEqual(mapping.mergeVariables);
expect(csvOrder.billingId).toBeOneOf([undefined, expect.any(String)]);
expect(csvOrder.finish).toEqual(expect.any(String));
expect(csvOrder.format).toEqual(expect.any(String));
expect(csvOrder.isBillable).toEqual(expect.any(Boolean));
expect(csvOrder.estimatedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.failedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.processedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.totalOrderCount).toEqual(expect.any(Number));
});
});

describe("update", function () {
describe("ready", function () {
it("should update the batch to ready", async function () {
Expand Down
10 changes: 5 additions & 5 deletions test/PrintOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,11 @@ describe("createCsvOrder", function () {
let file: ArrayBuffer = null as unknown as ArrayBuffer;
const mapping: CreateCsvOrder["mapping"] = {
recipient: {
city: "{{City}}",
name: "{{FirstName}} {{LastName}}",
address: "{{Street}} {{HouseNr}}",
country: "{{Country}}",
postalCode: "{{ZIP}}",
city: "{{City}}",
country: "{{Country}}",
},
};

Expand Down Expand Up @@ -721,7 +721,7 @@ describe("createCsvOrder", function () {
expect(csvOrder.recipientMapping).toEqual(mapping.recipient);
expect(csvOrder.templateId).toEqual(template.id);
expect(csvOrder.mergeVariableMapping).toEqual(mapping.mergeVariables);
expect(csvOrder.billingId).toEqual(undefined);
expect(csvOrder.billingId).toBeOneOf([undefined, expect.any(String)]);
expect(csvOrder.finish).toEqual(expect.any(String));
expect(csvOrder.format).toEqual(expect.any(String));
expect(csvOrder.isBillable).toEqual(expect.any(Boolean));
Expand Down Expand Up @@ -845,7 +845,7 @@ describe("getCsvOrder", function () {
expect(csvOrder.recipientMapping).toEqual(mapping.recipient);
expect(csvOrder.templateId).toEqual(template.id);
expect(csvOrder.mergeVariableMapping).toEqual(mapping.mergeVariables);
expect(csvOrder.billingId).toEqual(undefined);
expect(csvOrder.billingId).toBeOneOf([undefined, expect.any(String)]);
expect(csvOrder.finish).toEqual(expect.any(String));
expect(csvOrder.format).toEqual(expect.any(String));
expect(csvOrder.isBillable).toEqual(expect.any(Boolean));
Expand All @@ -854,7 +854,7 @@ describe("getCsvOrder", function () {
expect(csvOrder.processedOrderCount).toEqual(expect.any(Number));
expect(csvOrder.totalOrderCount).toEqual(expect.any(Number));
});
})
});

describe("getOrder", function () {
let orderId: string = null as unknown as string;
Expand Down

0 comments on commit 745014e

Please sign in to comment.