Refactor Plan: Date Utility Extraction
1. Current State Analysis * The date utility includes formatDate, isLeapYear, and addDays, currently duplicated or inconsistently implemented across service-api (src/utils/date.ts), web-app (src/utils/date.ts), and admin-dashboard (src/helpers/date-helpers.js). Variations exist in formatDate outputs and addDays timezone handling. * Direct dependents: service-api, web-app, admin-dashboard. * Usage: formatDate is heavily used for UI and API responses; addDays for scheduling; isLeapYear for specific calculations. Inconsistencies are the primary friction.
2. Target Architecture * Proposed package: @org/date-utils in packages/date-utils. * Public API: export function formatDate(date: Date | string, format: string): string;, export function isLeapYear(year: number): boolean;, export function addDays(date: Date | string, days: number): Date;. * Versioning: Initial 1.0.0 release, subsequent SemVer managed by changesets. * Monorepo Integration: Local dependency via workspace:* in package.json files, pnpm install for linking, changesets for publishing to internal registry.
3. Step-by-Step Migration Plan * Phase 1: Package Creation and Initial Release * Create packages/date-utils boilerplate. * Migrate core logic for formatDate, isLeapYear, addDays into the new package. Write comprehensive unit tests. * Configure pnpm-workspace.yaml. Add changeset for 1.0.0. * Publish @org/date-utils@1.0.0 to internal registry. * Phase 2: Dependent Project Updates * For service-api, web-app, admin-dashboard: * Add @org/date-utils as workspace:* dependency. * Replace old utility calls with imports from the new package. * Adapt calling code for API differences. Run existing tests. * Create PRs per project. Consider staggered adoption (e.g., web-app first). * Phase 3: Cleanup and Deprecation * After successful migration and deployment, remove old utility files (src/utils/date.ts, src/helpers/date-helpers.js) from dependent projects. * Verify no lingering references. Update project documentation.
4. Test Strategy * Unit testing (`@org/date-utils`): Comprehensive Jest tests for all exported functions, covering edge cases and timezones. * Integration testing (dependents): Ensure existing integration tests for service-api, web-app, admin-dashboard pass post-migration, focusing on date-intensive flows. * End-to-end testing: Re-verify critical user flows in web-app and admin-dashboard manually or via automated E2E tests. * Performance testing: Basic checks if date utilities are performance-critical.
5. Rollback Plan * Phase 1 Rollback: Delete packages/date-utils, revert pnpm-workspace.yaml and changeset. * Phase 2 Rollback: Revert specific dependent project PR, remove @org/date-utils dependency, revert code changes. Deprecate problematic package version if published. * Communication: Immediate notification to relevant teams (dev, QA, product) via Slack/email detailing issue and rollback status.