-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package writer | ||
|
||
import ( | ||
"context" | ||
"firebase.google.com/go/v4/db" | ||
"ynufes-mypage-backend/pkg/firebase" | ||
"ynufes-mypage-backend/svc/pkg/domain/model/id" | ||
"ynufes-mypage-backend/svc/pkg/exception" | ||
entity "ynufes-mypage-backend/svc/pkg/infra/entity/relation" | ||
) | ||
|
||
type RoleStaffRelation struct { | ||
ref *db.Ref | ||
} | ||
|
||
func NewRelationRoleStaff(c *firebase.Firebase) RoleStaffRelation { | ||
return RoleStaffRelation{ | ||
ref: c.Client(entity.RelationRootName).Child(entity.RelationRoleStaffName), | ||
} | ||
} | ||
|
||
func (r RoleStaffRelation) CreateRoleStaff(ctx context.Context, roleID id.RoleID, staffID id.UserID) error { | ||
t := entity.RoleStaffRelation{ | ||
RoleID: roleID.ExportID(), | ||
UserID: staffID.ExportID(), | ||
} | ||
_, err := r.ref. | ||
Push(ctx, t) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (r RoleStaffRelation) DeleteRoleStaff(ctx context.Context, roleID id.RoleID, staffID id.UserID) error { | ||
relations, err := r.ref. | ||
OrderByChild("user_id").EqualTo(staffID.ExportID()). | ||
GetOrdered(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
var found bool | ||
for _, relation := range relations { | ||
var rEntity entity.RoleStaffRelation | ||
if err := relation.Unmarshal(&rEntity); err != nil { | ||
return err | ||
} | ||
if rEntity.RoleID != roleID.ExportID() { | ||
continue | ||
} | ||
if err := r.ref.Child(relation.Key()).Delete(ctx); err != nil { | ||
return err | ||
} | ||
found = true | ||
} | ||
if !found { | ||
return exception.ErrNotFound | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package writer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"ynufes-mypage-backend/pkg/identity" | ||
"ynufes-mypage-backend/pkg/testutil" | ||
"ynufes-mypage-backend/svc/pkg/domain/model/id" | ||
"ynufes-mypage-backend/svc/pkg/exception" | ||
) | ||
|
||
func TestRelationRoleStaff_Create(t *testing.T) { | ||
ctx := context.Background() | ||
fbt := testutil.NewFirebaseTest() | ||
defer fbt.Reset() | ||
roleID := id.RoleID(identity.IssueID()) | ||
userID := id.UserID(identity.IssueID()) | ||
fmt.Println("firebase test created") | ||
w := NewRelationRoleStaff(fbt.GetClient()) | ||
err := w.CreateRoleStaff(ctx, roleID, userID) | ||
assert.NoError(t, err) | ||
|
||
// TODO: create reader and check if the relation is created | ||
//relationR := reader.NewRelationRoleStaff(fbt.GetClient()) | ||
//orgs, err := relationR.ListOrgIDsByUserID(ctx, userID) | ||
//assert.NoError(t, err) | ||
//assert.Equal(t, []id.RoleID{roleID}, orgs) | ||
//users, err := relationR.ListUserIDsByOrgID(ctx, roleID) | ||
//assert.NoError(t, err) | ||
//assert.Equal(t, []id.UserID{userID}, users) | ||
} | ||
|
||
func TestRelationRoleStaff_Delete(t *testing.T) { | ||
type relationRoleStaff struct { | ||
UserID id.UserID | ||
RoleID id.RoleID | ||
} | ||
|
||
rSimple := relationRoleStaff{RoleID: identity.IssueID(), UserID: id.UserID(identity.IssueID())} | ||
rMultiple := relationRoleStaff{RoleID: identity.IssueID(), UserID: id.UserID(identity.IssueID())} | ||
relations := []relationRoleStaff{rSimple, rMultiple, rMultiple, rMultiple} | ||
tests := []struct { | ||
name string | ||
give relationRoleStaff | ||
wantErr error | ||
}{ | ||
{ | ||
name: "normal delete", | ||
give: rSimple, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "delete with multiple connections", | ||
give: rMultiple, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "not exist - 1", | ||
give: relationRoleStaff{RoleID: identity.IssueID(), UserID: id.UserID(identity.IssueID())}, | ||
wantErr: exception.ErrNotFound, | ||
}, | ||
{ | ||
name: "not exist - 2", | ||
give: relationRoleStaff{RoleID: relations[0].RoleID, UserID: id.UserID(identity.IssueID())}, | ||
wantErr: exception.ErrNotFound, | ||
}, | ||
{ | ||
name: "not exist - 3", | ||
give: relationRoleStaff{RoleID: identity.IssueID(), UserID: relations[0].UserID}, | ||
wantErr: exception.ErrNotFound, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fb := testutil.NewFirebaseTest() | ||
defer fb.Reset() | ||
|
||
ctx := context.Background() | ||
w := NewRelationRoleStaff(fb.GetClient()) | ||
|
||
for _, r := range relations { | ||
assert.NoError(t, w.CreateRoleStaff(ctx, r.RoleID, r.UserID)) | ||
} | ||
|
||
err := w.DeleteRoleStaff(ctx, tt.give.RoleID, tt.give.UserID) | ||
assert.ErrorIs(t, err, tt.wantErr) | ||
|
||
// TODO: create reader and check if the relation is deleted | ||
//if tt.wantErr == nil { | ||
// roles, err := reader.NewRelationRoleStaff(fb.GetClient()).ListRoleIDsByStaffID(ctx, tt.give.UserID) | ||
// assert.NoError(t, err) | ||
// assert.NotContains(t, roles, tt.give.RoleID) | ||
// | ||
// staffs, err := reader.NewRelationRoleStaff(fb.GetClient()).ListStaffIDsByRoleID(ctx, tt.give.RoleID) | ||
// assert.NoError(t, err) | ||
// assert.NotContains(t, staffs, tt.give.UserID) | ||
//} | ||
}) | ||
} | ||
} |