From 952ceab014beb7522f9549d226b4f87779411569 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 30 Dec 2024 20:10:32 -0800 Subject: [PATCH] submissions: ReleaseSubmissions operation --- pkg/service/submissions.go | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/pkg/service/submissions.go b/pkg/service/submissions.go index c2f29bc..1824b6a 100644 --- a/pkg/service/submissions.go +++ b/pkg/service/submissions.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" + "git.itzana.me/strafesnet/go-grpc/maps" "git.itzana.me/strafesnet/maps-service/pkg/api" "git.itzana.me/strafesnet/maps-service/pkg/datastore" "git.itzana.me/strafesnet/maps-service/pkg/model" @@ -33,6 +34,8 @@ var ( ErrCreationPhaseSubmissionsLimit = errors.New("Active submissions limited to 20") ErrActiveSubmissionSameAssetID = errors.New("There is an active submission with the same AssetID") ErrActiveSubmissionSameTargetAssetID = errors.New("There is an active submission with the same TargetAssetID") + ErrReleaseInvalidStatus = errors.New("Only submissions with Uploaded status can be released") + ErrReleaseNoTargetAssetID = errors.New("Only submissions with a TargetAssetID can be released") ) // POST /submissions @@ -485,5 +488,63 @@ func (svc *Service) ActionSubmissionTriggerValidate(ctx context.Context, params // // POST /release-submissions func (svc *Service) ReleaseSubmissions(ctx context.Context, request []api.ReleaseInfo) error { + userInfo, ok := ctx.Value("UserInfo").(UserInfo) + if !ok { + return ErrUserInfo + } + + has_role, err := userInfo.HasRoleSubmissionRelease() + if err != nil { + return err + } + // check if caller has required role + if !has_role { + return ErrPermissionDenied + } + + idList := make([]int64, len(request)) + for i, releaseInfo := range request { + idList[i] = releaseInfo.SubmissionID + } + + // fetch submissions + submissions, err := svc.DB.Submissions().GetList(ctx, idList) + if err != nil { + return err + } + + // check each submission to make sure it is ready to release + for _,submission := range submissions{ + if submission.StatusID != model.StatusUploaded{ + return ErrReleaseInvalidStatus + } + if submission.TargetAssetID == 0{ + return ErrReleaseNoTargetAssetID + } + } + + for i,submission := range submissions{ + date := request[i].Date.Unix() + // create each map with go-grpc + _, err := svc.Client.Create(ctx, &maps.MapRequest{ + ID: submission.TargetAssetID, + DisplayName: &submission.DisplayName, + Creator: &submission.Creator, + GameID: &submission.GameID, + Date: &date, + }) + if err != nil { + return err + } + + // update each status to Released + smap := datastore.Optional() + smap.Add("status_id", model.StatusReleased) + err = svc.DB.Submissions().IfStatusThenUpdate(ctx, submission.ID, []model.Status{model.StatusUploaded}, smap) + if err != nil { + return err + } + } + return nil }