diff --git a/src/resources/Refers.ts b/src/resources/Refers.ts new file mode 100644 index 00000000..f688dbd7 --- /dev/null +++ b/src/resources/Refers.ts @@ -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, + auth?: AuthParams, + ): Promise { + 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, + auth?: AuthParams, + ): Promise { + this._sendCode = + this._sendCode ?? this.defineRoute(HTTPMethod.POST, "(/stores/:storeId)/authorize/customer_login"); + return this._sendCode(data, auth, { storeId }); + } +} diff --git a/src/resources/Subscriptions.ts b/src/resources/Subscriptions.ts index 4ef2a9c8..3b474368 100644 --- a/src/resources/Subscriptions.ts +++ b/src/resources/Subscriptions.ts @@ -62,6 +62,7 @@ export interface ScheduledPaymentItem { isLastPayment: boolean; createdOn: string; successfulPaymentDate?: string; + terminateWithStatus?: SubscriptionStatus; } export type SchedulePaymentListItem = ScheduledPaymentItem; @@ -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, auth?: AuthParams): Promise { + 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, auth?: AuthParams): Promise { + 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 { return ignoreDescriptor( (updatedData: SubscriptionCreateParams) => diff --git a/src/resources/index.ts b/src/resources/index.ts index 6ad5a6c8..056685b1 100644 --- a/src/resources/index.ts +++ b/src/resources/index.ts @@ -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"; diff --git a/test/specs/refers.specs.ts b/test/specs/refers.specs.ts new file mode 100644 index 00000000..8ea91a3f --- /dev/null +++ b/test/specs/refers.specs.ts @@ -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); + }); + }); +}); diff --git a/test/specs/subscriptions.specs.ts b/test/specs/subscriptions.specs.ts index 8d45e463..0eb5596e 100644 --- a/test/specs/subscriptions.specs.ts +++ b/test/specs/subscriptions.specs.ts @@ -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, {