-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.js
80 lines (67 loc) · 1.6 KB
/
testing.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
const { addYears, isAfter } = require('date-fns');
const { parseFuzzyDate } = require('./date');
const SHODAN = 'SHODAN';
const NIDAN = 'NIDAN';
const SANDAN = 'SANDAN';
const YONDAN = 'YONDAN';
const GODAN = 'GODAN';
const eligibility = {
MUDAN: {
next: SHODAN
},
SHODAN: {
years: 1,
seminars: 3,
next: NIDAN
},
NIDAN: {
years: 1,
seminars: 6,
next: SANDAN
},
SANDAN: {
years: 2,
seminars: 10,
taikai: 2,
next: YONDAN
},
YONDAN: {
years: 3,
seminars: 14,
taikai: 3,
next: GODAN
},
GODAN: {
years: 4,
seminars: 16
// No next rank
},
ROKUDAN: {
// No info here
}
};
function isEligibleToTest (member, today = new Date()) {
const memberRank = member.currentRank.toUpperCase();
const rawLastDate = member.ranks?.[0]?.date || member.joined;
// If we don't know any date info, we can't tell folks they're eligible to test
if (!rawLastDate) return false;
const lastDate = parseFuzzyDate(rawLastDate);
const nextRank = eligibility[memberRank].next;
// Member is already yondan, they'll know when they're ready
if (!nextRank) return false;
const { years, seminars, taikai } = eligibility[nextRank];
let yearsOk = false;
let seminarsOk = false;
let taikaiOk = !taikai;
if (isAfter(today, addYears(lastDate.val, years))) {
yearsOk = true;
}
if ((member.seminars?.length || 0) >= seminars) {
seminarsOk = true;
}
if ((member.taikai?.length || 0) >= (taikai || 0)) {
taikaiOk = true;
}
return yearsOk && seminarsOk && taikaiOk;
}
module.exports.isEligibleToTest = isEligibleToTest;