Repository navigation
[Customer Entity] Add deployments search API and move product versions to resource-scoped route #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Rashmika998
merged 4 commits into
wso2-open-operations:v2
from
cloby99:task/entity-service
May 28, 2026
Merged
[Customer Entity] Add deployments search API and move product versions to resource-scoped route #751
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
91701d3
[Customer Entity] Add deployments table with deployment_type_enum and…
cloby99 84fcec5
[Customer Entity] Implement deployment search repository, service, an…
cloby99 5c40eed
[Customer Entity] Move product versions search to POST /products/{id}…
cloby99 e9eeb96
Merge remote-tracking branch 'upstream/v2' into task/entity-service
cloby99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| // Package handler is declared in user_handler.go. | ||
| package handler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/wso2-open-operations/cs-tools/entity-service/internal/domain" | ||
| "github.com/wso2-open-operations/cs-tools/entity-service/internal/service" | ||
| ) | ||
|
|
||
| // DeploymentHandler handles HTTP requests for the deployment resource. | ||
| type DeploymentHandler struct { | ||
| svc service.DeploymentService | ||
| } | ||
|
|
||
| // NewDeploymentHandler constructs a DeploymentHandler with the given service. | ||
| func NewDeploymentHandler(svc service.DeploymentService) *DeploymentHandler { | ||
| return &DeploymentHandler{svc: svc} | ||
| } | ||
|
|
||
| // SearchDeployments handles POST /deployments/search. | ||
| func (h *DeploymentHandler) SearchDeployments(w http.ResponseWriter, r *http.Request) { | ||
| var req domain.SearchDeploymentsRequest | ||
| if !decodeRequest(w, r, &req) { | ||
| return | ||
| } | ||
| resp, err := h.svc.SearchDeployments(r.Context(), req) | ||
| if err != nil { | ||
| writeServiceError(w, r, err) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(resp) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package repository | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/jackc/pgx/v5/pgxpool" | ||
| "github.com/wso2-open-operations/cs-tools/entity-service/internal/domain" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| // DeploymentRepository defines the persistence operations for the deployments table. | ||
| type DeploymentRepository interface { | ||
| // SearchDeployments returns a filtered, paginated slice of deployments together | ||
| // with the total count of matching rows before pagination. | ||
| // COUNT and SELECT are executed concurrently on separate pool connections. | ||
| SearchDeployments(ctx context.Context, req domain.SearchDeploymentsRequest) ([]domain.Deployment, int, error) | ||
| } | ||
|
|
||
| type deploymentRepo struct { | ||
| db *pgxpool.Pool | ||
| } | ||
|
|
||
| // NewDeploymentRepository constructs a DeploymentRepository backed by the given connection pool. | ||
| func NewDeploymentRepository(db *pgxpool.Pool) DeploymentRepository { | ||
| return &deploymentRepo{db: db} | ||
| } | ||
|
|
||
| // SearchDeployments implements DeploymentRepository. | ||
| func (r *deploymentRepo) SearchDeployments(ctx context.Context, req domain.SearchDeploymentsRequest) ([]domain.Deployment, int, error) { | ||
| filterArgs := []any{} | ||
| argIdx := 1 | ||
|
|
||
| where := "WHERE 1=1" | ||
|
|
||
| if len(req.ProjectIDs) > 0 { | ||
| // Cast the parameter to uuid[] so the column stays uncast and idx_deployments_project_id is usable. | ||
| where += fmt.Sprintf(" AND project_id = ANY($%d::uuid[])", argIdx) | ||
| filterArgs = append(filterArgs, req.ProjectIDs) | ||
| argIdx++ | ||
| } | ||
|
|
||
| if len(req.DeploymentTypeKeys) > 0 { | ||
| // Convert []DeploymentType to []string — pgx has no codec for named string types. | ||
| // Cast the parameter to deployment_type_enum[] so the column stays uncast and idx_deployments_type is usable. | ||
| typeStrings := make([]string, len(req.DeploymentTypeKeys)) | ||
| for i, t := range req.DeploymentTypeKeys { | ||
| typeStrings[i] = string(t) | ||
| } | ||
| where += fmt.Sprintf(" AND type = ANY($%d::deployment_type_enum[])", argIdx) | ||
| filterArgs = append(filterArgs, typeStrings) | ||
| argIdx++ | ||
| } | ||
|
|
||
| if req.SearchQuery != "" { | ||
| escaped := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(req.SearchQuery) | ||
| pattern := "%" + escaped + "%" | ||
| where += fmt.Sprintf(" AND (name ILIKE $%d ESCAPE '\\')", argIdx) | ||
| filterArgs = append(filterArgs, pattern) | ||
| argIdx++ | ||
| } | ||
|
|
||
| countQuery := "SELECT COUNT(*) FROM deployments " + where | ||
|
|
||
| dataQuery := fmt.Sprintf( | ||
| `SELECT id, project_id, name, type, description, created_by, created_at, updated_at | ||
| FROM deployments %s | ||
| ORDER BY created_at DESC, id | ||
| LIMIT $%d OFFSET $%d`, | ||
| where, argIdx, argIdx+1, | ||
| ) | ||
| dataArgs := append(append([]any{}, filterArgs...), req.Pagination.Limit, req.Pagination.Offset) | ||
|
|
||
| var total int | ||
| var deployments []domain.Deployment | ||
|
|
||
| eg, egCtx := errgroup.WithContext(ctx) | ||
|
|
||
| eg.Go(func() error { | ||
| if err := r.db.QueryRow(egCtx, countQuery, filterArgs...).Scan(&total); err != nil { | ||
| return fmt.Errorf("count deployments: %w", err) | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| eg.Go(func() error { | ||
| rows, err := r.db.Query(egCtx, dataQuery, dataArgs...) | ||
| if err != nil { | ||
| return fmt.Errorf("query deployments: %w", err) | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| result := make([]domain.Deployment, 0, req.Pagination.Limit) | ||
| for rows.Next() { | ||
| var d domain.Deployment | ||
| if err := rows.Scan( | ||
| &d.ID, &d.ProjectID, &d.Name, &d.Type, &d.Description, &d.CreatedBy, | ||
| &d.CreatedAt, &d.UpdatedAt, | ||
| ); err != nil { | ||
| return fmt.Errorf("scan deployment: %w", err) | ||
| } | ||
| result = append(result, d) | ||
| } | ||
| if err := rows.Err(); err != nil { | ||
| return fmt.Errorf("iterate deployments: %w", err) | ||
| } | ||
| deployments = result | ||
| return nil | ||
| }) | ||
|
|
||
| if err := eg.Wait(); err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| return deployments, total, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| // Package service is declared in interfaces.go. | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/wso2-open-operations/cs-tools/entity-service/internal/domain" | ||
| "github.com/wso2-open-operations/cs-tools/entity-service/internal/repository" | ||
| ) | ||
|
|
||
| type deploymentService struct { | ||
| repo repository.DeploymentRepository | ||
| } | ||
|
|
||
| // NewDeploymentService constructs a DeploymentService backed by the given repository. | ||
| func NewDeploymentService(repo repository.DeploymentRepository) DeploymentService { | ||
| return &deploymentService{repo: repo} | ||
| } | ||
|
|
||
| // SearchDeployments implements DeploymentService. | ||
| func (s *deploymentService) SearchDeployments(ctx context.Context, req domain.SearchDeploymentsRequest) (domain.SearchDeploymentsResponse, error) { | ||
| if err := normalizePagination(&req.Pagination); err != nil { | ||
| return domain.SearchDeploymentsResponse{}, err | ||
| } | ||
| if err := validateSearchQuery(req.SearchQuery); err != nil { | ||
| return domain.SearchDeploymentsResponse{}, err | ||
| } | ||
|
|
||
| deployments, total, err := s.repo.SearchDeployments(ctx, req) | ||
| if err != nil { | ||
| return domain.SearchDeploymentsResponse{}, err | ||
| } | ||
|
|
||
| return domain.SearchDeploymentsResponse{ | ||
| Deployments: deployments, | ||
| Total: total, | ||
| Limit: req.Pagination.Limit, | ||
| Offset: req.Pagination.Offset, | ||
| HasMore: req.Pagination.Offset+len(deployments) < total, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| DROP TABLE IF EXISTS deployments; | ||
| DROP TYPE IF EXISTS deployment_type_enum; |
26 changes: 26 additions & 0 deletions
26
entity-service/migrations/000006_create_deployments.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| CREATE TYPE deployment_type_enum AS ENUM ( | ||
| 'primary_production', | ||
| 'staging', | ||
| 'qa', | ||
| 'stress', | ||
| 'uat', | ||
| 'development' | ||
| ); | ||
|
cloby99 marked this conversation as resolved.
Resolved
|
||
|
|
||
| CREATE TABLE IF NOT EXISTS deployments ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| project_id UUID NOT NULL REFERENCES projects(id), | ||
| name TEXT NOT NULL, | ||
| type deployment_type_enum NOT NULL, | ||
| description TEXT, | ||
| created_by UUID NOT NULL REFERENCES users(id), | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
|
|
||
| CONSTRAINT uq_deployment_project_name UNIQUE (project_id, name) | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_deployments_project_id ON deployments (project_id); | ||
| CREATE INDEX IF NOT EXISTS idx_deployments_created_by ON deployments (created_by); | ||
| CREATE INDEX IF NOT EXISTS idx_deployments_type ON deployments (type); | ||
| CREATE INDEX IF NOT EXISTS idx_deployments_project_type ON deployments (project_id, type); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.