-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add refer routes * Fix refer routes * Add tests
- Loading branch information
1 parent
3a78a23
commit 5120731
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters