Skip to content

Commit

Permalink
feat: ✨ allow for getting CSV order by id
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRill00 committed Mar 7, 2024
1 parent a506f3f commit 0560944
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
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");
```
10 changes: 10 additions & 0 deletions src/models/Batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ export class Batch {
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
53 changes: 53 additions & 0 deletions test/Batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,59 @@ describe("createCsvOrder", function () {
});
});

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

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).toEqual(undefined);
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
2 changes: 1 addition & 1 deletion test/PrintOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 0560944

Please sign in to comment.