Is there a different or better way to sort files created from template?

Am I supposed to call this in my template or just leave it in scripts and do nothing?
So far I have tried this as a user script:

async function organizeNotes() {
    // Let frontmatter values = folders pertaining
	const classMapping = {
        "cse1icb": "cse1icb - introduction to cybersecurity",
        "cse1iit": "cse1iit - inside information technology",
        "cse1pe": "cse1pe - programming environment",
        "sta1dct": "sta1dct - data-based critical thinking"
    };

	// Get frontmatter values
    const currentClass = tp.frontmatter.class;
    const currentWeek = parseInt(tp.frontmatter.week);

	// Set targetFolder and weekSubfolder
    if (classMapping.hasOwnProperty(currentClass) && !isNaN(currentWeek) && currentWeek >= 1 && currentWeek <= 12) {
        const targetFolder = classMapping[currentClass];
        const weekSubfolder = `Week ${currentWeek}`;

        // Construct the destination path
        const destinationPath = `${targetFolder}/${weekSubfolder}/${tp.file.title}`;

        // Move the file
        await tp.file.move(destinationPath);
    }
}

// Call the function
organizeNotes();

But I get this error in console:

The boilerplate for a tp.user.function is like the following:

function my_function (msg) {
    return `Message from my script: ${msg}`;
}
module.exports = my_function;

You’re not supposed to call the function at the end of the script, and doing so will cause issues like you’ve experienced. In addition many of us tend to add the tp variable as a parameter to our user functions, as that allows for it to be used in various other contexts.

Adding the tp to the parameter list will however mean you need to do <% tp.user.organizeNotes(tp) %> or similar to properly pass it into your user function.