Skip to content

Commit 9e04141

Browse files
feat: ✨ added CsvOrder model + endpoints (#7)
* feat: ✨ added CsvOrder model + endpoints * docs: 📝 updated docs about CsvOrder * fix: PR feedback * chore: type error --------- Co-authored-by: Koen van Staveren <[email protected]>
1 parent b822eaf commit 9e04141

File tree

10 files changed

+652
-17
lines changed

10 files changed

+652
-17
lines changed

docs/CsvOrder.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Contains all information about a given CsvOrder
2+
3+
# Fields
4+
5+
| Name | Type | Description |
6+
| --------------------- | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7+
| `id` | `string` | The ID of the order. |
8+
| `templateId` | `string` | The ID of the template the order is based on. |
9+
| `finish` | `string` | The finish of the order. Can be `GLOSSY` or `MATT`. |
10+
| `format` | `string` | The format of the order. Can be `POSTCARD_A5`, `POSTCARD_A6`, `POSTCARD_SQ14` or `GREETINGCARD_SQ15`. |
11+
| `mergeVariableMapping` | `object` | The merge variables of the order. Keys are the merge variable names. |
12+
| `sender` | [`Address`](#Address) \| `undefined` | The sender of the order. |
13+
| `recipientMapping` | [`Address`](#Address) | The mapping of the recipient. |
14+
| `definitiveCountryId` | `string` | The ID of the definitive country of the order. |
15+
| `billingId` | `string` \| `undefined` | The ID assigned to the csv order by the customer. |
16+
| `isBillable` | `boolean` | Whether the csv order is billable. True when an live API key is used, false when a test API key is used. |
17+
| `status` | `string` | The status of the csv order. Can be `order_created` or `order_processed` |
18+
| `friendlyStatus` | `string` | The friendly status of the csv order. Can be `Processing` or `Success` |
19+
| `estimatedOrderCount` | `number` | The amount of initial orders in the CSV
20+
| `failedOrderCount` | `number` | The amount of orders which have failed to be processed
21+
| `processedOrderCount` | `number` | The amount of orders which have successfully been processed
22+
| `totalOrderCount` | `number` | The total amount of orders failed or processed
23+
| `sendDate` | `Date` | The date the order will be sent on. |
24+
| `createdAt` | `Date` | The date and time the order was created. |
25+
| `updatedAt` | `Date` | The date and time the order was last updated. |
26+
27+
# Methods
28+
29+
## `.getTemplate()`
30+
31+
Get the template the order is based on. Uses [`PrintOne.getTemplate()`](./PrintOne#gettemplateid).
32+
33+
**Returns: [`Promise<Template>`](./Template)**
34+
35+
**Example**
36+
37+
```js
38+
const order = await client.getOrder("example-order-id");
39+
const template = await order.getTemplate();
40+
```
41+
42+
---
43+
44+
## `.refresh()`
45+
46+
Refresh the CsvOrder data to get the latest information
47+
48+
**Returns: `Promise<void>`**
49+
50+
**Example**
51+
52+
```js
53+
const csvOrder: CsvOrder;
54+
await csvOrder.refresh();
55+
```
56+
57+
---
58+
59+
## [`CsvOrder.getOrders([options])`](./PrintOne#getordersoptions)
60+
61+
Get all orders generated by the CSV.
62+
63+
**Parameters**
64+
65+
| Name | Type | Default | Description |
66+
| ------------------------------- | ----------------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
67+
| `options.limit` | `number` | `10` | The maximum number of orders to return. |
68+
| `options.page` | `number` | `1` | The page of orders to return. |
69+
| `options.sortBy` | [`sort`](./Filtering#Sorting) | `createdAt:DESC` | The field(s) to sort the orders by. Can be `createdAt`, `anonymizedAt`, `updatedAt`, `friendlyStatus` or `sendDate` |
70+
| `options.filter.friendlyStatus` | `string` \| `string[]` | `undefined` | The friendly status(es) of the order(s) to filter by. Can be `Processing`, `Success`, `Sent`, `Scheduled`, `Cancelled` or `Failed` |
71+
| `options.filter.billingId` | `string` \| `string[]` | `undefined` | The billing ID(s) of the order(s) to filter by. |
72+
| `options.filter.format` | `string` \| `string[]` | `undefined` | The format(s) of the order(s) to filter by. Can be `POSTCARD_A5`, `POSTCARD_A6`, `POSTCARD_SQ14` or `GREETINGCARD_SQ15` |
73+
| `options.filter.finish` | `string` \| `string[]` | `undefined` | The finish(es) of the order(s) to filter by. Can be `GLOSSY` or `MATTE` |
74+
| `options.filter.isBillable` | `boolean` | `undefined` | Whether the order(s) are live order or test orders. |
75+
| `options.filter.createdAt` | [`date`](./Filtering#Date) | `undefined` | The date(s) the order(s) were created on. |
76+
77+
**Returns: [`Promise<PaginatedResponse<Order>>`](./Order)**
78+
79+
**Example**
80+
81+
```js
82+
const orders = await csvOrder.getOrders({
83+
limit: 20,
84+
page: 1,
85+
sortBy: "createdAt:ASC",
86+
filter: {
87+
friendlyStatus: "Success",
88+
billingId: "example-billing-id",
89+
format: Format.POSTCARD_A5,
90+
finish: Finish.GLOSSY,
91+
isBillable: true,
92+
createdAt: {
93+
from: "2020-01-01",
94+
to: "2020-01-31",
95+
},
96+
},
97+
});
98+
```

docs/PrintOne.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,39 @@ const orders = await client.getOrders({
243243
},
244244
});
245245
```
246+
247+
---
248+
249+
## `.createCsvOrder(data)`
250+
251+
Create a new csv order.
252+
253+
**Parameters**
254+
255+
| Name | Type | Description |
256+
| ------ | -------- | ---------------------------------------------------------------------------------------- |
257+
| `data` | `object` | The data to create the order with. See [`CsvOrder`](./CsvOrder#createcsvorderdata) for more info. |
258+
259+
**Returns: [`Promise<CsvOrder>`](./CsvOrder)**
260+
261+
**Example**
262+
263+
```js
264+
const order = await client.createCsvOrder({
265+
mapping: {
266+
recipient: {
267+
city: "{{City}}",
268+
name: "{{FirstName}} {{LastName}}",
269+
address: "{{Street}} {{HouseNr}}",
270+
country: "{{Country}}",
271+
postalCode: "{{ZIP}}",
272+
},
273+
mergeVariables: {
274+
name: "{{FirstName}}",
275+
coupon: "{{Coupon}}",
276+
},
277+
},
278+
template: template,
279+
file: file,
280+
});
281+
```

src/PrintOne.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ import { IOrder } from "./models/_interfaces/IOrder";
2424
import { FriendlyStatus } from "./enums/Status";
2525
import { Format } from "./enums/Format";
2626
import { AxiosHTTPHandler } from "./AxiosHttpHandler";
27+
import { CreateCsvOrder, CsvOrder } from "./models/CsvOrder";
28+
import { ICsvOrder } from "./models/_interfaces/ICsvOrder";
2729

2830
export type RequestHandler = new (
2931
token: string,
3032
options: Required<PrintOneOptions>,
3133
debug: debug.Debugger,
32-
) => HttpHandler<unknown, unknown>;
33-
34+
) => HttpHandler<{ headers: Record<string, string> }, unknown>;
3435
export type PrintOneOptions = Partial<{
3536
url: string;
3637
version: "v2";
@@ -52,6 +53,21 @@ export type Protected = {
5253
printOne: PrintOne;
5354
};
5455

56+
export type OrderPaginatedQuery = PaginationOptions<
57+
"createdAt" | "anonymizedAt" | "updatedAt" | "friendlyStatus" | "sendDate"
58+
> & {
59+
filter?: {
60+
friendlyStatus?: InFilter<FriendlyStatus>;
61+
billingId?: InFilter;
62+
format?: InFilter<Format>;
63+
// finish?: InFilter<Finish>;
64+
isBillable?: boolean;
65+
createdAt?: DateFilter;
66+
anonymizedAt?: DateFilter | boolean;
67+
csvId?: InFilter;
68+
};
69+
};
70+
5571
export class PrintOne {
5672
private readonly _protected: Partial<Protected> = {
5773
debug: debug("print-one"),
@@ -249,6 +265,43 @@ export class PrintOne {
249265
return new Order(this.protected, response);
250266
}
251267

268+
public async createCsvOrder(data: CreateCsvOrder): Promise<CsvOrder> {
269+
const templateId =
270+
typeof data.template === "string" ? data.template : data.template.id;
271+
272+
const formData = new FormData();
273+
formData.append(
274+
"file",
275+
new Blob([data.file], { type: "text/csv" }),
276+
"upload.csv",
277+
);
278+
formData.append("mapping", JSON.stringify(data.mapping));
279+
formData.append(
280+
"orderData",
281+
JSON.stringify({
282+
sender: data.sender,
283+
templateId,
284+
finish: data.finish,
285+
billingId: data.billingId,
286+
}),
287+
);
288+
289+
const response = await this.client.POST<{ id: string }>(
290+
"orders/csv",
291+
formData,
292+
{
293+
headers: {
294+
"Content-Type": "multipart/form-data",
295+
},
296+
},
297+
);
298+
299+
const id = response.id;
300+
const csvInfo = await this.client.GET<ICsvOrder>(`orders/csv/${id}`);
301+
302+
return new CsvOrder(this.protected, csvInfo);
303+
}
304+
252305
/**
253306
* Get an order by its id.
254307
* @param { string } id The id of the order.
@@ -270,19 +323,7 @@ export class PrintOne {
270323
* @throws { PrintOneError } If the order could not be found.
271324
*/
272325
public async getOrders(
273-
options: PaginationOptions<
274-
"createdAt" | "anonymizedAt" | "updatedAt" | "friendlyStatus" | "sendDate"
275-
> & {
276-
filter?: {
277-
friendlyStatus?: InFilter<FriendlyStatus>;
278-
billingId?: InFilter;
279-
format?: InFilter<Format>;
280-
// finish?: InFilter<Finish>;
281-
isBillable?: boolean;
282-
createdAt?: DateFilter;
283-
anonymizedAt?: DateFilter | boolean;
284-
};
285-
} = {},
326+
options: OrderPaginatedQuery = {},
286327
): Promise<PaginatedResponse<Order>> {
287328
let params = {
288329
...sortToQuery(options),
@@ -291,6 +332,7 @@ export class PrintOne {
291332
...inFilterToQuery("format", options.filter?.format),
292333
// ...inFilterToQuery("finish", options.filter?.finish),
293334
...dateFilterToQuery("createdAt", options.filter?.createdAt),
335+
...inFilterToQuery("csvOrderId", options.filter?.csvId),
294336
};
295337

296338
if (typeof options.filter?.isBillable === "boolean") {

src/enums/CsvStatus.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export const CsvStatus: {
2+
order_created: "order_created";
3+
order_processed: "order_processed";
4+
} = {
5+
order_created: "order_created",
6+
order_processed: "order_processed",
7+
};
8+
9+
export type CsvStatus = (typeof CsvStatus)[keyof typeof CsvStatus];
10+
11+
export const FriendlyCsvStatusText: {
12+
Processing: "Processing";
13+
Success: "Success";
14+
} = {
15+
Processing: "Processing",
16+
Success: "Success",
17+
};
18+
19+
export type FriendlyCsvStatusText =
20+
(typeof FriendlyCsvStatusText)[keyof typeof FriendlyCsvStatusText];
21+
22+
export const FriendlyCsvStatus: {
23+
[key in CsvStatus]: FriendlyCsvStatusText;
24+
} = {
25+
order_created: "Processing",
26+
order_processed: "Success",
27+
};
28+
29+
export type FriendlyCsvStatus = typeof FriendlyCsvStatus;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from "./PrintOne";
77
// Models
88
export * from "./models/Address";
99
export * from "./models/Company";
10+
export * from "./models/CsvOrder";
1011
export * from "./models/CustomFile";
1112
export * from "./models/Order";
1213
export * from "./models/PaginatedResponse";
@@ -15,6 +16,7 @@ export * from "./models/PreviewDetails";
1516
export * from "./models/Template";
1617

1718
// Enums
19+
export * from "./enums/CsvStatus";
1820
export * from "./enums/Finish";
1921
export * from "./enums/Format";
2022
export * from "./enums/Status";

0 commit comments

Comments
 (0)