Skip to content

Commit

Permalink
Merge pull request #1684 from stakwork/feat/db_transactions
Browse files Browse the repository at this point in the history
Change Business Logic DB Calls To DB Transactions
  • Loading branch information
elraphty authored Jun 10, 2024
2 parents 2395b7f + e6ab5cb commit c59691e
Show file tree
Hide file tree
Showing 10 changed files with 550 additions and 112 deletions.
50 changes: 50 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,56 @@ func (db database) AddUserInvoiceData(userData UserInvoiceData) UserInvoiceData
return userData
}

func (db database) ProcessAddInvoice(invoice NewInvoiceList, userData UserInvoiceData) error {
tx := db.db.Begin()
var err error

defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err = tx.Error; err != nil {
return err
}

if err = tx.Create(&invoice).Error; err != nil {
tx.Rollback()
}

if err = tx.Create(&userData).Error; err != nil {
tx.Rollback()
}

return tx.Commit().Error
}

func (db database) ProcessBudgetInvoice(paymentHistory NewPaymentHistory, newInvoice NewInvoiceList) error {
tx := db.db.Begin()
var err error

defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err = tx.Error; err != nil {
return err
}

if err = tx.Create(&paymentHistory).Error; err != nil {
tx.Rollback()
}

if err = tx.Create(&newInvoice).Error; err != nil {
tx.Rollback()
}

return tx.Commit().Error
}

func (db database) GetUserInvoiceData(payment_request string) UserInvoiceData {
ms := UserInvoiceData{}
db.db.Where("payment_request = ?", payment_request).Find(&ms)
Expand Down
5 changes: 5 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ type Database interface {
GetWorkspaceBudget(workspace_uuid string) NewBountyBudget
GetWorkspaceStatusBudget(workspace_uuid string) StatusBudget
GetWorkspaceBudgetHistory(workspace_uuid string) []BudgetHistoryData
ProcessUpdateBudget(invoice NewInvoiceList) error
AddAndUpdateBudget(invoice NewInvoiceList) NewPaymentHistory
WithdrawBudget(sender_pubkey string, workspace_uuid string, amount uint)
AddPaymentHistory(payment NewPaymentHistory) NewPaymentHistory
ProcessBountyPayment(payment NewPaymentHistory, bounty NewBounty) error
GetPaymentHistory(workspace_uuid string, r *http.Request) []NewPaymentHistory
GetInvoice(payment_request string) NewInvoiceList
GetWorkspaceInvoices(workspace_uuid string) []NewInvoiceList
Expand All @@ -116,10 +118,13 @@ type Database interface {
AddInvoice(invoice NewInvoiceList) NewInvoiceList
DeleteInvoice(payment_request string) NewInvoiceList
AddUserInvoiceData(userData UserInvoiceData) UserInvoiceData
ProcessAddInvoice(invoice NewInvoiceList, userData UserInvoiceData) error
ProcessBudgetInvoice(paymentHistory NewPaymentHistory, newInvoice NewInvoiceList) error
GetUserInvoiceData(payment_request string) UserInvoiceData
DeleteUserInvoiceData(payment_request string) UserInvoiceData
ChangeWorkspaceDeleteStatus(workspace_uuid string, status bool) Workspace
UpdateWorkspaceForDeletion(uuid string) error
ProcessDeleteWorkspace(workspace_uuid string) error
DeleteAllUsersFromWorkspace(uuid string) error
GetFilterStatusCount() FilterStattuCount
UserHasManageBountyRoles(pubKeyFromAuth string, uuid string) bool
Expand Down
185 changes: 178 additions & 7 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func (db database) CreateWorkspaceUser(orgUser WorkspaceUsers) WorkspaceUsers {
return orgUser
}

func (db database) DeleteWorkspaceUser(orgUser WorkspaceUsersData, org string) WorkspaceUsersData {
db.db.Where("owner_pub_key = ?", orgUser.OwnerPubKey).Where("workspace_uuid = ?", org).Delete(&WorkspaceUsers{})
db.db.Where("owner_pub_key = ?", orgUser.OwnerPubKey).Where("workspace_uuid = ?", org).Delete(&UserRoles{})
func (db database) DeleteWorkspaceUser(orgUser WorkspaceUsersData, workspace_uuid string) WorkspaceUsersData {
db.db.Where("owner_pub_key = ?", orgUser.OwnerPubKey).Where("workspace_uuid = ?", workspace_uuid).Delete(&WorkspaceUsers{})
db.db.Where("owner_pub_key = ?", orgUser.OwnerPubKey).Where("workspace_uuid = ?", workspace_uuid).Delete(&UserRoles{})
return orgUser
}

Expand Down Expand Up @@ -251,6 +251,70 @@ func (db database) GetWorkspaceBudgetHistory(workspace_uuid string) []BudgetHist
return budgetHistory
}

func (db database) ProcessUpdateBudget(invoice NewInvoiceList) error {
// Start db transaction
tx := db.db.Begin()

var err error

defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err = tx.Error; err != nil {
return err
}

created := invoice.Created
workspace_uuid := invoice.WorkspaceUuid

// Get payment history and update budget
paymentHistory := db.GetPaymentHistoryByCreated(created, workspace_uuid)
if paymentHistory.WorkspaceUuid != "" && paymentHistory.Amount != 0 {
paymentHistory.Status = true

// Update payment history
if err = tx.Where("created = ?", created).Where("workspace_uuid = ? ", workspace_uuid).Updates(paymentHistory).Error; err != nil {
tx.Rollback()
}

// get Workspace budget and add payment to total budget
WorkspaceBudget := db.GetWorkspaceBudget(workspace_uuid)

if WorkspaceBudget.WorkspaceUuid == "" {
now := time.Now()
workBudget := NewBountyBudget{
WorkspaceUuid: workspace_uuid,
TotalBudget: paymentHistory.Amount,
Created: &now,
Updated: &now,
}

if err = tx.Create(&workBudget).Error; err != nil {
tx.Rollback()
}
} else {
totalBudget := WorkspaceBudget.TotalBudget
WorkspaceBudget.TotalBudget = totalBudget + paymentHistory.Amount

if err = tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", WorkspaceBudget.WorkspaceUuid).Updates(map[string]interface{}{
"total_budget": WorkspaceBudget.TotalBudget,
}).Error; err != nil {
tx.Rollback()
}
}

// update invoice
if err = tx.Model(&NewInvoiceList{}).Where("payment_request = ?", invoice.PaymentRequest).Update("status", true).Error; err != nil {
tx.Rollback()
}
}

return tx.Commit().Error
}

func (db database) AddAndUpdateBudget(invoice NewInvoiceList) NewPaymentHistory {
created := invoice.Created
workspace_uuid := invoice.WorkspaceUuid
Expand Down Expand Up @@ -284,17 +348,32 @@ func (db database) AddAndUpdateBudget(invoice NewInvoiceList) NewPaymentHistory
}

func (db database) WithdrawBudget(sender_pubkey string, workspace_uuid string, amount uint) {
tx := db.db.Begin()
var err error

defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err = tx.Error; err != nil {
return
}

// get Workspace budget and add payment to total budget
WorkspaceBudget := db.GetWorkspaceBudget(workspace_uuid)
totalBudget := WorkspaceBudget.TotalBudget

newBudget := totalBudget - amount
db.db.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspace_uuid).Updates(map[string]interface{}{

if err = tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspace_uuid).Updates(map[string]interface{}{
"total_budget": newBudget,
})
}).Error; err != nil {
tx.Rollback()
}

now := time.Now()

budgetHistory := NewPaymentHistory{
WorkspaceUuid: workspace_uuid,
Amount: amount,
Expand All @@ -306,7 +385,11 @@ func (db database) WithdrawBudget(sender_pubkey string, workspace_uuid string, a
ReceiverPubKey: "",
BountyId: 0,
}
db.AddPaymentHistory(budgetHistory)

if err = tx.Create(&budgetHistory).Error; err != nil {
tx.Rollback()
}
tx.Commit()
}

func (db database) AddPaymentHistory(payment NewPaymentHistory) NewPaymentHistory {
Expand All @@ -326,6 +409,48 @@ func (db database) AddPaymentHistory(payment NewPaymentHistory) NewPaymentHistor
return payment
}

func (db database) ProcessBountyPayment(payment NewPaymentHistory, bounty NewBounty) error {
tx := db.db.Begin()
var err error

defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err = tx.Error; err != nil {
return err
}

// add to payment history
if err = tx.Create(&payment).Error; err != nil {
tx.Rollback()
return err
}

// get Workspace budget and subtract payment from total budget
WorkspaceBudget := db.GetWorkspaceBudget(payment.WorkspaceUuid)
totalBudget := WorkspaceBudget.TotalBudget

// update budget
WorkspaceBudget.TotalBudget = totalBudget - payment.Amount
if err = tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", payment.WorkspaceUuid).Updates(map[string]interface{}{
"total_budget": WorkspaceBudget.TotalBudget,
}).Error; err != nil {
tx.Rollback()
return err
}

// updatge bounty status
if err = tx.Where("created", bounty.Created).Updates(&bounty).Error; err != nil {
tx.Rollback()
return err
}

return tx.Commit().Error
}

func (db database) GetPaymentHistory(workspace_uuid string, r *http.Request) []NewPaymentHistory {
payment := []NewPaymentHistory{}

Expand Down Expand Up @@ -378,6 +503,52 @@ func (db database) UpdateWorkspaceForDeletion(uuid string) error {
return nil
}

func (db database) ProcessDeleteWorkspace(workspace_uuid string) error {
tx := db.db.Begin()
var err error

defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err = tx.Error; err != nil {
return err
}

updates := map[string]interface{}{
"website": "",
"github": "",
"description": "",
"show": false,
}

// Update workspace
if err = tx.Model(&Workspace{}).Where("uuid = ?", workspace_uuid).Updates(updates).Error; err != nil {
tx.Rollback()
}

// Delete all users associated with the Workspace
if err = tx.Where("workspace_uuid = ?", workspace_uuid).Delete(&WorkspaceUsers{}).Error; err != nil {
tx.Rollback()
}

// Delete all user roles associated with the Workspace
if err = tx.Where("workspace_uuid = ?", workspace_uuid).Delete(&WorkspaceUserRoles{}).Error; err != nil {
tx.Rollback()
}

// Change delete status to true
if err = tx.Model(&Workspace{}).Where("uuid", workspace_uuid).Updates(map[string]interface{}{
"deleted": true,
}).Error; err != nil {
tx.Rollback()
}

return tx.Commit().Error
}

func (db database) DeleteAllUsersFromWorkspace(workspace_uuid string) error {
if workspace_uuid == "" {
return errors.New("no workspoace uuid provided")
Expand Down
Loading

0 comments on commit c59691e

Please sign in to comment.