Skip to content

Commit

Permalink
Add refer routes (#958)
Browse files Browse the repository at this point in the history
* Add refer routes

* Fix refer routes

* Add tests
  • Loading branch information
phieronymus authored Dec 6, 2023
1 parent 3a78a23 commit 5120731
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/resources/Refers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @module Resources/Refers
*/

import { AuthParams, HTTPMethod, SendData } from "../api/RestAPI.js";

import { CRUDResource } from "./CRUDResource.js";
import { DefinedRoute } from "./Resource.js";
import { CustomerRole } from "./common/Configuration.js";

export type ReferAuthenticateResponse = {
/**
* Parsed value of type ParsedAuthenticateToken
*/
jwt: string;
};

export type ReferAuthenticatePayload = {
email: string;
otp: string;
};

export type ReferSendCodePayload = {
email: string;
};

export type ParsedAuthenticateToken = {
email: string;
roles: CustomerRole[];
store_id: string;
name: string;
lang: string;
logo_uri: string;
ip_address: string;
};

export class Refers extends CRUDResource {
static routeBase = "/stores/:storeId/refers";

private _authorize: DefinedRoute;
authorize(
storeId: string,
data: SendData<ReferAuthenticatePayload>,
auth?: AuthParams,
): Promise<ReferAuthenticateResponse> {
this._authorize =
this._authorize ?? this.defineRoute(HTTPMethod.POST, "(/stores/:storeId)/authorize/customer_login");
return this._authorize(data, auth, { storeId });
}

private _sendCode: DefinedRoute;
sendCode(
storeId: string,
data: SendData<ReferSendCodePayload>,
auth?: AuthParams,
): Promise<ReferAuthenticateResponse> {
this._sendCode =
this._sendCode ?? this.defineRoute(HTTPMethod.POST, "(/stores/:storeId)/authorize/customer_login");
return this._sendCode(data, auth, { storeId });
}
}
15 changes: 15 additions & 0 deletions src/resources/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface ScheduledPaymentItem {
isLastPayment: boolean;
createdOn: string;
successfulPaymentDate?: string;
terminateWithStatus?: SubscriptionStatus;
}

export type SchedulePaymentListItem = ScheduledPaymentItem;
Expand Down Expand Up @@ -339,6 +340,20 @@ export class Subscriptions extends CRUDResource {
return this._list(data, auth, { storeId });
}

private _suspend?: DefinedRoute;
suspend(storeId: string, id: string, data?: SendData<void>, auth?: AuthParams): Promise<ResponsePayment> {
this._suspend =
this._suspend ?? this.defineRoute(HTTPMethod.PATCH, "(/stores/:storeId)/subscriptions/:id/suspend");
return this._suspend(data, auth, { storeId, id });
}

private _unsuspend?: DefinedRoute;
unsuspend(storeId: string, id: string, data?: SendData<void>, auth?: AuthParams): Promise<ResponsePayment> {
this._unsuspend =
this._unsuspend ?? this.defineRoute(HTTPMethod.PATCH, "(/stores/:storeId)/subscriptions/:id/unsuspend");
return this._unsuspend(data, auth, { storeId, id });
}

create(data: SubscriptionCreateParams, auth?: AuthParams): Promise<ResponseSubscription> {
return ignoreDescriptor(
(updatedData: SubscriptionCreateParams) =>
Expand Down
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from "./Ledgers.js";
export * from "./Merchants.js";
export * from "./Platforms.js";
export * from "./Products.js";
export * from "./Refers.js";
export * from "./Refunds.js";
export * from "./Resource.js";
export * from "./Stores.js";
Expand Down
49 changes: 49 additions & 0 deletions test/specs/refers.specs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect } from "chai";
import fetchMock from "fetch-mock";
import { v4 as uuid } from "uuid";

import { RestAPI } from "../../src/api/RestAPI.js";
import { Refers } from "../../src/resources/index.js";
import { testEndpoint } from "../utils/index.js";
import { pathToRegexMatcher } from "../utils/routes.js";

describe("Refers", () => {
let api: RestAPI;
let refers: Refers;

beforeEach(() => {
api = new RestAPI({ endpoint: testEndpoint });
refers = new Refers(api);
});

afterEach(() => {
fetchMock.restore();
});

context("POST (/stores/:storeId)/authorize/customer_login", () => {
const recordData = { jwt: "dummy-jwt" };
const recordPathMatcher = pathToRegexMatcher(`${testEndpoint}/stores/:storeId/authorize/customer_login`);

it("should get authorize response", async () => {
fetchMock.postOnce(recordPathMatcher, {
status: 200,
body: recordData,
headers: { "Content-Type": "application/json" },
});

await expect(refers.authorize(uuid(), { email: "myemail@univapay.com", otp: "test" })).to.become(
recordData,
);
});

it("should get send code response", async () => {
fetchMock.postOnce(recordPathMatcher, {
status: 200,
body: recordData,
headers: { "Content-Type": "application/json" },
});

await expect(refers.sendCode(uuid(), { email: "myemail@univapay.com" })).to.become(recordData);
});
});
});
24 changes: 24 additions & 0 deletions test/specs/subscriptions.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ describe("Subscriptions", () => {
});
});

context("PATCH /stores/:storeId/subscriptions/:id/suspend", () => {
it("should get response", async () => {
fetchMock.patchOnce(pathToRegexMatcher(`${testEndpoint}/stores/:storeId/subscriptions/:id/suspend`), {
status: 200,
body: recordData,
headers: { "Content-Type": "application/json" },
});

await expect(subscriptions.suspend(uuid(), uuid())).to.become(recordData);
});
});

context("PATCH /stores/:storeId/subscriptions/:id/unsuspend", () => {
it("should get response", async () => {
fetchMock.patchOnce(pathToRegexMatcher(`${testEndpoint}/stores/:storeId/subscriptions/:id/unsuspend`), {
status: 200,
body: recordData,
headers: { "Content-Type": "application/json" },
});

await expect(subscriptions.unsuspend(uuid(), uuid())).to.become(recordData);
});
});

context("DELETE /stores/:storeId/subscriptions/:id", () => {
it("should get response", async () => {
fetchMock.deleteOnce(recordPathMatcher, {
Expand Down

0 comments on commit 5120731

Please sign in to comment.