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

PR: Add Last Withdrawal, and Total Amount of Deposits Greater OR Equals Conditions #1893

Merged
merged 24 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9cd89fc
added convert to timestamp function and test
elraphty Oct 17, 2024
9f8b6fc
tested get hours difference
elraphty Oct 17, 2024
e83226d
test time to hours
elraphty Oct 17, 2024
a300c68
added function
elraphty Oct 17, 2024
1720006
checked for sum of deposits and withdrawals
elraphty Oct 17, 2024
3cd4997
check that user has done a withdrawal
elraphty Oct 17, 2024
a29a7fd
fixed withdraw bug
elraphty Oct 18, 2024
fccbcfe
fixed hour check error
elraphty Oct 18, 2024
99887b1
added payment_history to 400 test error
elraphty Oct 18, 2024
7efdd30
sleep after 2 seconds after adding payment to histiry
elraphty Oct 18, 2024
bf8501e
push ater passing locally
elraphty Oct 18, 2024
7324154
added to see the workspace_uuid in test
elraphty Oct 18, 2024
92739a4
add ageneral late time
elraphty Oct 18, 2024
27b9e5e
add general greater time to fix
elraphty Oct 18, 2024
c9d482f
revert after testing positive time additions
elraphty Oct 18, 2024
ed38f22
revert after testing positive time additions
elraphty Oct 18, 2024
f0bf975
mocked getHoursDifference
elraphty Oct 18, 2024
fb21888
mocked getHours for withdraw bounty
elraphty Oct 18, 2024
4803f35
revert to default test config
elraphty Oct 18, 2024
f4e1988
after security checks
elraphty Oct 18, 2024
f2d729b
changed db to default
elraphty Oct 18, 2024
d26873f
removed invoice settlement check from handler
elraphty Oct 18, 2024
9770732
removed unused poll invoice code
elraphty Oct 18, 2024
3d7aa60
remove relay test for poll invoice
elraphty Oct 18, 2024
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
3 changes: 3 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ type Database interface {
ChangeWorkspaceDeleteStatus(workspace_uuid string, status bool) Workspace
UpdateWorkspaceForDeletion(uuid string) error
ProcessDeleteWorkspace(workspace_uuid string) error
GetLastWithdrawal(workspace_uuid string) NewPaymentHistory
GetSumOfDeposits(workspace_uuid string) uint
GetSumOfWithdrawal(workspace_uuid string) uint
DeleteAllUsersFromWorkspace(uuid string) error
GetFilterStatusCount() FilterStattuCount
UserHasManageBountyRoles(pubKeyFromAuth string, uuid string) bool
Expand Down
84 changes: 66 additions & 18 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (db database) GetWorkspaceBudgetHistory(workspace_uuid string) []BudgetHist
return budgetHistory
}

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

Expand All @@ -280,15 +280,25 @@ func (db database) ProcessUpdateBudget(invoice NewInvoiceList) error {
return err
}

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

invoice := NewInvoiceList{}
tx.Where("payment_request = ?", non_tx_invoice.PaymentRequest).Find(&invoice)

if invoice.Status {
tx.Rollback()
return errors.New("cannot process already paid invoice")
}

if workspace_uuid == "" {
return errors.New("cannot Create a Workspace Without a Workspace uuid")
}

// Get payment history and update budget
paymentHistory := db.GetPaymentHistoryByCreated(created, workspace_uuid)
paymentHistory := NewPaymentHistory{}
tx.Model(&NewPaymentHistory{}).Where("created = ?", created).Where("workspace_uuid = ? ", workspace_uuid).Find(&paymentHistory)

if paymentHistory.WorkspaceUuid != "" && paymentHistory.Amount != 0 {
paymentHistory.Status = true

Expand All @@ -298,9 +308,10 @@ func (db database) ProcessUpdateBudget(invoice NewInvoiceList) error {
}

// get Workspace budget and add payment to total budget
WorkspaceBudget := db.GetWorkspaceBudget(workspace_uuid)
workspaceBudget := NewBountyBudget{}
tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspace_uuid).Find(&workspaceBudget)

if WorkspaceBudget.WorkspaceUuid == "" {
if workspaceBudget.WorkspaceUuid == "" {
now := time.Now()
workBudget := NewBountyBudget{
WorkspaceUuid: workspace_uuid,
Expand All @@ -313,11 +324,11 @@ func (db database) ProcessUpdateBudget(invoice NewInvoiceList) error {
tx.Rollback()
}
} else {
totalBudget := WorkspaceBudget.TotalBudget
WorkspaceBudget.TotalBudget = totalBudget + paymentHistory.Amount
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,
if err = tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspaceBudget.WorkspaceUuid).Updates(map[string]interface{}{
"total_budget": workspaceBudget.TotalBudget,
}).Error; err != nil {
tx.Rollback()
}
Expand All @@ -333,34 +344,51 @@ func (db database) ProcessUpdateBudget(invoice NewInvoiceList) error {
}

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

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

paymentHistory := db.GetPaymentHistoryByCreated(created, workspace_uuid)
paymentHistory := NewPaymentHistory{}
tx.Model(&NewPaymentHistory{}).Where("created = ?", created).Where("workspace_uuid = ? ", workspace_uuid).Find(&paymentHistory)

if paymentHistory.WorkspaceUuid != "" && paymentHistory.Amount != 0 {
paymentHistory.Status = true
db.db.Where("created = ?", created).Where("workspace_uuid = ? ", workspace_uuid).Updates(paymentHistory)

// get Workspace budget and add payment to total budget
WorkspaceBudget := db.GetWorkspaceBudget(workspace_uuid)
workspaceBudget := NewBountyBudget{}
tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspace_uuid).Find(&workspaceBudget)

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

if err := tx.Create(&workBudget).Error; err != nil {
tx.Rollback()
}
} else {
totalBudget := WorkspaceBudget.TotalBudget
WorkspaceBudget.TotalBudget = totalBudget + paymentHistory.Amount
db.UpdateWorkspaceBudget(WorkspaceBudget)
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()
}
}
} else {
tx.Rollback()
}

tx.Commit()

return paymentHistory
}

Expand Down Expand Up @@ -487,7 +515,7 @@ func (db database) GetPaymentHistory(workspace_uuid string, r *http.Request) []N
func (db database) GetPendingPaymentHistory() []NewPaymentHistory {
paymentHistories := []NewPaymentHistory{}

query := `SELECT * FROM payment_histories WHERE payment_status = '` + PaymentPending + `' AND status = true ORDER BY created DESC`
query := `SELECT * FROM payment_histories WHERE payment_status = '` + PaymentPending + `' AND status = true AND payment_type = 'payment' ORDER BY created DESC`

db.db.Raw(query).Find(&paymentHistories)
return paymentHistories
Expand Down Expand Up @@ -612,6 +640,26 @@ func (db database) DeleteAllUsersFromWorkspace(workspace_uuid string) error {
return nil
}

func (db database) GetLastWithdrawal(workspace_uuid string) NewPaymentHistory {
p := NewPaymentHistory{}
db.db.Model(&NewPaymentHistory{}).Where("workspace_uuid", workspace_uuid).Where("payment_type", "withdraw").Order("created DESC").Limit(1).Find(&p)
return p
}

func (db database) GetSumOfDeposits(workspace_uuid string) uint {
var depositAmount uint
db.db.Model(&NewPaymentHistory{}).Where("workspace_uuid = ?", workspace_uuid).Where("status = ?", true).Where("payment_type = ?", "deposit").Select("SUM(amount)").Row().Scan(&depositAmount)

return depositAmount
}

func (db database) GetSumOfWithdrawal(workspace_uuid string) uint {
var depositAmount uint
db.db.Model(&NewPaymentHistory{}).Where("workspace_uuid = ?", workspace_uuid).Where("status = ?", true).Where("payment_type = ?", "withdraw").Select("SUM(amount)").Row().Scan(&depositAmount)

return depositAmount
}

func (db database) GetFeaturePhasesBountiesCount(bountyType string, phaseUuid string) int64 {
var count int64

Expand Down
Loading
Loading