Skip to content

Refactor: replace hardcoded role strings with a typed Role constant #71

@ayush00git

Description

@ayush00git

Role strings like \"faculty\", \"warden\", \"centrehead\", and \"admin\" are scattered as raw literals across multiple handler files. This makes them easy to mistype (already inconsistent — centrehead vs centre_head in different places) and impossible for the compiler to catch.

Affected locations

handlers/faculty_auth.go

  • SendVerificationMail(..., "faculty") — lines 68, 86
  • helpers.GenerateToken(..., "faculty") — line 129
  • c.JSON(200, gin.H{..., "role": "faculty"}) — lines 145, 221
  • services.SendPasswordResetMail(..., "faculty") — line 172

handlers/warden_auth.go

  • SendVerificationMail(..., "warden") — lines 48, 65
  • helpers.GenerateToken(..., "warden") — line 106
  • c.JSON(200, gin.H{..., "role": "warden"}) — lines 122, 199
  • services.SendPasswordResetMail(..., "warden") — line 150

handlers/centrehead_auth.go

  • SendVerificationMail(..., "centrehead") — lines 48, 65
  • helpers.GenerateToken(..., "centrehead") — line 106
  • c.JSON(200, gin.H{..., "role": "centrehead"}) — lines 122, 198
  • services.SendPasswordResetMail(..., "centrehead") — line 149

handlers/admin_auth.go

  • helpers.GenerateToken(..., "admin") — line 69

handlers/auth.go (VerifyAccount / UserProfile switch-cases)

  • case "faculty", case "warden", case "admin" — lines 39–43, 67–75

handlers/admin_post.go (AdminGetPost switch-cases)

  • case "faculty", case "warden" — lines 269, 285

Fix

Add role constants to models/ (or a shared types package):

const (
    RoleFaculty    = "faculty"
    RoleWarden     = "warden"
    RoleCentreHead = "centrehead"
    RoleAdmin      = "admin"
)

Then replace every raw string literal above with the corresponding constant. The compiler will catch any future typo, and a rename only needs to happen in one place.

Note: centrehead_auth.go already uses "centrehead" while auth.go routes use "centre_head" in the URL path — consolidate on one canonical spelling when making this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions