This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit-rules-per-country.ts
56 lines (44 loc) · 2.03 KB
/
split-rules-per-country.ts
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
import {renderAsCompactText} from "certlogic-js/dist/misc"
import {normalCopyOf, parseRuleId, Rule} from "dcc-business-rules-utils"
import {join} from "path"
import {mkDir, writeJson} from "./utils/file-utils"
import {groupBy, sortArrayBy} from "./utils/func-utils"
import {
allRulesFile,
RulesStatistics,
rulesStatisticsFile, uploadingCountriesFile
} from "./json-files"
type RuleWithLogicAsText = Rule & { "expr-as-text": string }
const normalised = (rule: Rule): RuleWithLogicAsText => {
const copy: RuleWithLogicAsText = normalCopyOf(rule) as any
copy["expr-as-text"] = renderAsCompactText(rule.Logic)
return copy
}
const rulesPerCountry = groupBy(
allRulesFile.contents,
(rule) => parseRuleId(rule.Identifier).country
)
Object.entries(rulesPerCountry)
.forEach(([ country, rulesOfCountry ]) => {
const countryPath = join("per-country", country)
mkDir(countryPath)
Object.entries(
groupBy(rulesOfCountry, (rule) => rule.Identifier)
).forEach(([ id, ruleVersions ]) => {
const { type, number } = parseRuleId(id)
const sortedRuleVersions = sortArrayBy(ruleVersions, (ruleVersion) => new Date(ruleVersion.ValidFrom).getTime()).reverse()
writeJson(join(countryPath, `${type}-${number}.json`), sortedRuleVersions.map((ruleVersion) => normalised(ruleVersion)))
})
})
const statistics: RulesStatistics[] = Object.entries(rulesPerCountry)
.map(([ co, rules ]) => {
const ruleVersionsPerId = groupBy(rules, (rule) => rule.Identifier)
return ({
co,
n: Object.entries(ruleVersionsPerId).length,
nAcceptance: Object.entries(ruleVersionsPerId).filter(([ _, ruleVersions ]) => ruleVersions[0].Type === "Acceptance").length,
nInvalidation: Object.entries(ruleVersionsPerId).filter(([ _, ruleVersions ]) => ruleVersions[0].Type === "Invalidation").length
})
})
rulesStatisticsFile.contents = statistics
uploadingCountriesFile.contents = statistics.map(({ co }) => co)