Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create util function for to upsert ticket record in database #1977

Closed
7 tasks
Tracked by #1967
humansinstitute opened this issue Nov 27, 2024 · 1 comment · Fixed by #1989 or #1992
Closed
7 tasks
Tracked by #1967

Create util function for to upsert ticket record in database #1977

humansinstitute opened this issue Nov 27, 2024 · 1 comment · Fixed by #1989 or #1992
Assignees
Labels

Comments

@humansinstitute
Copy link
Contributor

humansinstitute commented Nov 27, 2024

Context

To support the ticket API endpoint's update operation, we need to implement a database utility function that can both create new tickets and update existing ones.

Task

Create a utility function that handles both insertion and updating of ticket records using GORM.
Please follow standards and conventions in line with the Bounty platform architecture.
This will be called via the route at: #1976

Outcome

A utility function that can reliably create new tickets or update existing ones based on UUID.

Design

// utils/ticket.go
package utils

import (
    "fmt"
    "errors"
    "gorm.io/gorm"
)

func UpsertTicket(ticket Ticket) (*Ticket, error) {
    var existingTicket Ticket
    
    result := db.Where("uuid = ?", ticket.UUID).First(&existingTicket)
    
    if result.Error != nil {
        if errors.Is(result.Error, gorm.ErrRecordNotFound) {
            // Create new ticket
            if err := db.Create(&ticket).Error; err != nil {
                return nil, fmt.Errorf("failed to create ticket: %w", err)
            }
            return &ticket, nil
        }
        return nil, fmt.Errorf("database error: %w", result.Error)
    }
    
    // Update existing ticket
    if err := db.Model(&existingTicket).Updates(ticket).Error; err != nil {
        return nil, fmt.Errorf("failed to update ticket: %w", err)
    }
    
    return &ticket, nil
}

Acceptance Criteria

  • Function handles both creation and updates correctly
  • CreatedAt timestamp is set for new tickets
  • UpdatedAt timestamp is updated appropriately
  • Required fields are validated before save
  • Function maintains data integrity for all ticket fields
  • Proper error handling for database operations
  • Unit tests implemented with test cases for:
    • Creating new ticket
    • Updating existing ticket
    • Invalid data scenarios
    • Database errors
@humansinstitute humansinstitute changed the title Create util function for to update ticket record in database Create util function for to upsert ticket record in database Nov 27, 2024
@MuhammadUmer44
Copy link
Contributor

MuhammadUmer44 commented Nov 27, 2024

@humansinstitute Please assign me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment