Skip to content

Commit

Permalink
fix: solve the FK constraint of "delete_group"
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielllllllllllllll committed Jun 1, 2024
1 parent d0c9ef2 commit 4e3ee6c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
27 changes: 11 additions & 16 deletions internal/services/group_bill/delete_bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,32 @@ package group_bill
import (
"context"
"database/sql"
"github.com/google/uuid"
"monify/internal/middlewares"
monify "monify/protobuf/gen/go"

"github.com/google/uuid"

"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (s Service) DeleteGroupBill(ctx context.Context, req *monify.DeleteGroupBillRequest) (*monify.GroupGroupBillEmpty, error) {
if req.BillId == "" {
return nil, status.Error(codes.InvalidArgument, "Bill id is required")
}
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger)
userId, ok := ctx.Value(middlewares.UserIdContextKey{}).(uuid.UUID)
if !ok {
return nil, status.Error(codes.Unauthenticated, "Unauthorized.")
}
billId, err := uuid.Parse(req.BillId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid bill id")
}
db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB)

//Check permission
rows := db.QueryRowContext(ctx, `
SELECT COUNT(*) FROM group_bill
LEFT JOIN group_member gm ON group_bill.group_id = gm.group_id
WHERE group_bill.bill_id = $1 AND gm.user_id = $2
`, billId, userId)
`, req.BillId, userId)
var count int
err = rows.Scan(&count)
err := rows.Scan(&count)
if err != nil {
logger.Error("Failed to check permission", zap.Error(err))
}
Expand All @@ -51,27 +45,28 @@ func (s Service) DeleteGroupBill(ctx context.Context, req *monify.DeleteGroupBil
defer tx.Rollback()

//Start delete

_, err = tx.ExecContext(ctx,
`DELETE FROM group_bill WHERE bill_id = $1`, billId,
`DELETE FROM group_split_bill WHERE bill_id = $1`, req.BillId,
)
if err != nil {
logger.Error("Failed to delete group from group_bill", zap.Error(err))
logger.Error("Failed to delete group from group_split_bill", zap.Error(err))
return nil, err
}

_, err = tx.ExecContext(ctx,
`DELETE FROM group_split_bill WHERE bill_id = $1`, billId,
`DELETE FROM group_prepaid_bill WHERE bill_id = $1`, req.BillId,
)
if err != nil {
logger.Error("Failed to delete group from group_split_bill", zap.Error(err))
logger.Error("Failed to delete group from group_prepaid_bill", zap.Error(err))
return nil, err
}

_, err = tx.ExecContext(ctx,
`DELETE FROM group_prepaid_bill WHERE bill_id = $1`, billId,
`DELETE FROM group_bill WHERE bill_id = $1`, req.BillId,
)
if err != nil {
logger.Error("Failed to delete group from group_prepaid_bill", zap.Error(err))
logger.Error("Failed to delete group from group_bill", zap.Error(err))
return nil, err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/test/group_bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package test

import (
"context"
"github.com/stretchr/testify/assert"
monify "monify/protobuf/gen/go"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateAndGetGroupBill(t *testing.T) {
Expand Down

0 comments on commit 4e3ee6c

Please sign in to comment.