Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
SpeedReach committed Jun 5, 2024
1 parent 8d22aa8 commit 0ca5567
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 12 deletions.
5 changes: 3 additions & 2 deletions internal/services/group_bill/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s Service) CreateGroupBill(ctx context.Context, req *monify.CreateGroupBil
defer tx.Rollback()
//Insert
billId := uuid.New()
if err = insertBill(ctx, tx, logger, insertBillInfo{
if err = insertBill(ctx, tx, insertBillInfo{
billId: billId,
groupId: groupId,
createdBy: memberId,
Expand Down Expand Up @@ -97,7 +97,8 @@ type insertBillInfo struct {
prepaidPeople []*monify.InsertPrepaidPerson
}

func insertBill(ctx context.Context, tx *sql.Tx, logger *zap.Logger, info insertBillInfo) error {
func insertBill(ctx context.Context, tx *sql.Tx, info insertBillInfo) error {
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger)
_, err := tx.ExecContext(ctx, `
INSERT INTO group_bill (bill_id, group_id, created_by, total_money, title, description)
VALUES ($1, $2, $3, $4, $5, $6)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/group_bill/modify_bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s Service) ModifyGroupBill(ctx context.Context, req *monify.ModifyGroupBil
}

//Insert
if err = insertBill(ctx, tx, logger, insertBillInfo{
if err = insertBill(ctx, tx, insertBillInfo{
billId: billId,
groupId: groupId,
createdBy: createdBy,
Expand Down
13 changes: 10 additions & 3 deletions internal/services/group_bill/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"github.com/google/uuid"
"go.uber.org/zap"
"monify/internal/middlewares"
monify "monify/protobuf/gen/go"
)
Expand All @@ -28,19 +29,25 @@ type billHistoryInsertion struct {
}

func insertBillHistory(ctx context.Context, db *sql.Tx, history billHistoryInsertion) error {
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger)
row := db.QueryRowContext(ctx, `
SELECT group_id FROM group_member WHERE group_member_id = $1
`, history.operator)

var groupId uuid.UUID
if err := row.Scan(&groupId); err != nil {
logger.Error("failed to get group_id", zap.Error(err))
return err
}

_, err := db.ExecContext(ctx, `
if _, err := db.ExecContext(ctx, `
INSERT INTO group_bill_history( history_id, type, bill_id, title, operator, group_id) VALUES ($1, $2, $3, $4, $5, $6)
`, uuid.New(), history.ty, history.billId, history.title, history.operator, groupId)
return err
`, uuid.New(), history.ty, history.billId, history.title, history.operator, groupId); err != nil {
logger.Error("failed to insert bill history", zap.Error(err))
return err
}

return nil
}

// context requires middlewares.DatabaseContextKey:*sql.DB
Expand Down
1 change: 0 additions & 1 deletion internal/test/group_bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,4 @@ func TestCreateAndGetGroupBill(t *testing.T) {
assert.NoError(t, err)
response2, err = client.GetGroupBills(context.TODO(), &monify.GetGroupBillsRequest{GroupId: group.GroupId})
assert.Empty(t, response2.GroupBills)

}
2 changes: 2 additions & 0 deletions migrations/2405310815_create_friend_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE friend_bill;
DROP TABLE friend;
5 changes: 2 additions & 3 deletions migrations/2405310815_create_friend_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ CREATE TABLE friend(

CREATE TABLE friend_bill(
friend_bill_id uuid PRIMARY KEY,
relation_id uuid NOT NULL ,
relation_id uuid NOT NULL REFERENCES friend(relation_id),
amount double precision NOT NULL,
title varchar(50) NOT NULL ,
description varchar(100) NOT NULL default '',
created_at timestamp NOT NULL default CURRENT_TIMESTAMP,
FOREIGN KEY (relation_id) REFERENCES friend(relation_id)
created_at timestamp NOT NULL default CURRENT_TIMESTAMP
)

2 changes: 1 addition & 1 deletion migrations/2406010512_create_bill_history_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ INSERT INTO group_bill_history_type (name, type) VALUES ('modify', 2);
CREATE TABLE group_bill_history(
history_id uuid PRIMARY KEY,
type int REFERENCES group_bill_history_type(type),
bill_id uuid UNIQUE,
bill_id uuid,
group_id uuid REFERENCES "group"(group_id),
title varchar(30) NOT NULL,
operator uuid REFERENCES group_member(group_member_id),
Expand Down
2 changes: 1 addition & 1 deletion migrations/2406030027_create_friend_invite_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ CREATE TABLE friend_invite (
sender uuid NOT NULL,
receiver uuid NOT NULL,
created_at timestamp default CURRENT_TIMESTAMP,
FOREIGN KEY (sender) REFERENCES "user_identity"(user_id)
FOREIGN KEY (sender) REFERENCES "user_identity"(user_id),
FOREIGN KEY (receiver) REFERENCES "user_identity"(user_id)
);

0 comments on commit 0ca5567

Please sign in to comment.