Why did the elite Stormtroopers have sanitation duties when they had millions of slave workers with no personhood-rights whatsoever?
Sanitation is a low-level duty. Basically you do it while you're a cadet, long before you get to go out and shoot people.
After years of training, FN-2187 had itched for some real action. Like other stormtrooper cadets, he’d rotated through several lower-level duties, including a janitorial assignment, but he hadn’t seen any real live-fire action yet.
Star Wars: Finn's Story
They were stormtroopers, but they weren’t quite, not yet. They were cadets, and as cadets they had additional duties aside from their training. Those duties covered everything from maintaining the armory to performing minor repairs on equipment to quite literally moving equipment from one location to another, often by hand but frequently with the assistance of the heavy-lifter droids, when whatever was to be moved was too big to be moved manually. They mopped the floors. They emptied the trash. They worked in the galley preparing meals.
Star Wars: Before the Awakening.
The goal seems to be that since you have a large force of cadet troops, you might as well get them to do something useful with their off-time. It teaches them about the base, about military discipline and procedures and about the importance of keeping their shit wired tight.
Fatigue duty is a long established military tradition. In the British military one form of punishment is "Restrictions of Privileges" and would usually involve repeated inspections of uniform and kit mixed with assorted menial tasks that could include, yes, sanitation duty. The American UCMJ allows the assignment of fatigue duty (over and above regular duties) as a form of non-judicial punishment, and the Canadian Forces allows the assignment of extra work in similar circumstances.
There is no other viable reason, except to portray Finn as someone who didn't bloody his hands (literally and figuratively :) ) until events of Episode VII . Note that many (all) armies in real world use soldiers for sanitation duties, partially to keep them busy and partially to reduce costs. But in SW universe there is no real reason for that, droids would be more effective and stormtroopers could spend more time preparing for combat.
At least some special forces maintain their own barracks which is probably partly out of convince but I have heard that it is to teach humility.
Alternatively to make the force self reliant, so that when stationed on a ship if the plumbers are killed/destroyed the combat troops can take over. A marine stationed on a ship doesn't just wait until they are near the shore before doing anything.
Least plausible for in the film but possible is the First Order wants every soldier to learn a civilian trade before they are retired. The forces care about the well being of their members as much as the country cares about not having a former army with mental disorders and no job prospects.