manage.ts
1 import * as pronote from "../../src"; 2 import { credentials } from "../_credentials"; 3 import { select } from "@inquirer/prompts"; 4 5 const isDiscussionInTrash = (discussion: pronote.Discussion) => { 6 return discussion.folders.some((folder) => folder.kind === pronote.DiscussionFolderKind.OCEM_Pre_Poubelle); 7 }; 8 9 void async function main () { 10 const session = pronote.createSessionHandle(); 11 await pronote.loginCredentials(session, { 12 url: credentials.pronoteURL, 13 kind: pronote.AccountKind.STUDENT, 14 username: credentials.username, 15 password: credentials.password, 16 deviceUUID: credentials.deviceUUID 17 }); 18 19 const discussions = await pronote.discussions(session); 20 21 while (true) { 22 // We have to delay the deletion because 23 // it updates the array and breaks the iterations. 24 let discussionsToDelete: pronote.Discussion[] = []; 25 26 for (const discussion of discussions.items) { 27 console.log(discussion.subject); 28 29 if (isDiscussionInTrash(discussion)) { 30 console.log("|> This discussion is in the trash."); 31 32 const action = await select({ 33 message: "What do you want to do?", 34 default: "nothing", 35 choices: [ 36 { 37 name: "Restore the discussion", 38 value: "restore" as const 39 }, 40 { 41 name: "Delete the discussion permanently", 42 value: "delete" as const 43 }, 44 { 45 name: "Nothing", 46 value: "nothing" as const 47 } 48 ] 49 }); 50 51 switch (action) { 52 case "restore": { 53 await pronote.discussionRestoreTrash(session, discussion); 54 console.info("|> Discussion restored."); 55 break; 56 } 57 case "delete": { 58 discussionsToDelete.push(discussion); 59 console.info("|> Discussion will be deleted permanently at the end of this cycle."); 60 break; 61 } 62 case "nothing": 63 break; 64 } 65 } 66 else { 67 console.log("|> This discussion is not in any folder."); 68 69 const action = await select({ 70 message: "What do you want to do?", 71 default: "nothing", 72 choices: [ 73 { 74 name: "Trash the discussion", 75 value: "trash" as const 76 }, 77 { 78 name: "Delete the discussion permanently", 79 value: "delete" as const 80 }, 81 { 82 name: "Nothing", 83 value: "nothing" as const 84 } 85 ] 86 }); 87 88 switch (action) { 89 case "trash": { 90 await pronote.discussionTrash(session, discussion); 91 console.info("|> Discussion trashed."); 92 break; 93 } 94 case "delete": { 95 discussionsToDelete.push(discussion); 96 console.info("|> Discussion will be deleted permanently at the end of this cycle."); 97 } 98 case "nothing": 99 break; 100 } 101 } 102 } 103 104 // Delete selected discussions. 105 if (discussionsToDelete.length > 0) { 106 await Promise.all(discussionsToDelete.map((discussion) => pronote.discussionDelete(session, discussion))); 107 console.log(`|> ${discussionsToDelete.length} discussions deleted permanently.`); 108 } 109 110 const shouldContinue = await select({ 111 message: "All discussions were managed, do you want to continue?", 112 default: "yes", 113 choices: [ 114 { 115 name: "Yes, loop again in the discussions", 116 value: true 117 }, 118 { 119 name: "No, exit", 120 value: false 121 } 122 ] 123 }); 124 125 if (!shouldContinue) break; 126 } 127 }();