-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathcustomerselfservice.js
145 lines (134 loc) · 3.76 KB
/
customerselfservice.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* This module exports functions to call the Customer Self-Service backend API
*
* @module api/customerselfservice
* @flow
*/
import { customerSelfServiceBackend as backend } from "../config"
import {
postJson,
getAuthenticatedJson,
postAuthenticatedJson,
putAuthenticatedJson,
patchAuthenticatedJson,
} from "./helpers"
/** Builds the URL to the backend API */
function urlForEndpoint(endpoint: string): string {
return `${backend}${endpoint}`
}
/**
* Logs in a user with the provided credentials.
* @param {string} email
* @param {string} password
*/
export function login(
email: string,
password: string
): Promise<{ token: string }> {
const url = urlForEndpoint("/auth")
return postJson(url, { email, password })
}
/**
* Signs up a new user with the provided credentials.
* @param {*string} email
* @param {*string} password
*/
export function signup(email: string, password: string): Promise<User> {
const url = urlForEndpoint("/auth/signup")
return postJson(url, { email, password })
}
/**
* Returns the currently logged in user.
* @param {string} token
*/
export function getUserDetails(token: string): Promise<User> {
const url = urlForEndpoint("/user")
return getAuthenticatedJson(url, token)
}
/**
* Returns the customer with the given Id.
* @param {string} token
* @param {CustomerId} customerId
*/
export function getCustomer(
token: string,
customerId: CustomerId
): Promise<Customer> {
const url = urlForEndpoint(`/customers/${customerId}`)
return getAuthenticatedJson(url, token)
}
/**
* After signing up, customers have to provide additional information about
* themselves. A signup is only complete when this method has been called.
* @param {string} token
* @param {*} data
*/
export function completeRegistration<T>(
token: string,
data: T
): Promise<Customer> {
const url = urlForEndpoint("/customers")
return postAuthenticatedJson(url, token, data)
}
/**
* Changes the current address of the given customer.
* @param {string} token
* @param {Customer} customer
* @param {Adress} address
*/
export function changeAddress(
token: string,
customer: Customer,
address: Address
): Promise<Address> {
// Instead of assembling the URL to change the address ourselves,
// we use the one provided in the customer response.
const url = customer._links["address.change"].href
return putAuthenticatedJson(url, token, address)
}
/**
* Fetches a list of city suggestions for the given postal code.
* @param {string} token
* @param {string} postalCode
*/
export function lookupCitiySuggestions(
token: string,
postalCode: string
): Promise<CitySuggestions> {
const url = urlForEndpoint(`/cities/${postalCode}`)
return getAuthenticatedJson(url, token)
}
export function createInsuranceQuoteRequest(
token: string,
data: InsuranceQuoteRequest
): Promise<InsuranceQuoteRequest> {
const url = urlForEndpoint("/insurance-quote-requests")
return postAuthenticatedJson(url, token, data)
}
export function getInsuranceQuoteRequests(
token: string,
customerId: CustomerId
): Promise<[InsuranceQuoteRequest]> {
const url = urlForEndpoint(
`/customers/${customerId}/insurance-quote-requests`
)
return getAuthenticatedJson(url, token)
}
export function getInsuranceQuoteRequest(
token: string,
id: string
): Promise<InsuranceQuoteRequest> {
const url = urlForEndpoint(`/insurance-quote-requests/${id}`)
return getAuthenticatedJson(url, token)
}
export function respondToInsuranceQuote(
token: string,
insuranceQuoteRequestId: string,
accepted: boolean
): Promise<InsuranceQuoteRequest> {
const url = urlForEndpoint(
`/insurance-quote-requests/${insuranceQuoteRequestId}`
)
const data = { status: accepted ? "QUOTE_ACCEPTED" : "QUOTE_REJECTED" }
return patchAuthenticatedJson(url, token, data)
}