Skip to content

Commit

Permalink
Merge pull request #20 from truework/jcrane/port-to-orders-api
Browse files Browse the repository at this point in the history
Port server to orders API
  • Loading branch information
jordancrane-truework authored Feb 7, 2024
2 parents 7e5e9a9 + 24692f4 commit 3076844
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 53 deletions.
49 changes: 27 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,31 @@ app.use(express.json());

app.get("/token", async (req, res) => {
const USER_PAYLOAD = {
type: "employment",
permissible_purpose: "credit-application",
use_case: "mortgage",
target: {
first_name: "Jane",
last_name: "Doe",
social_security_number: "000-20-0000",
contact_email: "[email protected]",
date_of_birth: "2020-02-02",
company: {
name: "Acme Inc",
targets: [
{
companies: [
{
name: "Acme Corp",
},
],
contact_email: "[email protected]",
date_of_birth: "2001-08-24",
first_name: "Jane",
last_name: "Doe",
permissible_purpose: "credit-application",
social_security_number: "000-20-0000",
type: "employment-income",
use_case: "mortgage",
},
},
],
};

const { data, error, status, response } = await gretch(
`${API_BASE_URL}/credentials/session`,
`${API_BASE_URL}/orders/truework-direct`,
{
method: "POST",
headers: {
Accept: "application/json",
Accept: "application/json; version=2023-10-30",
Authorization: `Bearer ${TW_SANDBOX_API_TOKEN}`,
},
json: USER_PAYLOAD,
Expand All @@ -60,7 +64,11 @@ app.get("/token", async (req, res) => {

console.info(`Session created:\n${JSON.stringify(data, null, 2)}`);

res.json(data).send();
res
.json({
token: data.truework_direct.targets[0].truework_direct_session_token,
})
.send();
});

app.post("/webhook", async (req, res) => {
Expand Down Expand Up @@ -91,17 +99,14 @@ app.post("/webhook", async (req, res) => {
/*
* If the verification is completed, get the data from the api
*/
if (
req.body.hook.event === "verification_request.state.change" &&
req.body.data.state === "completed"
) {
const id = req.body.data.verification_request_id;
if (req.body.hook.event === "order.completed") {
const id = req.body.data.order_id;
const { status, data, error, response } = await gretch(
`${API_BASE_URL}/verification-requests/${id}`,
`${API_BASE_URL}/orders/${id}`,
{
headers: {
Authorization: `Bearer ${TW_SANDBOX_API_TOKEN}`,
Accept: "application/json; version=2020-12-07",
Accept: "application/json; version=2023-10-30",
},
}
).json();
Expand Down
34 changes: 3 additions & 31 deletions test/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ const app = require("../server");
const requestWithSupertest = supertest(app);

describe("Webhook Endpoint", () => {
it("POST /webhook should accept valid verification_request.state.change event", async () => {
it("POST /webhook should accept valid order.completed event", async () => {
const stateChangeProcessing = {
hook: {
id: 68,
event: "verification_request.state.change",
event: "order.completed",
target: "https://1345-135-180-37-121.ngrok.io/webhook",
},
data: {
verification_request_id:
"AAAAAAAAAmYAB3acTtZ3DS2-6EgllSFmL8O9VfY3QwtnvaG9jvw6XHOV",
state: "processing",
credentials_session_token:
"FnwaBrkdPFYwqycSRv_iBrqjmKeM_fxJWzNutNZi_Fg",
order_id: "AAAAAAAAAmYAB3acTtZ3DS2-6EgllSFmL8O9VfY3QwtnvaG9jvw6XHOV",
},
};

Expand All @@ -36,30 +32,6 @@ describe("Webhook Endpoint", () => {
expect(res.status).toEqual(200);
});

it("POST /webhook should accept valid credentials_session.state.change event", async () => {
const stateChangeProcessing = {
hook: {
id: 50,
event: "credentials_session.state.change",
target: "https://example.com/webhook",
},
data: {
state: "success",
token: "GF7UOlmqj_4X4Sui99qvjc6jA-CHEyyhmQlmJ7yIuoM",
verification_request_id:
"AAAAAAAAAZ0ABz8-Lz-DIPDFfECWdfcOh8GHJQPZGFiSmb1SyxtR2O3z",
},
};
const res = await requestWithSupertest
.post("/webhook")
.set({
"x-truework-token":
"b1kZINxckg1B75mVU_HjpxCmqeLvLx4ZYpZ5c7NeXM3OILaZbYgVRqkXLWHKZ7SR3enrN1WUGnQKfoqcJBDsLQ",
})
.send(stateChangeProcessing);
expect(res.status).toEqual(200);
});

it("POST /webhook should return 403 without a valid security token", async () => {
const res = await requestWithSupertest.post("/webhook").send("Hello");
expect(res.status).toEqual(403);
Expand Down

0 comments on commit 3076844

Please sign in to comment.