*: add creatorUserID and cretedAt to organization
This commit is contained in:
parent
c30707528f
commit
041e8867f8
|
@ -17,6 +17,7 @@ package command
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/sorintlab/agola/internal/datamanager"
|
||||
"github.com/sorintlab/agola/internal/db"
|
||||
|
@ -55,6 +56,17 @@ func (s *CommandHandler) CreateOrg(ctx context.Context, org *types.Organization)
|
|||
if u != nil {
|
||||
return util.NewErrBadRequest(errors.Errorf("org %q already exists", u.Name))
|
||||
}
|
||||
|
||||
if org.CreatorUserID != "" {
|
||||
user, err := s.readDB.GetUser(tx, org.CreatorUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user == nil {
|
||||
return util.NewErrBadRequest(errors.Errorf("creator user %q doesn't exist", org.CreatorUserID))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -62,6 +74,7 @@ func (s *CommandHandler) CreateOrg(ctx context.Context, org *types.Organization)
|
|||
}
|
||||
|
||||
org.ID = uuid.NewV4().String()
|
||||
org.CreatedAt = time.Now()
|
||||
orgj, err := json.Marshal(org)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to marshal org")
|
||||
|
|
|
@ -45,6 +45,13 @@ func NewCreateOrgHandler(logger *zap.Logger, ch *command.CommandHandler) *Create
|
|||
func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var userID string
|
||||
userIDVal := ctx.Value("userid")
|
||||
if userIDVal != nil {
|
||||
userID = userIDVal.(string)
|
||||
}
|
||||
h.log.Infof("userID: %q", userID)
|
||||
|
||||
var req CreateOrgRequest
|
||||
d := json.NewDecoder(r.Body)
|
||||
if err := d.Decode(&req); err != nil {
|
||||
|
@ -53,7 +60,8 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
creq := &command.CreateOrgRequest{
|
||||
Name: req.Name,
|
||||
Name: req.Name,
|
||||
CreatorUserID: userID,
|
||||
}
|
||||
|
||||
org, err := h.ch.CreateOrg(ctx, creq)
|
||||
|
|
|
@ -25,6 +25,8 @@ import (
|
|||
|
||||
type CreateOrgRequest struct {
|
||||
Name string
|
||||
|
||||
CreatorUserID string
|
||||
}
|
||||
|
||||
func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) {
|
||||
|
@ -38,6 +40,9 @@ func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (
|
|||
org := &types.Organization{
|
||||
Name: req.Name,
|
||||
}
|
||||
if req.CreatorUserID != "" {
|
||||
org.CreatorUserID = req.CreatorUserID
|
||||
}
|
||||
|
||||
c.log.Infof("creating organization")
|
||||
org, resp, err := c.configstoreClient.CreateOrg(ctx, org)
|
||||
|
|
|
@ -17,6 +17,7 @@ package command
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
|
@ -48,6 +49,21 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
|
|||
return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path"))
|
||||
}
|
||||
|
||||
pg, resp, err := c.configstoreClient.GetProjectGroup(ctx, req.ParentID)
|
||||
if err != nil {
|
||||
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q", req.Name))
|
||||
}
|
||||
|
||||
projectPath := path.Join(pg.Path, req.Name)
|
||||
_, resp, err = c.configstoreClient.GetProject(ctx, projectPath)
|
||||
if err != nil {
|
||||
if resp != nil && resp.StatusCode != http.StatusNotFound {
|
||||
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", req.Name))
|
||||
}
|
||||
} else {
|
||||
return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath))
|
||||
}
|
||||
|
||||
user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID)
|
||||
if err != nil {
|
||||
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
|
||||
|
|
|
@ -81,6 +81,11 @@ type Organization struct {
|
|||
ID string `json:"id,omitempty"`
|
||||
|
||||
Name string `json:"name,omitempty"`
|
||||
|
||||
// CreatorUserID is the user id that created the organization. It could be empty
|
||||
// if the org was created by using the admin user or the user has been removed.
|
||||
CreatorUserID string `json:"creator_user_id,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectGroup struct {
|
||||
|
|
Loading…
Reference in New Issue