Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions backend/internal/auth/dashboard.go

This file was deleted.

2 changes: 1 addition & 1 deletion backend/internal/auth/forgotPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (h *Handler) ForgotPasswordSendOTP(w http.ResponseWriter, r *http.Request)

// Fetch the user's name
var fullName string
err = h.DB.QueryRowContext(ctx, "SELECT full_name FROM users WHERE email = ?", req.Email).Scan(&fullName)
err = h.DB.QueryRowContext(ctx, "SELECT name FROM users WHERE email = ?", req.Email).Scan(&fullName)
if err != nil {
fullName = "there" // Fallback if name is not found
}
Expand Down
123 changes: 0 additions & 123 deletions backend/internal/auth/repository.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package authentication

import (
"crypto/rand"
"database/sql"
"fmt"
"log"
"math/big"
"time"
)

// isUserIDExist checks if a given user ID already exists in the database.
Expand All @@ -24,104 +20,6 @@ func (h *Handler) isUserIDExist(userID int64) (bool, error) {
return true, nil // It exists
}

// GenerateUniqueUsername creates a random unique username
// Format: user + 12 random lowercase characters/numbers
// Example: user9d8a7c2b3e4f
func (h *Handler) GenerateUniqueUsername() (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
usernamePrefix := "user"
length := 7

loc, err := time.LoadLocation("Asia/Manila")
if err != nil {
//log.Printf("GenerateUniqueUsername error loading timezone: %v", err)
//return "", err
// Fallback to UTC if timezone loading fails
loc = time.UTC
}
time.Local = loc

for {
// Get the current date in DDYY format (Go uses "010206" as reference)
userDate := time.Now().In(loc).Format("06") // e.g "06" for 2026, "27" for 2027, etc.
// time.Now().Format("1504") // e.g., "1530" for 3:30 PM
timePart := time.Now().In(loc).Format("0405") // e.g., "1530" for 3:30 PM

// Combine date and time to form part of the username
//usernamePrefix = fmt.Sprintf("user%s%s", userDate, timePart)

// Generate the random suffix
randomPart := ""
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
randomPart += string(charset[num.Int64()])
}

// Combine prefix + date + time + random part
username := fmt.Sprintf("%s%s%s%s", usernamePrefix, userDate, randomPart, timePart)

// Check DB for uniqueness
var existing string
query := "SELECT username FROM users WHERE username = ?"
err := h.DB.QueryRow(query, username).Scan(&existing)

if err == sql.ErrNoRows {
//fmt.Println("Generated unique username:", username)
return username, nil // Found a unique one!
} else if err != nil {
return "", err // Real DB Error
}

// Collision detected, loop runs again...
log.Println("Username collision! Retrying...")
}
}

// It generates the unique cardID for every card users
// Checks the database for uniqueness.
// Returns the unique card ID as string or an error if any occurs.
// Example format: CARD-XXXXXXX
func (h *Handler) GenerateCardID() (string, error) {
// Define charset: letters and numbers
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
cardIDPrefix := "CARD-"
randomLength := 7

for {
// Get the current date in MMDDYY format (Go uses "010206" as reference)
datePart := time.Now().Format("010206")

// Generate the 7 random characters
randomPart := ""
for i := 0; i < randomLength; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
randomPart += string(charset[num.Int64()])
}

// Combine them: CARD- + Date + Random
// Example output: CARD-012626Ab7z9X1
cardID := fmt.Sprintf("%s%s%s", cardIDPrefix, datePart, randomPart)

// Check database for uniqueness
var tmpCardID string
query := "SELECT card_id FROM users WHERE card_id = ?"
err := h.DB.QueryRow(query, cardID).Scan(&tmpCardID)

if err == sql.ErrNoRows {
return cardID, nil // Unique ID found
} else if err != nil {
return "", err // DB error
}
log.Printf("Collision detected! Retrying... conflicting ID: %s", cardID)
}
}

// It check the initial balance based on card number prefix
// Returns the initial balance as float64 or an error if any occurs.
// Gets the initial balance from the "card" table in the database.
Expand Down Expand Up @@ -158,24 +56,3 @@ func (h *Handler) isPhoneExist(phone string) (bool, error) {
}
return true, nil
}

// This function checks if a given full name already exists in the database.
// It executes a SQL query to search for the full name in the users table.
// If the full name is found, it returns true. If not found, it returns false.
// If an error occurs during the query, it returns the error.
func (h *Handler) isFullNameExist(fullName string) (bool, error) {
// Hold the existing full name
var existingName string

// Check query
query := "SELECT name FROM users WHERE name = ?"
err := h.DB.QueryRow(query, fullName).Scan(&existingName)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
log.Printf("Full name check error: %v", err)
return false, err
}
return true, nil
}
18 changes: 9 additions & 9 deletions backend/internal/merchant/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ func (h *Handler) MerchantAccountDataHandler(w http.ResponseWriter, r *http.Requ
err := h.DB.QueryRowContext(ctx, `
SELECT
m.merchant_id,
m.status,
DATE_FORMAT(m.created_at, '%M %d, %Y') as created_at,
COALESCE(m.status, ''),
COALESCE(DATE_FORMAT(m.created_at, '%M %d, %Y'), '') as created_at,

-- Business Info
m.business_name,
m.business_type,
COALESCE(m.business_name, ''),
COALESCE(m.business_type, ''),
COALESCE(m.business_structure, ''),
m.business_email,
m.business_phone,
m.business_address,
COALESCE(m.business_email, ''),
COALESCE(m.business_phone, ''),
COALESCE(m.business_address, ''),

-- Location Info
COALESCE(m.city, ''),
Expand All @@ -101,9 +101,9 @@ func (h *Handler) MerchantAccountDataHandler(w http.ResponseWriter, r *http.Requ

-- Document Info
COALESCE(m.business_structure, ''),
COALESCE(m.business_document),
COALESCE(m.business_document, ''),
COALESCE(m.bir_document, ''),
COALESCE(m.document_status, 'pending'),
COALESCE(m.document_status, ''),
COALESCE(m.message, '')

FROM merchants m
Expand Down
10 changes: 5 additions & 5 deletions backend/internal/merchant/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (h *Handler) GetMerchantRecentTransactions(ctx context.Context, merchantID
SELECT
transaction_id, COALESCE(card_number, ''),
merchant_id, terminal_id,
transaction_type, amount,
points_earned, service_fee,
net_merchant_payout, processed_by,
status, description, created_at
COALESCE(transaction_type, ''), amount,
COALESCE(points_earned, 0), COALESCE(service_fee, 0),
COALESCE(net_merchant_payout, 0), processed_by,
COALESCE(status, ''), description, COALESCE(created_at, '')
FROM transactions
WHERE merchant_id = ?
ORDER BY created_at DESC
Expand All @@ -101,7 +101,7 @@ func (h *Handler) GetMerchantRecentTransactions(ctx context.Context, merchantID
}
defer rows.Close()

var transactions []MerchantTransaction
transactions := []MerchantTransaction{}
for rows.Next() {
var t MerchantTransaction
if err := rows.Scan(
Expand Down
8 changes: 4 additions & 4 deletions backend/internal/merchant/incomes.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func (h *Handler) GetMerchantIncomeHistory(ctx context.Context, merchantID strin

rows, err := h.DB.QueryContext(ctx, `
SELECT
created_at, description,
COALESCE(created_at, ''), description,
transaction_id, COALESCE(card_number, ''),
transaction_type, amount,
net_merchant_payout, service_fee,
processed_by,terminal_id
COALESCE(transaction_type, ''), amount,
COALESCE(net_merchant_payout, 0), COALESCE(service_fee, 0),
processed_by, terminal_id
FROM transactions
WHERE merchant_id = ? AND status = 'completed'
ORDER BY created_at DESC LIMIT 15
Expand Down
10 changes: 5 additions & 5 deletions backend/internal/merchant/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func (h *Handler) TransactionHandler(w http.ResponseWriter, r *http.Request) {
query := `SELECT
transaction_id, COALESCE(card_number, ''),
merchant_id, terminal_id,
transaction_type, amount,
points_earned, service_fee,
net_merchant_payout, processed_by,
status, description, created_at
COALESCE(transaction_type, ''), amount,
COALESCE(points_earned, 0), COALESCE(service_fee, 0),
COALESCE(net_merchant_payout, 0), processed_by,
COALESCE(status, ''), description, COALESCE(created_at, '')
FROM transactions
WHERE merchant_id = ?`

Expand Down Expand Up @@ -100,7 +100,7 @@ func (h *Handler) TransactionHandler(w http.ResponseWriter, r *http.Request) {
}
defer rows.Close()

var transactions []MerchantTransaction
transactions := []MerchantTransaction{}
for rows.Next() {
var t MerchantTransaction
if err := rows.Scan(
Expand Down
Loading