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: ✨ support batch order's API urls #13

Merged
merged 4 commits into from
Mar 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
18 changes: 16 additions & 2 deletions src/models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export class Order {
: undefined;
}

public get csvOrderId(): string | null {
return this._data.csvOrderId;
}

public get batchId(): string | undefined {
return this._data.batchId;
}

/**
* Get the template of the order
* @throws { PrintOneError } If the template could not be fetched.
Expand All @@ -125,7 +133,9 @@ export class Order {
* @throws { PrintOneError } If the order could not be refreshed.
*/
public async refresh(): Promise<void> {
this._data = await this._protected.client.GET<IOrder>(`orders/${this.id}`);
this._data = await this._protected.client.GET<IOrder>(
`${this.urlPrefix}orders/${this.id}`,
);
}

/**
Expand Down Expand Up @@ -169,8 +179,12 @@ export class Order {
}

this._data = await this._protected.client.POST<IOrder>(
`orders/${this.id}/cancel`,
`${this.urlPrefix}orders/${this.id}/cancel`,
{},
);
}

protected get urlPrefix(): string {
return this.batchId ? `batches/${this.batchId}/` : "";;
}
}
2 changes: 2 additions & 0 deletions src/models/_interfaces/IOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export type IOrder = {
createdAt: string;
updatedAt: string;
anonymizedAt: string | null;
csvOrderId: string | null;
batchId?: string;
};
48 changes: 47 additions & 1 deletion test/Batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { BatchStatus } from "../src/enums/BatchStatus";
import * as fs from "fs";
import * as path from "path";
import { sleep } from "../src/utils";

let batch: Batch = null as unknown as Batch;
let template: Template = null as unknown as Template;
Expand Down Expand Up @@ -124,6 +125,7 @@
expect(csvOrder.sendDate.getDay()).toEqual(new Date().getDay());
expect(csvOrder.friendlyStatus).toEqual(expect.any(String));
expect(csvOrder.sender).toEqual(undefined);
expect(csvOrder.format).toEqual(expect.any(String));
expect(csvOrder.recipientMapping).toEqual(mapping.recipient);
expect(csvOrder.templateId).toEqual(template.id);
expect(csvOrder.mergeVariableMapping).toStrictEqual({});
Expand Down Expand Up @@ -167,7 +169,7 @@
// arrange

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

// assert
expect(csvOrder).toBeDefined();
Expand Down Expand Up @@ -316,6 +318,7 @@
expect(result).toBeDefined();
expect(result).toBeInstanceOf(Order);
expect(result.id).toEqual(order.id);
expect(result.batchId).toEqual(batch.id);
});

it("should not return an non batch order", async function () {
Expand Down Expand Up @@ -402,3 +405,46 @@
expect(result.data).toBeArrayOfSize(1);
});
});

describe('BatchOrder', function () {
it('should be able to cancel Order', async function() {

Check failure on line 410 in test/Batch.spec.ts

View workflow job for this annotation

GitHub Actions / Test

BatchOrder › should be able to cancel Order

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at test/Batch.spec.ts:410:3 at Object.<anonymous> (test/Batch.spec.ts:409:1)
// arrange
const order = await batch.createOrder({
recipient: {
name: "John Doe",
address: "123 Main Street",
postalCode: "1234 AB",
city: "Anytown",
country: "Nederland",
},
});

// act
await order.cancel();

// assert
expect(order.status).toEqual('order_cancelled');
})

it('should be able to refresh Order', async function () {
// arrange
const order = await batch.createOrder({
recipient: {
name: "John Doe",
address: "123 Main Street",
postalCode: "1234 AB",
city: "Anytown",
country: "Nederland",
},
});

// act
while (order.status === "order_created") {
await order.refresh();
await sleep(1000);
}

// assert
expect(order.status).not.toEqual("order_created");
}, 30000);
})
7 changes: 7 additions & 0 deletions test/PrintOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,12 @@ describe("getOrder", function () {
expect(order.isBillable).toEqual(expect.any(Boolean));
expect(order.errors).toEqual(expect.any(Array));
expect(order.definitiveCountryId).toEqual(expect.any(String));
expect(order.csvOrderId).toEqual(
expect.toBeOneOf([expect.any(String), null]),
);
expect(order.batchId).toEqual(
expect.toBeOneOf([expect.any(String), undefined]),
);
});

it("should throw an error when the order does not exist", async function () {
Expand Down Expand Up @@ -1393,6 +1399,7 @@ describe("createBatch", function () {
expect(batch.createdAt).toEqual(expect.any(Date));
expect(batch.updatedAt).toEqual(expect.any(Date));
expect(batch.finish).toEqual(Finish.GLOSSY);
expect(batch.format).toEqual(Format.POSTCARD_SQ15);
expect(batch.templateId).toEqual(template.id);
expect(batch.isBillable).toEqual(expect.any(Boolean));
expect(batch.estimatedPrice).toEqual(0);
Expand Down
Loading