Path: blob/main/components/gitpod-db/go/organization_membership.go
2497 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package db56import (7"context"8"errors"9"fmt"10"time"1112"github.com/google/uuid"13"gorm.io/gorm"14)1516type OrganizationMembership struct {17ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`1819OrganizationID uuid.UUID `gorm:"column:teamId;type:char;size:36;" json:"teamId"`20UserID uuid.UUID `gorm:"column:userId;type:char;size:36;" json:"userId"`21Role OrganizationMembershipRole `gorm:"column:role;type:varchar;size:255;" json:"role"`2223CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`24// Read-only (-> property).25LastModified time.Time `gorm:"->:column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`26}2728// TableName sets the insert table name for this struct type29func (d *OrganizationMembership) TableName() string {30return "d_b_team_membership"31}3233type OrganizationMembershipRole string3435const (36OrganizationMembershipRole_Owner = OrganizationMembershipRole("owner")37OrganizationMembershipRole_Member = OrganizationMembershipRole("member")38)3940func GetOrganizationMembership(ctx context.Context, conn *gorm.DB, userID, orgID uuid.UUID) (OrganizationMembership, error) {41if userID == uuid.Nil {42return OrganizationMembership{}, errors.New("user ID must not be empty")43}4445if orgID == uuid.Nil {46return OrganizationMembership{}, errors.New("Organization ID must not be empty")47}4849var membership OrganizationMembership50tx := conn.WithContext(ctx).51Where("userId = ?", userID.String()).52Where("teamId = ?", orgID.String()).53First(&membership)54if tx.Error != nil {55if errors.Is(tx.Error, gorm.ErrRecordNotFound) {56return OrganizationMembership{}, fmt.Errorf("no membership record for user %s and organization %s exists: %w", userID.String(), orgID.String(), ErrorNotFound)57}58return OrganizationMembership{}, fmt.Errorf("failed to retrieve organization membership for user %s, organization %s: %w", userID.String(), orgID.String(), tx.Error)59}6061return membership, nil62}6364func DeleteOrganizationMembership(ctx context.Context, conn *gorm.DB, userID uuid.UUID, orgID uuid.UUID) error {65if userID == uuid.Nil {66return errors.New("user ID must not be empty")67}6869if orgID == uuid.Nil {70return errors.New("organization ID must not be empty")71}7273tx := conn.WithContext(ctx).74Model(&OrganizationMembership{}).75Where("userId = ?", userID.String()).76Where("teamId = ?", orgID.String()).77Delete(&OrganizationMembership{})78if tx.Error != nil {79return fmt.Errorf("failed to retrieve organization membership for user %s, organization %s: %w", userID.String(), orgID.String(), tx.Error)80}81if tx.RowsAffected == 0 {82return fmt.Errorf("no membership record for user %s and organization %s exists: %w", userID.String(), orgID.String(), ErrorNotFound)83}8485return nil86}878889