-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
235 lines (205 loc) · 9.33 KB
/
service.ts
File metadata and controls
235 lines (205 loc) · 9.33 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import Boom from "@hapi/boom";
import { withServiceErrorHandling } from "../../utilities/error";
import { ILocationAddressTransaction } from "./transaction";
import { DeleteResult } from "typeorm";
import {
CreateLocationAddressBulkDTO,
CreateLocationAddressBulkResponse,
CreateLocationAddressDTO,
CreateLocationAddressResponse,
GetLocationAddressDTO,
GetLocationAddressResponse,
LocationAddress,
UpdateLocationAddressBulkDTO,
UpdateLocationAddressBulkResponse,
UpdateLocationAddressDTO,
UpdateLocationAddressResponse,
} from "../../types/Location";
import { IFEMALocationMatcher } from "../clients/fips-location-matching/service";
export interface ILocationAddressService {
/**
* Attempts to create a new location address with the given payload
* @throws If the given payload cannot be used to create a location address
* @param payload The payload used to try to create a new location address
*/
createLocationAddress(payload: CreateLocationAddressDTO, companyId: string): Promise<CreateLocationAddressResponse>;
createLocationAddressBulk(
payload: CreateLocationAddressBulkDTO,
companyId: string
): Promise<CreateLocationAddressBulkResponse>;
/**
* Attempts to find a location address with the given payload
* @throws If the given payload does not map to a location address
* @param payload The payload used to try find the location address
*/
getLocationAddress(payload: GetLocationAddressDTO): Promise<GetLocationAddressResponse>;
/**
* Attempts to remove a location address with the given id (in the payload)
* @throws If the given id does not map to a location address
* @param payload contains the id for the location that must be removed
*/
removeLocationAddressById(payload: GetLocationAddressDTO): Promise<DeleteResult>;
/**
* Attempts to update a location address based on the given id
* @param payload The updated location address
*/
updateLocationAddressById(
payload: UpdateLocationAddressDTO,
companyId: string
): Promise<UpdateLocationAddressResponse>;
/**
* Attempts to update multiple location addresses in bulk
* @param payload The updated location addresses
*/
updateLocationAddressBulk(
payload: UpdateLocationAddressBulkDTO,
companyId: string
): Promise<UpdateLocationAddressBulkResponse>;
}
export class LocationAddressService implements ILocationAddressService {
private locationAddressTransaction: ILocationAddressTransaction;
private locationMatcher: IFEMALocationMatcher;
constructor(locationAddressTransaction: ILocationAddressTransaction, locationMatcher: IFEMALocationMatcher) {
this.locationAddressTransaction = locationAddressTransaction;
this.locationMatcher = locationMatcher;
}
/**
* Attempts to create a new location address with the given payload
* @throws If the given payload cannot be used to create a location address
* @param payload The payload used to try to create a new location address
*/
createLocationAddress = withServiceErrorHandling(
async (payload: CreateLocationAddressDTO, companyId: string): Promise<CreateLocationAddressResponse> => {
const fipsLocation = await this.locationMatcher.getLocationFips(payload);
if (
fipsLocation === null ||
!fipsLocation ||
fipsLocation.fipsStateCode === null ||
fipsLocation.fipsCountyCode === null
) {
throw Boom.badRequest(
`Please enter a valid address. Unable to validate address: ${payload.streetAddress}, ${payload.city}, ${payload.stateProvince}.`
);
}
// Add FIPS codes into payload to send to transaction layer
const completePayload = {
...payload,
fipsStateCode: Number(fipsLocation.fipsStateCode),
fipsCountyCode: Number(fipsLocation.fipsCountyCode),
lat: fipsLocation.lat,
long: fipsLocation.long,
};
// Pass complete data with fips codes to transaction layer
const locationAddress = await this.locationAddressTransaction.createLocationAddress(
completePayload,
companyId
);
if (!locationAddress) {
throw new Error("Failed to create location address");
}
return locationAddress;
}
);
/**
* Attempts to find a location address with the given payload
* @throws If the given payload does not map to a location address
* @param payload The payload used to try find the location address
*/
getLocationAddress = withServiceErrorHandling(async (payload: GetLocationAddressDTO): Promise<LocationAddress> => {
const locationAddress = await this.locationAddressTransaction.getLocationAddressById(payload);
if (!locationAddress) {
throw Boom.notFound("No Location Address found with the given the ID");
}
return locationAddress;
});
removeLocationAddressById = withServiceErrorHandling(
async (payload: GetLocationAddressDTO): Promise<DeleteResult> => {
const result = await this.locationAddressTransaction.removeLocationAddressById(payload);
return result;
}
);
createLocationAddressBulk = withServiceErrorHandling(
async (
payload: CreateLocationAddressBulkDTO,
companyId: string
): Promise<CreateLocationAddressBulkResponse> => {
const transformedPayload = await Promise.all(
payload.map(async (element) => {
const currFips = await this.locationMatcher.getLocationFips(element);
if (!currFips || currFips.fipsStateCode === null || currFips.fipsCountyCode === null) {
throw Boom.badRequest(
`Please enter a valid address. Unable to validate address: ${element.streetAddress}, ${element.city}, ${element.stateProvince}.`
);
}
return { ...element, ...currFips };
})
);
const result = await this.locationAddressTransaction.createLocationAddressBulk(
transformedPayload,
companyId
);
if (result.length !== payload.length) {
throw Boom.internal(
"The number of created addresses does not match the number of given new addresses."
);
}
return result;
}
);
updateLocationAddressById = withServiceErrorHandling(
async (payload: UpdateLocationAddressDTO, companyId: string): Promise<UpdateLocationAddressResponse> => {
// get the current location
const locationAddress = await this.locationAddressTransaction.getLocationAddressById({ id: payload.id });
if (!locationAddress) {
throw Boom.badRequest("No location address with given id found");
}
const locationForMatching: Partial<LocationAddress> = {
country: payload.country ?? locationAddress.country,
stateProvince: payload.stateProvince ?? locationAddress.stateProvince,
city: payload.city ?? locationAddress.city,
streetAddress: payload.streetAddress ?? locationAddress.streetAddress,
postalCode: payload.postalCode ?? locationAddress.postalCode,
county: payload.county ?? locationAddress.county,
};
// get the new fips codes if any of the address fields have changed
const fipsLocation = await this.locationMatcher.getLocationFips(locationForMatching);
if (
fipsLocation === null ||
!fipsLocation ||
fipsLocation.fipsStateCode === null ||
fipsLocation.fipsCountyCode === null
) {
throw Boom.badRequest(
`Please enter a valid address. Unable to validate address: ${payload.streetAddress}, ${payload.city}, ${payload.stateProvince}.`
);
}
const updatedLocationWithFips = {
...payload,
fipsStateCode: Number(fipsLocation.fipsStateCode),
fipsCountyCode: Number(fipsLocation.fipsCountyCode),
lat: fipsLocation.lat,
long: fipsLocation.long,
};
const updatedLocationAddress = await this.locationAddressTransaction.updateLocationAddressById(
updatedLocationWithFips,
companyId
);
if (!updatedLocationAddress) {
throw Boom.internal("Could not update Location");
}
return updatedLocationAddress;
}
);
updateLocationAddressBulk = withServiceErrorHandling(
async (
payload: UpdateLocationAddressBulkDTO,
companyId: string
): Promise<UpdateLocationAddressBulkResponse> => {
const updatedLocations = await this.locationAddressTransaction.updateLocationAddressBulk(
payload,
companyId
);
return updatedLocations;
}
);
}