-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgrantBadges.js
64 lines (56 loc) · 1.74 KB
/
grantBadges.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
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
require('dotenv').config();
const { sign } = require('jsonwebtoken');
const { apiFetch } = require('@codeday/topo/utils');
const TYPE_TO_BADGE = {
'best-in-show': 'best-in-show',
'best-in-class': 'best-in-class',
special: 'special-prize',
};
const token = sign({ scopes: 'write:users' }, process.env.ACCOUNT_SECRET, { expiresIn: '5m' });
// eslint-disable-next-line no-secrets/no-secrets
const queryFetchMembers = `
query ShowYourWorkBadgesOwed($eventGroup: String!) {
showcase {
projects(where: { awarded: true, eventGroup: $eventGroup }) {
awards {
type
}
members {
username
}
}
}
}
`;
const queryGrantBadge = `
mutation GrantBadge ($username: String!, $badge: ID!) {
account {
grantBadge(where: {username: $username}, badge: { id: $badge })
}
}
`;
async function fetchUsers(season) {
const result = await apiFetch(queryFetchMembers, { eventGroup: season });
return result.showcase
.projects.map((p) => p.members.map((m) => p.awards.map((a) => ({
username: m.username,
badge: TYPE_TO_BADGE[a.type] || null,
}))))
.reduce((accum, e) => [...accum, ...e], [])
.reduce((accum, e) => [...accum, ...e], [])
.filter((e) => e.username && e.badge);
}
async function grantBadge({ username, badge }) {
return apiFetch(queryGrantBadge, { username, badge }, { Authorization: `Bearer ${token}` });
}
(async () => {
const users = await fetchUsers(process.argv[2]);
for (const user of users) {
try {
console.log(`Granting badge ${user.badge} to ${user.username}`);
console.log(await grantBadge(user));
} catch (ex) { console.error(ex); }
}
})();