From Excel and localStorage to MySQL: Seven Reworks That Shaped an Esports Venue's Next.js 16 Scheduling and Payroll System
I Used Next.js 16 to Build a Scheduling and Payroll System for an Esports Venue: 7 Pitfalls from localStorage to MySQL
An esports venue with a dozen-plus staff used Excel for scheduling and hand calculations for payroll. I built an integrated 'schedule -> attendance -> payroll' system with Next.js 16 + MySQL, and it has run real business for several months. This article covers the three difficulties that actually took time, and the seven pitfalls that made me rework.
1. Origin: a small requirement, a pile of hidden complexity
The requirement sounded simple: schedule shifts and calculate pay for an esports venue.
Actually building it revealed that all the complexity was hidden in the details:
- Scheduling is not 'who works each day'; it has to keep shift patterns stable within a half-month period, otherwise employees' body clocks get wrecked.
- Payroll is not one multiplication; it is eight pay items stacked: base, performance, attendance, holiday triple pay, trial-period pay, overnight allowance, recharge commission, overtime.
- Staff are not fixed: mid-month hires, mid-month departures, trial periods, and trial periods spanning months all have to stay consistent across scheduling, attendance, and payroll.
Tech stack:
| Layer | Choice | Reason |
|---|---|---|
| Framework | Next.js 16 (App Router) | Full-stack in one codebase; Route Handlers are enough for a dozen-person admin backend |
| UI | React 19 + Tailwind CSS 4 + shadcn/ui | Dark console style; data tables are the main character |
| Language | TypeScript 5 (strict) | The domain model is complex; types are the first line of defense |
| Database | MySQL (mysql2/promise) | Scheduling is typical relational data; direct connection + a thin repo layer is more transparent than an ORM |
| Spreadsheets | SheetJS | The boss wants to export schedule and payroll tables to Excel |
2. The three difficulties that really took time
Difficulty 1: Scheduling cannot be 'grabbing shifts day by day'; it needs 'half-month fixed blocks'
The first version followed intuition: iterate over each day and assign whoever is free.
The resulting schedule got rejected by the boss outright: the same employee rotated through early, middle, and late shifts like a revolving door, late followed by early, early followed by late, and staff complained a lot.
The second version switched to half-month fixed blocks: split each month into two segments (1-15 and 16-end), and each person holds one fixed shift for the entire segment.
Four key design rules:
- Slot ownership is decoupled from hire date: a stable sort by name decides who occupies the standard slots (front desk: one each for early/middle/late; servers: early/middle/floating/late/late), so shifts do not jump within the same month.
- New hires go into the floating slots first: employees who joined this month or leave mid-month are sorted to the end and take the floating slots, so they don't crowd out veterans' fixed shifts.
- Floating slots only substitute when someone is missing: when a fixed role is absent that day (comp time/mentoring), the floating position covers in the order 'late > early > middle'.
- Late-to-day transitions get a bridging rest day: people on late shift in the first segment who switch to early/middle in the second get one comp day in the 14-18 window, avoiding weekends and holidays, so their body clock switches smoothly.
/**
* Schedules a whole month for one rotating shift group.
* 1. Stable sort by name to determine standard slot ownership (decoupled from hire date);
* 2. Assign slots per segment (1-15 / 16-end): veterans employed for the whole segment take standard slots, new hires take floating slots;
* 3. Fill within each segment by working days; floating slots substitute only when a fixed role is absent, in the order late > early > middle;
* 4. Employees switching from late to early/middle get one transition comp day in the 14-18 window;
* 5. The whole process writes only blank cells; manually scheduled shifts are always preserved.
*/
function scheduleRotatingGroup(...) { ... }
This rule of 'only write blank cells' is an iron law, and it comes up again later: auto-generated output overwriting manual results is the easiest mistake to make in this kind of system.
Difficulty 2: The payroll engine must be a pure function
Eight pay items, each one an independent business rule:
| Pay item | Formula |
|---|---|
| Base / performance | Full attendance pays full amount; otherwise prorate as standard / 31 * payable attendance |
| Attendance pay | standard * min(payable attendance, D) / D, where D = days in month - monthly rest days |
| Holiday triple pay | base salary / D * holiday days worked * 2 |
| Trial-period pay | trial shift days * daily trial rate (configurable) |
| Overnight allowance | servers: late shifts in the month * per-night standard; other roles: fixed value from profile |
| Recharge commission | servers: flat 5%, no threshold; front desk: tiered (after reaching the threshold, commission is calculated on the full amount) |
| Overtime pay | overtime hours * rate |
| Manual entries | meal commission, outstanding-employee bonus, performance adjustments, management-role allowance, deductions |
The most important design point: the engine is a pure function.
computePayroll(employee, monthData, monthKey, settings, holidays) => PayrollRow
It does not read the database, does not depend on new Date(), and does not mutate any external state. The benefits:
- The same input can be computed repeatedly; after changing a rule, diffing two outputs shows who is affected.
- Real historical months can be used as regression data, guaranteeing that 'only the intended rule changed'.
- Frontend display, Excel export, and snapshot persistence all use the same function, so the calculation basis never forks.
Two other easily overlooked measurement rules:
- Payable attendance is not attendance. Employees on trial period work 'trial shifts'; that pay is calculated separately by day and must not be double-counted into the base-salary proration basis.
- The denominator must be fixed. The daily-rate basis for attendance pay and holiday pay is uniformly
days in month - monthly rest days; it does not float with actual attendance, otherwise the same employee's daily rate for the same calendar day differs across months.
Difficulty 3: Employment period boundaries; changing a profile once changes the whole month
If an employee joins on October 21, October 1-20 should be 'not yet employed', not blank. The same applies for departure.
The most direct approach is to 'modify the database', but that would delete manually scheduled shifts along with it: the boss's manual adjustments cannot be lost.
The solution ended up as a two-layer separation:
| Layer | Approach | Persisted? |
|---|---|---|
| Presentation | normalizeForView: force 'not yet employed' before hire date and 'departed' after termination |
No (in-memory only) |
| Persistence | normalizeEmploymentPeriods: fill only blank cells |
Yes |
The presentation layer is responsible for 'looking right'; the persistence layer is responsible for 'storing right'. The two layers' responsibilities do not overlap. Inside the employment period, existing cells are never touched, so manual schedules are protected.
3. The 7 pitfalls I hit
Pitfall 1: The CREATE TABLE statement 'succeeded', but the table was not in the database the app connects to
Running CREATE TABLE in a SQL tool returned success. Refreshing the page, the API reported Table 'holiday_defs' doesn't exist.
After a long investigation, it turned out the SQL tool was connected to the development database, while the app was connected to a different instance. Any schema change has to be verified with the same connection config the app uses.
Lesson: after a DDL change, confirm once more using the app's own connection configuration.
Pitfall 2: Whole-row snapshot overwrites erased manually entered fields
Payroll snapshots are written as whole-row overwrites. User-entered overtime hours lived in a JSON remark field, and the save path did not pass them back through. Result: the user entered 3 hours of overtime, and after saving it became 0.
Lesson: snapshot/overwrite writes must distinguish between 'engine-derived fields' and 'manual fields'; manual fields have to be explicitly passed through, not assumed.
Pitfall 3: Payroll calculation rules changed 5 times
In real business, payroll rules can almost never be finalized in one pass:
Full attendance definition -> attendance pay deducted by day -> trial shifts separated from attendance -> add statutory holiday triple pay -> change holiday denominator to a fixed basis.
Every adjustment meant recomputing historical months and checking person by person.
There is only one reason this survived: payroll has snapshots, and every write has an audit trail. Otherwise, after a change, you would have no idea which version was correct.
Pitfall 4: React 19 + App Router hydration errors
As soon as localStorage or new Date() appears in render logic, server and client produce different results.
Fix: the global store initializes hydrated = false, and anything depending on the browser environment renders only after useEffect.
Pitfall 5: Date time zones shifted the month by one
new Date('2026-10-01') becomes September 30 in some time zones, so September data showed up inside October's schedule.
Fix: pass dates as 'YYYY-MM-DD' strings everywhere, use pure helper functions (daysInMonth / getDates / dayKey), use local time fields only when a Date must be constructed, and never use toISOString().
Pitfall 6: Auto-scheduling overwrote manual scheduling
The first auto-scheduler rewrote the entire month. Every shift the boss had manually adjusted was gone, and he thought he had misremembered.
Lesson: the iron rule for auto-generation is 'only fill blank cells'. The generation function reads the existing schedule once and skips any cell that already has a value. This rule saved us many times later.
Pitfall 7: Using localStorage as the data source
The first version stored all data in the browser locally. It disappeared when switching computers; two managers editing at the same time overwrote each other.
After migrating to MySQL, localStorage kept only one purpose: remembering which month is currently being viewed.
4. Current results
- Scheduling: from 2 hours of Excel per week to one-click generation plus a little manual fine-tuning.
- Payroll: from 3 hours of manual calculation at month-end plus the boss checking each person, to automatic calculation + snapshot archiving + one-click export.
- Verification: every column in the payroll sheet can be clicked to see the breakdown and formula source, so disputes dropped.
- Audit trail: every write operation goes into an audit table; who changed what and when is traceable.
5. Open source and next steps
The project is open-sourced (MIT): the scheduling engine, payroll engine, three-stage date normalization, and dark console UI can each be referenced independently.
If it is useful, Stars and Issues are welcome.
- Repo:
github.com/iipaladin/esports-shift-payroll - Stack: Next.js 16 / React 19 / TypeScript 5 / Tailwind CSS 4 / shadcn/ui / MySQL
Next, I want to make positions, shift types, and commission rules configurable so it can adapt to more business types (restaurants, retail, factory shift teams).
Final note: for this kind of 'small but complex' business system, the difficulty is never the framework; it is pinning down real-world rules one by one. Once a rule is set wrong, no matter how beautiful the code is, it is all for nothing.
Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.
That part where auto-scheduling rewrote the entire month made me sweat—the boss thought he'd just misremembered. Brutal lesson.
Good thing I'd snapped a photo back then, otherwise my whole salary wouldn't have been enough to cover it. [crying]