Skip to content

Commit 4ba5d43

Browse files
committed
feat: toggle scoreboard support
1 parent 24cce55 commit 4ba5d43

8 files changed

Lines changed: 64 additions & 22 deletions

File tree

domain/workspace.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import (
66
)
77

88
type RawWorkspace struct {
9-
Id int `json:"id" db:"id"`
10-
Name string `json:"name" db:"name"`
11-
ProfileUrl string `json:"profileUrl" db:"profile_url"`
12-
CreatedAt time.Time `json:"createdAt" db:"created_at"`
13-
OwnerName string `json:"ownerName" db:"owner_name"`
14-
OwnerProfileUrl string `json:"ownerProfileUrl" db:"owner_profile_url"`
15-
ParticipantCount int `json:"participantCount" db:"participant_count"`
16-
TotalAssignment int `json:"totalAssignment" db:"total_assignment"`
17-
IsArchived bool `json:"isArchived" db:"is_archived"`
18-
IsOpenScoreboard bool `json:"-" db:"is_open_scoreboard"`
19-
IsDeleted bool `json:"-" db:"is_deleted"`
9+
Id int `json:"id" db:"id"`
10+
Name string `json:"name" db:"name"`
11+
ProfileUrl string `json:"profileUrl" db:"profile_url"`
12+
CreatedAt time.Time `json:"createdAt" db:"created_at"`
13+
OwnerName string `json:"ownerName" db:"owner_name"`
14+
OwnerProfileUrl string `json:"ownerProfileUrl" db:"owner_profile_url"`
15+
ParticipantCount int `json:"participantCount" db:"participant_count"`
16+
TotalAssignment int `json:"totalAssignment" db:"total_assignment"`
17+
IsArchived bool `json:"isArchived" db:"is_archived"`
18+
IsScoreboardEnabled bool `json:"isScoreboardEnabled" db:"is_scoreboard_enabled"`
19+
IsOpenScoreboard bool `json:"-" db:"is_open_scoreboard"`
20+
IsDeleted bool `json:"-" db:"is_deleted"`
2021
}
2122

2223
type Workspace struct {
@@ -35,9 +36,10 @@ type CreateWorkspace struct {
3536
}
3637

3738
type UpdateWorkspace struct {
38-
Name *string
39-
Profile io.Reader
40-
Archive *bool
39+
Name *string
40+
Profile io.Reader
41+
Archive *bool
42+
IsScoreboardEnabled *bool
4143
}
4244

4345
type UpdateParticipant struct {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE `workspace`
2+
DROP `is_scoreboard_enabled`;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE `workspace`
2+
ADD `is_scoreboard_enabled` TINYINT(1) NOT NULL DEFAULT '0' AFTER `profile_url`;

platform/server/controller/workspace.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ func (c *WorkspaceController) Update(ctx *fiber.Ctx) error {
252252
user.Id,
253253
pl.WorkspaceId,
254254
&domain.UpdateWorkspace{
255-
Name: pl.Name,
256-
Profile: pl.Profile,
257-
Archive: pl.Archive,
255+
Name: pl.Name,
256+
Profile: pl.Profile,
257+
Archive: pl.Archive,
258+
IsScoreboardEnabled: pl.IsScoreboardEnabled,
258259
},
259260
); err != nil {
260261
return err

platform/server/middleware/scoreboard.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ func NewScoreboardMiddleware(
3434
return errs.New(errs.SameCode, "workspace id %d not found", pl.WorkspaceId)
3535
}
3636

37+
if !workspace.IsScoreboardEnabled {
38+
// Bypass for workspace owner and admin
39+
sid, _ := validator.ValidateAuth(ctx)
40+
if sid != "" {
41+
user, err := authUsecase.Authenticate(sid)
42+
if err != nil {
43+
return errs.New(errs.SameCode, "cannot get user to get scoreboard", err)
44+
}
45+
role, err := workspaceUsecase.GetRole(user.Id, workspace.Id)
46+
if err != nil {
47+
return errs.New(errs.SameCode, "cannot get user role to get scoreboard", err)
48+
}
49+
50+
if *role == domain.OwnerRole || *role == domain.AdminRole {
51+
return ctx.Next()
52+
}
53+
}
54+
55+
return errs.New(errs.ErrGetScoreboard, "scoreboard is disabled")
56+
}
57+
3758
if !workspace.IsOpenScoreboard {
3859
sid, err := validator.ValidateAuth(ctx)
3960
if sid == "" {

platform/server/payload/workspace.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ type CreateWorkspacePayload struct {
2626

2727
type UpdateWorkspacePayload struct {
2828
WorkspacePath
29-
Name *string `json:"name"`
30-
Favorite *bool `json:"favorite"`
31-
Archive *bool `json:"archive"`
32-
Profile multipart.File `file:"profile"`
29+
Name *string `json:"name"`
30+
Favorite *bool `json:"favorite"`
31+
Archive *bool `json:"archive"`
32+
Profile multipart.File `file:"profile"`
33+
IsScoreboardEnabled *bool `json:"isScoreboardEnabled"`
3334
}
3435

3536
type CreateInvitationPayload struct {

repository/workspace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ func (r *workspaceRepository) Update(userId string, workspace *domain.Workspace)
304304
UPDATE workspace SET
305305
name = :name,
306306
profile_url = :profile_url,
307-
is_archived = :is_archived
307+
is_archived = :is_archived,
308+
is_scoreboard_enabled = :is_scoreboard_enabled
308309
WHERE id = :id;
309310
`, workspace.RawWorkspace)
310311
if err != nil {

usecase/workspace.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ func (u *workspaceUsecase) Update(userId string, workspaceId int, uw *domain.Upd
327327
workspace.IsArchived = *uw.Archive
328328
}
329329

330+
if uw.IsScoreboardEnabled != nil {
331+
isAuthorized, err = u.CheckPermRole(userId, workspaceId, []domain.WorkspaceRole{domain.OwnerRole, domain.AdminRole})
332+
if err != nil {
333+
return errs.New(errs.SameCode, "cannot get workspace role while updating workspace scoreboard", err)
334+
}
335+
336+
if !isAuthorized {
337+
return errs.New(errs.ErrWorkspaceNoPerm, "permission denied")
338+
}
339+
workspace.IsScoreboardEnabled = *uw.IsScoreboardEnabled
340+
}
341+
330342
if err := u.workspaceRepository.Update(userId, workspace); err != nil {
331343
return errs.New(errs.ErrUpdateWorkspace, "cannot update workspace id %d", workspaceId, err)
332344
}

0 commit comments

Comments
 (0)