-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart1.ts
More file actions
59 lines (54 loc) · 2.03 KB
/
part1.ts
File metadata and controls
59 lines (54 loc) · 2.03 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
import {
CactusClient,
INCOMING_EVENTS,
type OrderLostEvent,
type OrderShippedEvent,
} from "./cactusSdk";
/**
* Instructions:
*
* 1. Swap `parseEventV1()` to `parseEventV2()`
* 2. Resolve any type errors (check w/ `npm run --silent typecheck`)
* 3. Remove any manual casts (`as ...`)
* 4. Resolve any runtime errors (run code w/ `npm run --silent part-1`)
* 5. Add a new handler branch for the `movie.completed` event. It should print the user's rating and the movie's title. Bear in mind that some data is on the event itself while other data is on the related object.
*
*/
const client = new CactusClient();
const eventHandler = (body: string): void => {
const thinEvent = client.parseEventV1(body);
if (thinEvent.type === "order.shiped") {
const orderId = thinEvent.relatedObject.id;
const order = client.retrieveOrder(orderId);
console.log(
` Created a database record for ${orderId} w/ ${order.num_items} items`
);
} else if (thinEvent.type === "order.delivery_attempted") {
const event = client.retrieveEvent(thinEvent.id) as OrderShippedEvent;
console.log(
` Order ${event.relatedObject.id} has been delivered after ${event.data.attempt_num} attempt(s)!`
);
} else if (thinEvent.type === "order.lost") {
const orderId = thinEvent.relatedObject.id;
const event = client.retrieveEvent(thinEvent.id) as OrderLostEvent;
const order = client.retrieveOrder(orderId);
console.log(
` An order was last seen in ${event.data.last_seen_city}... we have no additional information`
);
} else if (thinEvent.type === "movie.started") {
const movieId = thinEvent.relatedObject.id;
const movie = client.retrieveOrder(movieId);
console.log(` Someone started watching ${movie.title}`);
} else {
throw new Error(`Unhandled event w/ type "${thinEvent.type}"`);
}
};
INCOMING_EVENTS.forEach((body, idx) => {
console.log(`\n== parsing event ${idx}`);
try {
eventHandler(body);
} catch (e) {
console.log(` failed to handle: ${body}\n`);
throw e;
}
});