This repository has been archived by the owner on Aug 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
death-certificates.js
347 lines (300 loc) · 8.13 KB
/
death-certificates.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// @flow
import moment from 'moment-timezone';
import type { Context } from './index';
import type RegistryOrders from '../services/RegistryOrders';
import type RegistryData, {
DeathCertificateSearchResult,
DeathCertificate as DbDeathCertificate,
} from '../services/RegistryData';
export const Schema = `
type DeathCertificate {
id: String!
firstName: String!
lastName: String!
deathDate: String
deathYear: String!
pending: Boolean
age: String
birthDate: String
}
# Pages are 1-indexed to make the UI look better
type DeathCertificateSearch {
page: Int!
pageSize: Int!
pageCount: Int!
results: [DeathCertificate!]!
resultCount: Int!
}
type CertificateOrderItem {
id: String!
quantity: Int!
cost: Int!
certificate: DeathCertificate
}
type DeathCertificateOrder {
id: String!
date: String!
contactName: String!
contactEmail: String!
contactPhone: String!
shippingName: String!
shippingCompanyName: String!
shippingAddress1: String!
shippingAddress2: String!
shippingCity: String!
shippingState: String!
shippingZip: String!
cardholderName: String!
billingAddress1: String!
billingAddress2: String!
billingCity: String!
billingState: String!
billingZip: String!
items: [CertificateOrderItem!]!
certificateCost: Int!
subtotal: Int!
serviceFee: Int!
total: Int!
}
type DeathCertificates {
search(query: String!, page: Int, pageSize: Int, startYear: String, endYear: String): DeathCertificateSearch!
certificate(id: String!): DeathCertificate
certificates(ids: [String!]!): [DeathCertificate]!
order(id: String!, contactEmail: String!): DeathCertificateOrder
}
`;
const DEFAULT_PAGE_SIZE = 20;
const MAX_PAGE_SIZE = 500;
type SearchArgs = {
query: string,
page?: number,
pageSize?: number,
startYear?: string,
endYear?: string,
};
type CertificateArgs = {
id: string,
};
type CertificatesArgs = {
ids: string[],
};
type OrderLookupArgs = {|
id: string,
contactEmail: string,
|};
type DeathCertificate = {
id: string,
firstName: string,
lastName: string,
deathDate: ?string,
deathYear: string,
pending: boolean,
age: ?string,
};
type DeathCertificateSearch = {
page: number,
pageSize: number,
pageCount: number,
results: DeathCertificate[],
resultCount: number,
};
type CertificateOrderItem = {
id: string,
quantity: number,
cost: number,
certificate: () => Promise<?DeathCertificate>,
};
type DeathCertificateOrder = {
id: string,
date: string,
contactName: string,
contactEmail: string,
contactPhone: string,
shippingName: string,
shippingCompanyName: string,
shippingAddress1: string,
shippingAddress2: string,
shippingCity: string,
shippingState: string,
shippingZip: string,
cardholderName: string,
billingAddress1: string,
billingAddress2: string,
billingCity: string,
billingState: string,
billingZip: string,
items: Array<CertificateOrderItem>,
certificateCost: number,
subtotal: number,
serviceFee: number,
total: number,
};
const DATE_REGEXP = /\(?\s*(\d\d?\/\d\d?\/\d\d\d\d)\s*\)?/;
export function parseAgeOrDateOfBirth(
str: ?string
): { age: ?string, birthDate: ?string } {
const dateMatch = (str || '').match(DATE_REGEXP);
let age = (str || '')
.replace(/^0+/, '')
.replace(DATE_REGEXP, '')
.trim();
if (dateMatch && !age) {
age = moment().diff(moment(dateMatch[1], 'MM/DD/YYYY'), 'years');
}
return {
age: age ? `${age}` : null,
birthDate: dateMatch ? dateMatch[1] : null,
};
}
function searchResultToDeathCertificate(
res: DeathCertificateSearchResult | DbDeathCertificate
): DeathCertificate {
const { age, birthDate } = parseAgeOrDateOfBirth(res.AgeOrDateOfBirth);
return {
id: res.CertificateID.toString(),
firstName: res['First Name'],
lastName: res['Last Name'],
deathDate: res['Date of Death'],
deathYear: res.RegisteredYear,
pending: !!res.Pending,
age,
birthDate,
};
}
// We export this function so that other places can take advantage of how it
// parses out ids and amounts. That's a little wonky, since this is still fairly
// GraphQL-specific in its return values.
export async function loadOrder(
registryData: RegistryData,
registryOrders: RegistryOrders,
id: string
): Promise<?DeathCertificateOrder> {
const order = await registryOrders.findOrder(id);
if (!order) {
return null;
}
const certificateIds = order.CertificateIDs.split(',');
const certificateQuantities = order.CertificateQuantities
.split(',')
.map(q => parseInt(q, 10));
const certificateCount = certificateQuantities.reduce((sum, q) => q + sum, 0);
const certificateCost = Math.floor(
order.CertificateCost * 100 / certificateCount
);
const items = certificateIds.map((id, idx) => {
const quantity = certificateQuantities[idx];
return {
id,
quantity,
cost: quantity * certificateCost,
// Will only be executed if dereferenced.
certificate: async () => {
const cert = await registryData.lookup(id);
return cert ? searchResultToDeathCertificate(cert) : null;
},
};
});
return {
id,
date: moment(order.OrderDate)
.tz('America/New_York')
.format('l h:mmA'),
contactName: order.ContactName,
contactEmail: order.ContactEmail,
contactPhone: order.ContactPhone,
shippingName: order.ShippingName,
shippingCompanyName: order.ShippingCompany,
shippingAddress1: order.ShippingAddr1,
shippingAddress2: order.ShippingAddr2,
shippingCity: order.ShippingCity,
shippingState: order.ShippingState,
shippingZip: order.ShippingZIP,
cardholderName: order.BillingName,
billingAddress1: order.BillingAddr1,
billingAddress2: order.BillingAddr2,
billingCity: order.BillingCity,
billingState: order.BillingState,
billingZip: order.BillingZIP,
items,
certificateCost,
subtotal: Math.floor(order.CertificateCost * 100),
serviceFee: Math.floor(order.ServiceFee * 100),
total: Math.floor(order.TotalCost * 100),
};
}
export const resolvers = {
DeathCertificates: {
search: async (
root: mixed,
{ query, pageSize, page, startYear, endYear }: SearchArgs,
{ registryData }: Context
): Promise<DeathCertificateSearch> => {
const queryPageSize = Math.min(
pageSize || DEFAULT_PAGE_SIZE,
MAX_PAGE_SIZE
);
const queryPage = (page || 1) - 1;
const results: Array<
DeathCertificateSearchResult
> = await registryData.search(
query,
queryPage,
queryPageSize,
startYear,
endYear
);
const resultCount = results.length > 0 ? results[0].ResultCount : 0;
const pageCount = Math.ceil(resultCount / queryPageSize);
return {
page: queryPage + 1,
pageSize: queryPageSize,
pageCount,
resultCount,
results: results.map(searchResultToDeathCertificate),
};
},
certificate: async (
root: mixed,
{ id }: CertificateArgs,
{ registryData }: Context
): Promise<?DeathCertificate> => {
const res = await registryData.lookup(id);
if (res) {
return searchResultToDeathCertificate(res);
} else {
return null;
}
},
certificates: (
root: mixed,
{ ids }: CertificatesArgs,
{ registryData }: Context
): Promise<Array<?DeathCertificate>> =>
Promise.all(
ids.map(async (id): Promise<?DeathCertificate> => {
const res = await registryData.lookup(id);
if (res) {
return searchResultToDeathCertificate(res);
} else {
return null;
}
})
),
order: async (
root: mixed,
{ id, contactEmail }: OrderLookupArgs,
{ registryData, registryOrders }: Context
): Promise<?DeathCertificateOrder> => {
const order = await loadOrder(registryData, registryOrders, id);
if (!order) {
return null;
}
// Safety check so that you can't easily iterate through all the receipt
// IDs to get sensitive information.
if (order.contactEmail.toLowerCase() !== contactEmail.toLowerCase()) {
return null;
}
return order;
},
},
};