Skip to content

Commit

Permalink
feat: product - update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahdeyyy committed Apr 10, 2024
1 parent 1ad7252 commit fd77607
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ const archived_request = await paystack.payment_request.archive("request id")
- [x] Deactivate Authorization
- [x] Tests

- [ ] Add support for Products
- [x] Add support for Products
- [x] Create Product
- [x] List Products
- [ ] Fetch Product
- [ ] Update Product
- [ ] Tests
- [x] Fetch Product
- [x] Update Product
- [x] Tests

## Testing

Expand Down
12 changes: 12 additions & 0 deletions lib/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ test("fetch a product", async () => {
}
}
})

test("update a product", async () => {
const pro = await product()
expect(pro.status).toBe(true)
if (pro.status) {
const response = await paystack().product.update(pro.data.id, { price: 69420 })
expect(response.status).toBe(true)
if (response.status) {
expect(response.data.price).toBe(69420)
}
}
})
14 changes: 12 additions & 2 deletions lib/product.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CreateProductData, CreateProductResponse, FetchProductResponse, ListProductQuery, ListProductResponse } from "./types";
import type { CreateProductData, CreateProductResponse, FetchProductResponse, ListProductQuery, ListProductResponse, UpdateProductData, UpdateProductResponse } from "./types";

export class Product {
private secret_key: string;
Expand Down Expand Up @@ -56,5 +56,15 @@ export class Product {
const response_data = await response.json() as FetchProductResponse;
return response_data
}
async update() { }
async update(id: number, data: UpdateProductData): Promise<UpdateProductResponse> {
const headers = this.get_headers()
const url = `${this.endpoint}/${id}`
const response = await fetch(url, {
method: "PUT",
headers: headers,
body: JSON.stringify(data)
})
const response_data = await response.json() as UpdateProductResponse
return response_data
}
}
27 changes: 27 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,30 @@ export type FetchProductResponse = { message: string; } & ({
updatedAt: string;
}
} | PaystackResponseError)
export type UpdateProductData = {
/** Name of product */
name?: string;

/** A description for this product */
description?: string;

/** Price should be in the subunit of the supported currency */
price?: number;

/**
* Currency in which price is set
*/
currency?: Currency;

/**
* Set to true if the product has unlimited stock. Leave as false if the product has limited stock
*/

unlimited?: boolean;

/**
* Number of products in stock.Use if unlimited is false
*/
quantity?: number;
}
export type UpdateProductResponse = CreateProductResponse;

0 comments on commit fd77607

Please sign in to comment.