|
1 | 1 | import { PolicyUpdate } from "../generated/PolicyRegistry/PolicyRegistry"; |
2 | 2 | import { Court } from "../generated/schema"; |
3 | | -import { ipfs, log, json, Bytes } from "@graphprotocol/graph-ts"; |
| 3 | +import { ipfs, log, json, Bytes, TypedMap, JSONValue } from "@graphprotocol/graph-ts"; |
4 | 4 |
|
5 | 5 | export function handlePolicyUpdate(event: PolicyUpdate): void { |
6 | 6 | const courtID = event.params._courtID.toString(); |
7 | 7 | const court = Court.load(courtID); |
8 | 8 | if (court) { |
9 | 9 | court.policy = event.params._policy; |
10 | 10 | court.name = event.params._courtName; |
| 11 | + court.save(); |
11 | 12 |
|
12 | 13 | let jsonStr = ipfs.cat(event.params._policy); |
13 | | - if (!jsonStr) { |
14 | | - log.error("Failed to fetch policy #{} SubcourtID: {}", [event.params._policy, event.params._courtID.toString()]); |
15 | | - court.save(); |
16 | | - return; |
17 | | - } |
18 | | - |
19 | | - let jsonObjValueAndSuccess = json.try_fromBytes(jsonStr as Bytes); |
20 | | - if (!jsonObjValueAndSuccess.isOk) { |
21 | | - log.error(`Error getting json object value for policy #{} SubcourtID: {}`, [ |
22 | | - event.params._policy, |
23 | | - event.params._courtID.toString(), |
24 | | - ]); |
25 | | - court.save(); |
26 | | - return; |
27 | | - } |
28 | | - |
29 | | - let jsonObj = jsonObjValueAndSuccess.value.toObject(); |
30 | | - if (!jsonObj) { |
31 | | - log.error(`Error converting object value for policy #{} SubcourtID: {}`, [ |
32 | | - event.params._policy, |
33 | | - event.params._courtID.toString(), |
34 | | - ]); |
35 | | - court.save(); |
36 | | - return; |
37 | | - } |
| 14 | + if (!jsonStr) return; |
38 | 15 |
|
39 | | - const name = jsonObj.get("name"); |
40 | | - court.name = name ? name.toString() : null; |
| 16 | + const jsonObjValueAndSuccess = json.try_fromBytes(jsonStr as Bytes); |
| 17 | + if (!jsonObjValueAndSuccess.isOk) return; |
41 | 18 |
|
42 | | - const description = jsonObj.get("description"); |
43 | | - court.description = description ? description.toString() : null; |
| 19 | + const jsonObj = jsonObjValueAndSuccess.value.toObject(); |
| 20 | + if (!jsonObj) return; |
44 | 21 |
|
45 | | - const requiredSkills = jsonObj.get("requiredSkills"); |
46 | | - court.requiredSkills = requiredSkills ? requiredSkills.toString() : null; |
| 22 | + court.name = tryGetValue(jsonObj, "name"); |
| 23 | + court.description = tryGetValue(jsonObj, "description"); |
| 24 | + court.requiredSkills = tryGetValue(jsonObj, "requiredSkills"); |
47 | 25 | court.save(); |
48 | 26 | } |
49 | 27 | } |
| 28 | + |
| 29 | +function tryGetValue(jsonObj: TypedMap<string, JSONValue>, key: string): string | null { |
| 30 | + const value = jsonObj.get(key); |
| 31 | + return value ? value.toString() : null; |
| 32 | +} |
0 commit comments