-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-candidates.ts
46 lines (41 loc) · 1.07 KB
/
remove-candidates.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
import { readFile } from "fs/promises";
async function main() {
const users: {
chatId?: string;
candidates?: {
chatId: string;
}[];
}[] = JSON.parse(await readFile("data-append.json", "utf-8"));
const chatIds = users
.filter((user) => user.chatId)
.map((user) => user.chatId) as string[];
const chatIdsSet = new Set(chatIds);
const newUsers = users.map((user) => {
if (!user.candidates) {
return {
...user,
chatUrl: getChatUrl(user.chatId),
};
}
return {
...user,
candidates: user.candidates
.filter((candidate) => {
return chatIdsSet.has(candidate.chatId);
})
.map((candidate) => {
return {
...candidate,
chatUrl: getChatUrl(candidate.chatId),
};
}),
};
});
console.log(JSON.stringify(newUsers, null, 2));
}
function getChatUrl(chatId?: string): string {
return chatId
? `https://chat.line.biz/${process.env.LINE_CHANNEL_USER_ID}/chat/${chatId}`
: "";
}
main().catch((err) => console.error(err));