Skip to content

Commit

Permalink
feat: ✨ support batch order's API urls
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRill00 committed Mar 18, 2024
1 parent c526300 commit c2ca919
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
16 changes: 14 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,10 @@ 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}`);
const urlPrefix = this.batchId ? `batches/${this.batchId}/` : "";
this._data = await this._protected.client.GET<IOrder>(
`${urlPrefix}orders/${this.id}`,
);
}

/**
Expand Down Expand Up @@ -168,8 +179,9 @@ export class Order {
time++;
}

const urlPrefix = this.batchId ? `batches/${this.batchId}/` : "";
this._data = await this._protected.client.POST<IOrder>(
`orders/${this.id}/cancel`,
`${urlPrefix}orders/${this.id}/cancel`,
{},
);
}
Expand Down
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;
};
1 change: 1 addition & 0 deletions test/Batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ describe("getOrder", function () {
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
6 changes: 6 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

0 comments on commit c2ca919

Please sign in to comment.