Skip to content

Refactor duplicated code into centralized utilities - #22

Draft
Xsidz with Copilot wants to merge 3 commits into
mainfrom
copilot/refactor-duplicated-code
Draft

Refactor duplicated code into centralized utilities#22
Xsidz with Copilot wants to merge 3 commits into
mainfrom
copilot/refactor-duplicated-code

Conversation

Copilot AI commented Dec 14, 2025

Copy link
Copy Markdown

Eliminated duplicated validation, error handling, and state management patterns across backend controllers and frontend stores.

Backend

Created utilities:

  • passwordUtils.js - Centralized password regex, validation message, hashing, and comparison
  • errorHandler.js - Standardized HTTP error responses (400/403/404/500)

Refactored: 5 controllers, 1 model

Before:

// Duplicated in auth.controller.js and user.controller.js
const passwordRegex = /^(?=.*[A-Z])(?=.*[!@#$%^&*()_+{}\[\]:;<>,.?~\\-]).{8,16}$/;
if (!passwordRegex.test(password)) {
  return res.status(400).json({ message: "Password must be 8-16..." });
}
const salt = await bcrypt.genSalt(10);
const hashed = await bcrypt.hash(password, salt);

After:

import { validatePassword, hashPassword, PASSWORD_VALIDATION_MESSAGE } from "../utils/passwordUtils.js";
if (!validatePassword(password)) {
  return handleValidationError(res, PASSWORD_VALIDATION_MESSAGE);
}
const hashed = await hashPassword(password);

Frontend

Created storeHelpers.js:

  • handleStoreError() - Extract error messages from API responses
  • applyFilters() - Generic array filtering for search/filter state

Refactored: 4 Zustand stores

Replaced 15+ instances of error.response?.data?.message || 'default message' and collapsed duplicate filter logic in userStore and storeStore into single reusable function.

Stats

  • 13 files changed: 3 new utilities, 10 refactored files
  • 128 lines of duplicated code removed
  • Fixed typos: "Inavlid" → "Invalid", "Successfuly" → "Successfully"
Original prompt

Find and refactor duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 14, 2025 08:58
Co-authored-by: Xsidz <146343711+Xsidz@users.noreply.github.com>
Co-authored-by: Xsidz <146343711+Xsidz@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code to enhance maintainability Refactor duplicated code into centralized utilities Dec 14, 2025
Copilot AI requested a review from Xsidz December 14, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants