Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Added coupons #25

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions docs/Coupon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Contains all information about a given CsvOrder

# Fields

| Name | Type | Description |
| ---------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------- |
| `id` | `string` | The ID of the coupon. |
| `name` | `string` | The name of the coupon. |
| `companyId` | `string` | The ID of the company the coupon belongs to. |
| `stats` | `object` | An object containing the stats of the coupon. With keys 'total', 'used' and 'remaining' |

# Methods

## `.refresh()`

Refresh the Coupon data to get the latest information

**Returns: `Promise<void>`**

**Example**

```js
const coupon: Coupon;
await coupon.refresh();
```

---

## `Coupon.getCodes()`

Get all coupon codes within the coupon. |

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

**Example**

```js
const couponCodes = await coupon.getCodes();
```

---

## `Coupon.getCode(id)`

Get all coupon codes by its ID.

**Parameters**

| Name | Type | Description |
| ---- | -------- | --------------------------------- |
| `id` | `string` | The ID of the coupon code to get. |

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

**Example**

```js
const couponCode = await coupon.getCode('example-coupon-code-id');
```

---

## `Coupon.addCodes(csv)`

Add coupon codes to the coupon by uploading a CSV.

**Parameters**

| Name | Type | Description |
| ---------- | ------------- | --------------------------------- |
| `csv` | `ArrayBuffer` | The file to upload. Must be a CSV |

**Returns: `Promise<void>`

**Example**

```js
const data = fs.readFileSync("example.csv").buffer;
const file = await coupon.addCodes(data);
```


---

## `Coupon.delete()`

Delete the coupon.

**Returns: `Promise<void>`

**Example**

```js
await coupon.delete();
```

43 changes: 43 additions & 0 deletions docs/CouponCode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Contains all information about a given CsvOrder

# Fields

| Name | Type | Description |
| ---------------------- | ------------------------------------ | --------------------------------------------------------------------------------- |
| `id` | `string` | The ID of the coupon code. |
| `couponId` | `string` | The ID of the parent coupon. |
| `code` | `string` | The actual code saved for the coupon code`. |
| `used` | `boolean` | Whether the coupon code has been used. |
| `usedAt` | `Date` or `null` | The date at which the coupon code was used or `null` if not used yet. |
| `orderId` | `string` or `null` | The order ID by which the coupon code was used or `null` if not used yet. |

# Methods
---

## `.refresh()`

Refresh the CouponCode data to get the latest information

**Returns: `Promise<void>`**

**Example**

```js
const couponCode: CouponCOde;
await couponCode.refresh();
```

---

## `.getOrder()`

Get the order the coupon code was used by. You might need to do [`CouponCode.refresh`](#refresh) first when coupon was used after fetching.

**Returns: [`Promise<Order|null>`](./Order)**

**Example**

```js
const couponCode: CouponCode
const order = await couponCode.getOrder();;
```
74 changes: 74 additions & 0 deletions docs/PrintOne.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,77 @@ const csvOrder = await client.getCsvOrder("example-order-id");
```

```

--

## `.createCoupon(data)`

Create a new coupon.

**Parameters**

| Name | Type | Description |
| ------ | -------- | ------------------------------------------------------------------------------------------- |
| `data` | `object` | The data to create the coupon with. See [`Order`](./Coupon#createcoupondata) for more info. |

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

**Example**

```js
const coupon = await client.createCoupon({
name: "coupon",
});
```

---

## `.getCoupons([options])`

Get all coupons.

**Parameters**

| Name | Type | Default | Description |
| ------------------------------- | ----------------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `options.limit` | `number` | `10` | The maximum number of coupons to return. |
| `options.page` | `number` | `1` | The page of coupons to return. |
| `options.sortBy` | [`sort`](./Filtering#Sorting) | `createdAt:DESC` | The field(s) to sort the coupons by. Can be `createdAt` or `name` |
| `options.filter.name` | `string` \| `string[]` | `undefined` | The name(s) of the coupon(s) to filter |

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

**Example**

```js
const coupons = await client.getCoupons({
limit: 20,
page: 1,
sortBy: "createdAt:ASC",
filter: {
name: "test",
},
});
```

---

## `.getCoupon(id)`

Get a coupon by its ID.

**Parameters**

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

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

**Example**

```js
const coupon = await client.getCoupon("example-coupon-id");
```

---
57 changes: 57 additions & 0 deletions src/PrintOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { Batch, CreateBatch } from "~/models/Batch";
import { IBatch } from "~/models/_interfaces/IBatch";
import { BatchStatus } from "~/enums/BatchStatus";
import * as crypto from "crypto";
import { Coupon, CreateCoupon } from "~/models/Coupon";
import { ICoupon } from "~/models/_interfaces/ICoupon";

export type RequestHandler = new (
token: string,
Expand Down Expand Up @@ -523,6 +525,61 @@ export class PrintOne {
);
}

/**
* Create a coupon
* @param data The coupon data
*/
public async createCoupon(data: CreateCoupon): Promise<Coupon> {
const response = await this.client.POST<ICoupon>("coupons", {
name: data.name,
});

return new Coupon(this.protected, response);
}

/**
* Get all coupons.
* @param { PaginationOptions } options The options to use for pagination
* @param options.limit The maximum amount of coupons to return.
* @param options.page The page to return.
* @param options.sortBy The fields to sort by, can be "createdAt", "updatedAt".
* @param options.filter The filters to apply.
*/
public async getCoupons(
options: PaginationOptions<"name"> & {
filter?: {
name?: InFilter;
createdAt?: DateFilter;
};
} = {},
): Promise<PaginatedResponse<Coupon>> {
const params = {
...sortToQuery(options),
...inFilterToQuery("name", options.filter?.name),
};

const data = await this.client.GET<IPaginatedResponse<ICoupon>>("coupons", {
params: params,
});

return PaginatedResponse.safe(
this.protected,
data,
(data) => new Coupon(this.protected, data),
);
}

/**
* Get a coupon by its id.
* @param { string } id The id of the coupon.
* @throws { PrintOneError } If the coupon could not be found.
*/
public async getCoupon(id: string): Promise<Coupon> {
const data = await this.client.GET<ICoupon>(`coupons/${id}`);

return new Coupon(this.protected, data);
}

public validatedWebhook(
body: string,
headers: Record<string, string>,
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ export * from "./PrintOne";

// Models
export * from "./models/Address";
export * from "./models/Batch";
export * from "./models/Company";
export * from "./models/Coupon";
export * from "./models/CouponCode";
export * from "./models/CsvOrder";
export * from "./models/CustomFile";
export * from "./models/Order";
export * from "./models/PaginatedResponse";
export * from "./models/Preview";
export * from "./models/PreviewDetails";
export * from "./models/Template";
export * from "./models/Batch";

// Enums
export * from "./enums/CsvStatus";
Expand Down
Loading
Loading