-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoanProcessor.ValuationRequestFunction.ts
54 lines (41 loc) · 1.58 KB
/
LoanProcessor.ValuationRequestFunction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import axios from 'axios';
import { LoanApplication } from './LoanApplication';
import TaskTokenStore from './TaskTokenStore';
import {
ValuationRequest,
ValuationRequestResponse,
} from './valuation-service/ValuationRequest';
export class ValuationRequestFunctionEnv {
static readonly SERVICE_URL = 'SERVICE_URL';
static readonly CALLBACK_URL = 'CALLBACK_URL';
static readonly TASK_TOKEN_TABLE_NAME = 'TASK_TOKEN_TABLE_NAME';
}
const valuationServiceUrl =
process.env[ValuationRequestFunctionEnv.SERVICE_URL] ?? '<undefined>';
const callbackUrl =
process.env[ValuationRequestFunctionEnv.CALLBACK_URL] ?? '<undefined>';
const taskTokenStore = new TaskTokenStore(
process.env[ValuationRequestFunctionEnv.TASK_TOKEN_TABLE_NAME]
);
export const handler = async (event: {
taskToken: string;
loanApplication: LoanApplication;
}): Promise<void> => {
console.log(JSON.stringify({ event }, null, 2));
const valuationRequest: ValuationRequest = {
property: event.loanApplication.property,
callbackUrl,
};
const response = await axios.post(valuationServiceUrl, valuationRequest);
if (response.status !== 201) {
throw new Error(`Unexpected response.status: ${response.status}`);
}
console.log(JSON.stringify({ 'response.data': response.data }, null, 2));
const valuationRequestResponse = response.data as ValuationRequestResponse;
await taskTokenStore.putAsync({
keyReference: valuationRequestResponse.valuationReference,
taskToken: event.taskToken,
});
};