droboticflow/internal/api/supplier_project_authorizations.go

155 lines
5.4 KiB
Go

package api
import (
"errors"
"net/http"
"strings"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"github.com/go-chi/chi/v5"
)
type supplierProjectAuthorizationView struct {
domain.SupplierProjectAuthorization
QualityReviewerAvailable bool `json:"qualityReviewerAvailable"`
}
func (server *server) supplierAuthorizationProject(w http.ResponseWriter, r *http.Request) (*domain.ProjectRecord, bool) {
project, err := server.store.GetProject(r.Context(), strings.TrimSpace(chi.URLParam(r, "id")))
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return nil, false
}
if project == nil || !tenantMatches(project.OrganizationID, server.requestOrganizationID(r)) {
renderJSON(w, http.StatusNotFound, map[string]string{"error": "project not found"})
return nil, false
}
return project, true
}
func (server *server) supplierAuthorizationViews(r *http.Request, grants []domain.SupplierProjectAuthorization) ([]supplierProjectAuthorizationView, error) {
users, err := server.store.ListUsersByOrganization(r.Context(), server.requestOrganizationID(r))
if err != nil {
return nil, err
}
qc := map[string]bool{}
for _, user := range users {
if user.Role == domain.UserRoleQualityReviewer && user.Status == domain.UserStatusActive && !user.IsSuperAdmin {
qc[user.SupplierID] = true
}
}
views := make([]supplierProjectAuthorizationView, 0, len(grants))
for _, grant := range grants {
views = append(views, supplierProjectAuthorizationView{SupplierProjectAuthorization: grant, QualityReviewerAvailable: qc[grant.SupplierID]})
}
return views, nil
}
func (server *server) handleProjectSupplierAuthorizations(w http.ResponseWriter, r *http.Request) {
principal, ok := currentPrincipal(r)
if !ok || (!isOrganizationAdmin(principal) && !isSupplierManager(principal)) {
renderJSON(w, http.StatusForbidden, map[string]string{"error": "administrator or supplier manager required"})
return
}
project, ok := server.supplierAuthorizationProject(w, r)
if !ok {
return
}
grants, err := server.store.ListSupplierProjectAuthorizations(r.Context(), project.OrganizationID, project.ID)
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
if !isOrganizationAdmin(principal) {
filtered := grants[:0]
for _, grant := range grants {
if grant.SupplierID == principal.User.SupplierID {
filtered = append(filtered, grant)
}
}
grants = filtered
}
views, err := server.supplierAuthorizationViews(r, grants)
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
renderJSON(w, http.StatusOK, map[string]any{"items": views})
}
func (server *server) handleSupplierProjectAuthorizations(w http.ResponseWriter, r *http.Request) {
principal, ok := currentPrincipal(r)
if !ok || (!isOrganizationAdmin(principal) && !isSupplierManager(principal)) {
renderJSON(w, http.StatusForbidden, map[string]string{"error": "administrator or supplier manager required"})
return
}
supplierID := strings.TrimSpace(chi.URLParam(r, "supplierId"))
if isSupplierManager(principal) && principal.User.SupplierID != supplierID {
renderJSON(w, http.StatusForbidden, map[string]string{"error": "only your supplier's grants are visible"})
return
}
supplier, err := server.store.GetSupplier(r.Context(), supplierID)
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
if supplier == nil || !tenantMatches(supplier.OrganizationID, server.requestOrganizationID(r)) {
renderJSON(w, http.StatusNotFound, map[string]string{"error": "supplier not found"})
return
}
grants, err := server.store.ListSupplierProjectAuthorizations(r.Context(), supplier.OrganizationID, "")
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
filtered := grants[:0]
for _, grant := range grants {
if grant.SupplierID == supplierID {
filtered = append(filtered, grant)
}
}
views, err := server.supplierAuthorizationViews(r, filtered)
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
renderJSON(w, http.StatusOK, map[string]any{"items": views})
}
func (server *server) mutateSupplierProjectAuthorization(w http.ResponseWriter, r *http.Request, active bool) {
principal, ok := currentPrincipal(r)
if !ok || !isOrganizationAdmin(principal) {
renderJSON(w, http.StatusForbidden, map[string]string{"error": "platform administrator required"})
return
}
project, ok := server.supplierAuthorizationProject(w, r)
if !ok {
return
}
grant, err := server.store.SetSupplierProjectAuthorization(r.Context(), project.OrganizationID, project.ID, strings.TrimSpace(chi.URLParam(r, "supplierId")), active, principal.User.ID)
if errors.Is(err, store.ErrSupplierAuthorizationScope) {
renderJSON(w, http.StatusUnprocessableEntity, map[string]string{"error": "project or supplier is unavailable"})
return
}
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
views, err := server.supplierAuthorizationViews(r, []domain.SupplierProjectAuthorization{*grant})
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return
}
renderJSON(w, http.StatusOK, views[0])
}
func (server *server) handleSupplierProjectAuthorizationGrant(w http.ResponseWriter, r *http.Request) {
server.mutateSupplierProjectAuthorization(w, r, true)
}
func (server *server) handleSupplierProjectAuthorizationRevoke(w http.ResponseWriter, r *http.Request) {
server.mutateSupplierProjectAuthorization(w, r, false)
}