Skip to content

Commit

Permalink
Merge pull request #68 from shion1305/issue/67
Browse files Browse the repository at this point in the history
Agent -> Role Staffへの移行, 対応するテーブルとWriterの作成
  • Loading branch information
Shion1305 authored Feb 26, 2024
2 parents ff403f5 + 46d459b commit 4e5c4af
Show file tree
Hide file tree
Showing 33 changed files with 561 additions and 345 deletions.
6 changes: 6 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"user_id",
"org_id"
]
},
"RoleStaff": {
".indexOn": [
"user_id",
"role_id"
]
}
},
"Forms": {
Expand Down
2 changes: 0 additions & 2 deletions svc/pkg/domain/command/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ type User interface {
Create(context.Context, *user.User) error
Set(context.Context, user.User) error
UpdateUserDetail(context.Context, id.UserID, user.Detail) error
SetAgent(context.Context, id.UserID, user.Agent) error
SetAdmin(context.Context, id.UserID, user.Admin) error
Delete(context.Context, user.User) error
}
5 changes: 2 additions & 3 deletions svc/pkg/domain/model/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"sort"
"time"
"ynufes-mypage-backend/svc/pkg/domain/model/id"
"ynufes-mypage-backend/svc/pkg/domain/model/user"
)

type (
Expand All @@ -14,7 +13,7 @@ type (
Title string
Summary string
Description string
Roles []user.RoleID
Roles []id.RoleID
Deadline time.Time
IsOpen bool
Sections SectionsOrder
Expand All @@ -27,7 +26,7 @@ func NewForm(
eventID id.EventID,
title, summary, description string,
sectionOrders map[id.SectionID]float64,
roles []user.RoleID,
roles []id.RoleID,
deadline time.Time,
isOpen bool,
) *Form {
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/domain/model/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type (
SectionID util.ID
QuestionID util.ID
OrgIDs []OrgID
RoleID util.ID
)

func (i OrgIDs) HasOrgID(oid OrgID) bool {
Expand Down
8 changes: 8 additions & 0 deletions svc/pkg/domain/model/role/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package role

import "ynufes-mypage-backend/svc/pkg/domain/model/id"

type Role struct {
ID id.RoleID
Name string
}
8 changes: 8 additions & 0 deletions svc/pkg/domain/model/staff/staff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package staff

import "ynufes-mypage-backend/svc/pkg/domain/model/id"

type Staff struct {
id.UserID
IsAdmin bool
}
13 changes: 0 additions & 13 deletions svc/pkg/domain/model/user/admin.go

This file was deleted.

39 changes: 0 additions & 39 deletions svc/pkg/domain/model/user/agent.go

This file was deleted.

2 changes: 0 additions & 2 deletions svc/pkg/domain/model/user/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ type (
User struct {
ID id.UserID
Detail Detail
Admin Admin
Agent Agent
}
)

Expand Down
5 changes: 2 additions & 3 deletions svc/pkg/domain/model/user/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package user

import "errors"

// Type represents the type of user.
// This field is currently not used, but it is expected to be used in the future.
type Type int

const (
TypeNormal Type = 1
TypeMember Type = 2
)

func NewType(t int) (Type, error) {
switch Type(t) {
case TypeNormal:
return TypeNormal, nil
case TypeMember:
return TypeMember, nil
default:
return -1, errors.New("USER TYPE VALUE IS INVALID")
}
Expand Down
3 changes: 1 addition & 2 deletions svc/pkg/infra/entity/form/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"ynufes-mypage-backend/pkg/identity"
"ynufes-mypage-backend/svc/pkg/domain/model/form"
"ynufes-mypage-backend/svc/pkg/domain/model/id"
"ynufes-mypage-backend/svc/pkg/domain/model/user"
"ynufes-mypage-backend/svc/pkg/exception"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ func (f Form) ToModel() (*form.Form, error) {
return nil, err
}

roles := make([]user.RoleID, 0, len(f.Roles))
roles := make([]id.RoleID, 0, len(f.Roles))
for k, v := range f.Roles {
if !v {
continue
Expand Down
2 changes: 1 addition & 1 deletion svc/pkg/infra/entity/relation/org_user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package relation
package entity

const (
RelationRootName = "Relation"
Expand Down
8 changes: 8 additions & 0 deletions svc/pkg/infra/entity/relation/role_staff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entity

const RelationRoleStaffName = "RoleStaff"

type RoleStaffRelation struct {
RoleID string `json:"role_id"`
UserID string `json:"user_id"`
}
8 changes: 8 additions & 0 deletions svc/pkg/infra/entity/role/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entity

const RoleTableName = "Roles"

type Role struct {
ID string `json:"-"`
Name string `json:"name"`
}
8 changes: 8 additions & 0 deletions svc/pkg/infra/entity/staff/staff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entity

const StaffTableName = "Staffs"

type Staff struct {
UserID string `json:"-"`
IsAdmin bool `json:"is_admin"`
}
8 changes: 0 additions & 8 deletions svc/pkg/infra/entity/user/admin.go

This file was deleted.

12 changes: 0 additions & 12 deletions svc/pkg/infra/entity/user/agent.go

This file was deleted.

28 changes: 0 additions & 28 deletions svc/pkg/infra/entity/user/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package entity

import (
"time"
"ynufes-mypage-backend/pkg/identity"
"ynufes-mypage-backend/svc/pkg/domain/model/id"
"ynufes-mypage-backend/svc/pkg/domain/model/user"
)
Expand All @@ -12,8 +10,6 @@ const UserRootName = "Users"
type User struct {
ID id.UserID `json:"-"`
UserDetail `json:"detail"`
Admin `json:"admin"`
Agent `json:"agent"`
}

func (u User) ToModel() (*user.User, error) {
Expand All @@ -25,23 +21,6 @@ func (u User) ToModel() (*user.User, error) {
if err != nil {
return nil, err
}
roles := make([]user.Role, len(u.Roles))
for i, role := range u.Roles {
lv, err := user.NewRoleLevel(role.Level)
if err != nil {
return nil, err
}
roles[i] = user.Role{
ID: identity.NewID(role.ID),
Level: lv,
GrantedTime: time.UnixMilli(role.GrantedTime),
}
}
var adminGrantedTime *time.Time
if u.IsSuperAdmin {
t := time.UnixMilli(u.GrantedTime)
adminGrantedTime = &t
}
ty, err := user.NewType(u.UserDetail.Type)
if err != nil {
return nil, err
Expand All @@ -62,12 +41,5 @@ func (u User) ToModel() (*user.User, error) {
Type: ty,
PictureURL: user.PictureURL(u.PictureURL),
},
Admin: user.Admin{
IsSuperAdmin: u.IsSuperAdmin,
GrantedTime: adminGrantedTime,
},
Agent: user.Agent{
Roles: roles,
},
}, nil
}
29 changes: 14 additions & 15 deletions svc/pkg/infra/reader/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"ynufes-mypage-backend/svc/pkg/domain/model/event"
"ynufes-mypage-backend/svc/pkg/domain/model/form"
"ynufes-mypage-backend/svc/pkg/domain/model/id"
"ynufes-mypage-backend/svc/pkg/domain/model/user"
"ynufes-mypage-backend/svc/pkg/infra/writer"
)

Expand All @@ -33,9 +32,9 @@ func TestForm_GetByID(t *testing.T) {
Title: "FormTitle1",
Summary: "FormSummary1",
Description: "FormDescription1",
Roles: []user.RoleID{
user.RoleID(identity.IssueID()),
user.RoleID(identity.IssueID()),
Roles: []id.RoleID{
id.RoleID(identity.IssueID()),
id.RoleID(identity.IssueID()),
},
Deadline: time.UnixMilli(deadline),
IsOpen: false,
Expand Down Expand Up @@ -82,9 +81,9 @@ func TestForm_ListByEventID(t *testing.T) {
Title: "FormTitle1",
Summary: "FormSummary1",
Description: "FormDescription1",
Roles: []user.RoleID{
user.RoleID(identity.IssueID()),
user.RoleID(identity.IssueID()),
Roles: []id.RoleID{
id.RoleID(identity.IssueID()),
id.RoleID(identity.IssueID()),
},
Deadline: time.UnixMilli(deadline1),
IsOpen: false,
Expand All @@ -102,9 +101,9 @@ func TestForm_ListByEventID(t *testing.T) {
Title: "FormTitle2",
Summary: "FormSummary2",
Description: "FormDescription2",
Roles: []user.RoleID{
user.RoleID(identity.IssueID()),
user.RoleID(identity.IssueID()),
Roles: []id.RoleID{
id.RoleID(identity.IssueID()),
id.RoleID(identity.IssueID()),
},
Deadline: time.UnixMilli(deadline2),
IsOpen: false,
Expand All @@ -122,9 +121,9 @@ func TestForm_ListByEventID(t *testing.T) {
Title: "FormTitle3",
Summary: "FormSummary3",
Description: "FormDescription3",
Roles: []user.RoleID{
user.RoleID(identity.IssueID()),
user.RoleID(identity.IssueID()),
Roles: []id.RoleID{
id.RoleID(identity.IssueID()),
id.RoleID(identity.IssueID()),
},
Deadline: time.UnixMilli(deadline3),
IsOpen: false,
Expand Down Expand Up @@ -174,9 +173,9 @@ func checkFormEqual(t *testing.T, f1, f2 form.Form) {
}
}

func checkRolesEqual(t *testing.T, r1, r2 []user.RoleID) {
func checkRolesEqual(t *testing.T, r1, r2 []id.RoleID) {
assert.Equal(t, len(r1), len(r2))
roles := make(map[user.RoleID]struct{}, len(r1))
roles := make(map[id.RoleID]struct{}, len(r1))
for _, v := range r1 {
roles[v] = struct{}{}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
entity "ynufes-mypage-backend/svc/pkg/infra/entity/relation"
)

type Relation struct {
type RelationOrgUser struct {
orgUserRef *db.Ref
}

func NewRelation(db *firebase.Firebase) Relation {
return Relation{
func NewRelationOrgUser(db *firebase.Firebase) RelationOrgUser {
return RelationOrgUser{
orgUserRef: db.Client(entity.RelationRootName).Child(entity.RelationOrgUserName),
}
}

func (r Relation) ListUserIDsByOrgID(ctx context.Context, orgID id.OrgID) ([]id.UserID, error) {
func (r RelationOrgUser) ListUserIDsByOrgID(ctx context.Context, orgID id.OrgID) ([]id.UserID, error) {
qs, err := r.orgUserRef.OrderByChild("org_id").
EqualTo(orgID.ExportID()).
GetOrdered(ctx)
Expand All @@ -42,7 +42,7 @@ func (r Relation) ListUserIDsByOrgID(ctx context.Context, orgID id.OrgID) ([]id.
return userIDs, nil
}

func (r Relation) ListOrgIDsByUserID(ctx context.Context, userID id.UserID) (id.OrgIDs, error) {
func (r RelationOrgUser) ListOrgIDsByUserID(ctx context.Context, userID id.UserID) (id.OrgIDs, error) {
qs, err := r.orgUserRef.OrderByChild("user_id").
EqualTo(userID.ExportID()).
GetOrdered(ctx)
Expand Down
Loading

0 comments on commit 4e5c4af

Please sign in to comment.