From f485abb7b6ec057edb7d11a6381642c6e7a5b80d Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Fri, 22 Mar 2024 10:10:16 +0900 Subject: [PATCH] feature. seperate api for templates --- api/swagger/docs.go | 274 ++++++++++++++++-- api/swagger/swagger.json | 274 ++++++++++++++++-- api/swagger/swagger.yaml | 173 ++++++++++- internal/delivery/api/endpoint.go | 9 +- .../delivery/api/generated_endpoints.go.go | 72 ++++- internal/delivery/http/organization.go | 30 +- internal/delivery/http/stack-template.go | 68 +++++ .../http/system-notification-template.go | 121 ++++++++ internal/middleware/auth/role/default.go | 1 - internal/repository/organization.go | 44 ++- .../system-notification-template.go | 16 + internal/route/route.go | 5 + internal/usecase/organization.go | 49 ---- internal/usecase/stack-template.go | 48 +++ .../usecase/system-notification-template.go | 57 ++++ pkg/domain/organization.go | 13 +- pkg/domain/stack-template.go | 8 + pkg/domain/system-notification-template.go | 8 + pkg/httpErrors/errorCode.go | 15 +- 19 files changed, 1122 insertions(+), 163 deletions(-) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index 51083b71..0b44751f 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -5830,6 +5830,74 @@ const docTemplate = `{ } } } + }, + "put": { + "security": [ + { + "JWT": [] + } + ], + "description": "Remove organization stackTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "StackTemplates" + ], + "summary": "Remove organization stackTemplates", + "parameters": [ + { + "description": "Remove organization stack templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationStackTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "security": [ + { + "JWT": [] + } + ], + "description": "Add organization stackTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "StackTemplates" + ], + "summary": "Add organization stackTemplates", + "parameters": [ + { + "description": "Add organization stack templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.AddOrganizationStackTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } } }, "/organizations/{organizationId}/stack-templates/{stackTemplateId}": { @@ -6553,6 +6621,138 @@ const docTemplate = `{ } } }, + "/organizations/{organizationId}/system-notification-templates": { + "get": { + "security": [ + { + "JWT": [] + } + ], + "description": "Get Organization SystemNotificationTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "SystemNotificationTemplates" + ], + "summary": "Get Organization SystemNotificationTemplates", + "parameters": [ + { + "type": "string", + "description": "pageSize", + "name": "limit", + "in": "query" + }, + { + "type": "string", + "description": "pageNumber", + "name": "page", + "in": "query" + }, + { + "type": "string", + "description": "sortColumn", + "name": "soertColumn", + "in": "query" + }, + { + "type": "string", + "description": "sortOrder", + "name": "sortOrder", + "in": "query" + }, + { + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv", + "description": "filters", + "name": "filters", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.GetSystemNotificationTemplatesResponse" + } + } + } + }, + "put": { + "security": [ + { + "JWT": [] + } + ], + "description": "Remove organization systemNotificationTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "SystemNotificationTemplates" + ], + "summary": "Remove organization systemNotificationTemplates", + "parameters": [ + { + "description": "Remove organization systemNotification templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationSystemNotificationTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "security": [ + { + "JWT": [] + } + ], + "description": "Add organization systemNotificationTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "SystemNotificationTemplates" + ], + "summary": "Add organization systemNotificationTemplates", + "parameters": [ + { + "description": "Add organization systemNotification templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.AddOrganizationSystemNotificationTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/organizations/{organizationId}/system-notifications": { "get": { "security": [ @@ -7463,6 +7663,34 @@ const docTemplate = `{ } } }, + "github_com_openinfradev_tks-api_pkg_domain.AddOrganizationStackTemplatesRequest": { + "type": "object", + "required": [ + "stackTemplateIds" + ], + "properties": { + "stackTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "github_com_openinfradev_tks-api_pkg_domain.AddOrganizationSystemNotificationTemplatesRequest": { + "type": "object", + "required": [ + "systemNotificationTemplateIds" + ], + "properties": { + "systemNotificationTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "github_com_openinfradev_tks-api_pkg_domain.AddProjectMemberRequest": { "type": "object", "properties": { @@ -10549,6 +10777,34 @@ const docTemplate = `{ } } }, + "github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationStackTemplatesRequest": { + "type": "object", + "required": [ + "stackTemplateIds" + ], + "properties": { + "stackTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationSystemNotificationTemplatesRequest": { + "type": "object", + "required": [ + "systemNotificationTemplateIds" + ], + "properties": { + "systemNotificationTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "github_com_openinfradev_tks-api_pkg_domain.RemoveProjectMemberRequest": { "type": "object", "properties": { @@ -11383,24 +11639,6 @@ const docTemplate = `{ "type": "string", "maxLength": 30, "minLength": 1 - }, - "policyTemplateIds": { - "type": "array", - "items": { - "type": "string" - } - }, - "stackTemplateIds": { - "type": "array", - "items": { - "type": "string" - } - }, - "systemNotificationTemplateIds": { - "type": "array", - "items": { - "type": "string" - } } } }, diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index 3c11ca30..58f58a56 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -5824,6 +5824,74 @@ } } } + }, + "put": { + "security": [ + { + "JWT": [] + } + ], + "description": "Remove organization stackTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "StackTemplates" + ], + "summary": "Remove organization stackTemplates", + "parameters": [ + { + "description": "Remove organization stack templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationStackTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "security": [ + { + "JWT": [] + } + ], + "description": "Add organization stackTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "StackTemplates" + ], + "summary": "Add organization stackTemplates", + "parameters": [ + { + "description": "Add organization stack templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.AddOrganizationStackTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } } }, "/organizations/{organizationId}/stack-templates/{stackTemplateId}": { @@ -6547,6 +6615,138 @@ } } }, + "/organizations/{organizationId}/system-notification-templates": { + "get": { + "security": [ + { + "JWT": [] + } + ], + "description": "Get Organization SystemNotificationTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "SystemNotificationTemplates" + ], + "summary": "Get Organization SystemNotificationTemplates", + "parameters": [ + { + "type": "string", + "description": "pageSize", + "name": "limit", + "in": "query" + }, + { + "type": "string", + "description": "pageNumber", + "name": "page", + "in": "query" + }, + { + "type": "string", + "description": "sortColumn", + "name": "soertColumn", + "in": "query" + }, + { + "type": "string", + "description": "sortOrder", + "name": "sortOrder", + "in": "query" + }, + { + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv", + "description": "filters", + "name": "filters", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.GetSystemNotificationTemplatesResponse" + } + } + } + }, + "put": { + "security": [ + { + "JWT": [] + } + ], + "description": "Remove organization systemNotificationTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "SystemNotificationTemplates" + ], + "summary": "Remove organization systemNotificationTemplates", + "parameters": [ + { + "description": "Remove organization systemNotification templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationSystemNotificationTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "security": [ + { + "JWT": [] + } + ], + "description": "Add organization systemNotificationTemplates", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "SystemNotificationTemplates" + ], + "summary": "Add organization systemNotificationTemplates", + "parameters": [ + { + "description": "Add organization systemNotification templates request", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.AddOrganizationSystemNotificationTemplatesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/organizations/{organizationId}/system-notifications": { "get": { "security": [ @@ -7457,6 +7657,34 @@ } } }, + "github_com_openinfradev_tks-api_pkg_domain.AddOrganizationStackTemplatesRequest": { + "type": "object", + "required": [ + "stackTemplateIds" + ], + "properties": { + "stackTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "github_com_openinfradev_tks-api_pkg_domain.AddOrganizationSystemNotificationTemplatesRequest": { + "type": "object", + "required": [ + "systemNotificationTemplateIds" + ], + "properties": { + "systemNotificationTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "github_com_openinfradev_tks-api_pkg_domain.AddProjectMemberRequest": { "type": "object", "properties": { @@ -10543,6 +10771,34 @@ } } }, + "github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationStackTemplatesRequest": { + "type": "object", + "required": [ + "stackTemplateIds" + ], + "properties": { + "stackTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationSystemNotificationTemplatesRequest": { + "type": "object", + "required": [ + "systemNotificationTemplateIds" + ], + "properties": { + "systemNotificationTemplateIds": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "github_com_openinfradev_tks-api_pkg_domain.RemoveProjectMemberRequest": { "type": "object", "properties": { @@ -11377,24 +11633,6 @@ "type": "string", "maxLength": 30, "minLength": 1 - }, - "policyTemplateIds": { - "type": "array", - "items": { - "type": "string" - } - }, - "stackTemplateIds": { - "type": "array", - "items": { - "type": "string" - } - }, - "systemNotificationTemplateIds": { - "type": "array", - "items": { - "type": "string" - } } } }, diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index cc8b53ac..71ac2318 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -140,6 +140,24 @@ definitions: uri: type: string type: object + github_com_openinfradev_tks-api_pkg_domain.AddOrganizationStackTemplatesRequest: + properties: + stackTemplateIds: + items: + type: string + type: array + required: + - stackTemplateIds + type: object + github_com_openinfradev_tks-api_pkg_domain.AddOrganizationSystemNotificationTemplatesRequest: + properties: + systemNotificationTemplateIds: + items: + type: string + type: array + required: + - systemNotificationTemplateIds + type: object github_com_openinfradev_tks-api_pkg_domain.AddProjectMemberRequest: properties: projectMembers: @@ -2210,6 +2228,24 @@ definitions: $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.ParameterDef' type: array type: object + github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationStackTemplatesRequest: + properties: + stackTemplateIds: + items: + type: string + type: array + required: + - stackTemplateIds + type: object + github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationSystemNotificationTemplatesRequest: + properties: + systemNotificationTemplateIds: + items: + type: string + type: array + required: + - systemNotificationTemplateIds + type: object github_com_openinfradev_tks-api_pkg_domain.RemoveProjectMemberRequest: properties: projectMembers: @@ -2761,18 +2797,6 @@ definitions: maxLength: 30 minLength: 1 type: string - policyTemplateIds: - items: - type: string - type: array - stackTemplateIds: - items: - type: string - type: array - systemNotificationTemplateIds: - items: - type: string - type: array required: - name type: object @@ -6829,6 +6853,48 @@ paths: summary: Get Organization StackTemplates tags: - StackTemplates + post: + consumes: + - application/json + description: Add organization stackTemplates + parameters: + - description: Add organization stack templates request + in: body + name: body + required: true + schema: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.AddOrganizationStackTemplatesRequest' + produces: + - application/json + responses: + "200": + description: OK + security: + - JWT: [] + summary: Add organization stackTemplates + tags: + - StackTemplates + put: + consumes: + - application/json + description: Remove organization stackTemplates + parameters: + - description: Remove organization stack templates request + in: body + name: body + required: true + schema: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationStackTemplatesRequest' + produces: + - application/json + responses: + "200": + description: OK + security: + - JWT: [] + summary: Remove organization stackTemplates + tags: + - StackTemplates /organizations/{organizationId}/stack-templates/{stackTemplateId}: get: consumes: @@ -7286,6 +7352,89 @@ paths: summary: Check name for systemNotificationRule tags: - SystemNotificationRules + /organizations/{organizationId}/system-notification-templates: + get: + consumes: + - application/json + description: Get Organization SystemNotificationTemplates + parameters: + - description: pageSize + in: query + name: limit + type: string + - description: pageNumber + in: query + name: page + type: string + - description: sortColumn + in: query + name: soertColumn + type: string + - description: sortOrder + in: query + name: sortOrder + type: string + - collectionFormat: csv + description: filters + in: query + items: + type: string + name: filters + type: array + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.GetSystemNotificationTemplatesResponse' + security: + - JWT: [] + summary: Get Organization SystemNotificationTemplates + tags: + - SystemNotificationTemplates + post: + consumes: + - application/json + description: Add organization systemNotificationTemplates + parameters: + - description: Add organization systemNotification templates request + in: body + name: body + required: true + schema: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.AddOrganizationSystemNotificationTemplatesRequest' + produces: + - application/json + responses: + "200": + description: OK + security: + - JWT: [] + summary: Add organization systemNotificationTemplates + tags: + - SystemNotificationTemplates + put: + consumes: + - application/json + description: Remove organization systemNotificationTemplates + parameters: + - description: Remove organization systemNotification templates request + in: body + name: body + required: true + schema: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.RemoveOrganizationSystemNotificationTemplatesRequest' + produces: + - application/json + responses: + "200": + description: OK + security: + - JWT: [] + summary: Remove organization systemNotificationTemplates + tags: + - SystemNotificationTemplates /organizations/{organizationId}/system-notifications: get: consumes: diff --git a/internal/delivery/api/endpoint.go b/internal/delivery/api/endpoint.go index 2acd7f8d..2364a2ea 100644 --- a/internal/delivery/api/endpoint.go +++ b/internal/delivery/api/endpoint.go @@ -41,11 +41,11 @@ const ( // Organization Admin_CreateOrganization Admin_DeleteOrganization - UpdateOrganization GetOrganizations GetOrganization - UpdatePrimaryCluster CheckOrganizationName + UpdateOrganization + UpdatePrimaryCluster // Cluster CreateCluster @@ -105,6 +105,8 @@ const ( Admin_CheckStackTemplateName GetOrganizationStackTemplates GetOrganizationStackTemplate + AddOrganizationStackTemplates + RemoveOrganizationStackTemplates // Dashboard CreateDashboard @@ -120,6 +122,9 @@ const ( Admin_UpdateSystemNotificationTemplate Admin_GetSystemNotificationTemplates Admin_GetSystemNotificationTemplate + GetOrganizationSystemNotificationTemplates + AddOrganizationSystemNotificationTemplates + RemoveOrganizationSystemNotificationTemplates // SystemNotificationRule CreateSystemNotificationRule diff --git a/internal/delivery/api/generated_endpoints.go.go b/internal/delivery/api/generated_endpoints.go.go index 87efb1cc..891ac188 100644 --- a/internal/delivery/api/generated_endpoints.go.go +++ b/internal/delivery/api/generated_endpoints.go.go @@ -99,10 +99,6 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "Admin_DeleteOrganization", Group: "Organization", }, - UpdateOrganization: { - Name: "UpdateOrganization", - Group: "Organization", - }, GetOrganizations: { Name: "GetOrganizations", Group: "Organization", @@ -111,14 +107,18 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "GetOrganization", Group: "Organization", }, - UpdatePrimaryCluster: { - Name: "UpdatePrimaryCluster", - Group: "Organization", - }, CheckOrganizationName: { Name: "CheckOrganizationName", Group: "Organization", }, + UpdateOrganization: { + Name: "UpdateOrganization", + Group: "Organization", + }, + UpdatePrimaryCluster: { + Name: "UpdatePrimaryCluster", + Group: "Organization", + }, CreateCluster: { Name: "CreateCluster", Group: "Cluster", @@ -315,6 +315,14 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "GetOrganizationStackTemplate", Group: "StackTemplate", }, + AddOrganizationStackTemplates: { + Name: "AddOrganizationStackTemplates", + Group: "StackTemplate", + }, + RemoveOrganizationStackTemplates: { + Name: "RemoveOrganizationStackTemplates", + Group: "StackTemplate", + }, CreateDashboard: { Name: "CreateDashboard", Group: "Dashboard", @@ -359,6 +367,18 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "Admin_GetSystemNotificationTemplate", Group: "SystemNotificationTemplate", }, + GetOrganizationSystemNotificationTemplates: { + Name: "GetOrganizationSystemNotificationTemplates", + Group: "SystemNotificationTemplate", + }, + AddOrganizationSystemNotificationTemplates: { + Name: "AddOrganizationSystemNotificationTemplates", + Group: "SystemNotificationTemplate", + }, + RemoveOrganizationSystemNotificationTemplates: { + Name: "RemoveOrganizationSystemNotificationTemplates", + Group: "SystemNotificationTemplate", + }, CreateSystemNotificationRule: { Name: "CreateSystemNotificationRule", Group: "SystemNotificationRule", @@ -838,16 +858,16 @@ func (e Endpoint) String() string { return "Admin_CreateOrganization" case Admin_DeleteOrganization: return "Admin_DeleteOrganization" - case UpdateOrganization: - return "UpdateOrganization" case GetOrganizations: return "GetOrganizations" case GetOrganization: return "GetOrganization" - case UpdatePrimaryCluster: - return "UpdatePrimaryCluster" case CheckOrganizationName: return "CheckOrganizationName" + case UpdateOrganization: + return "UpdateOrganization" + case UpdatePrimaryCluster: + return "UpdatePrimaryCluster" case CreateCluster: return "CreateCluster" case GetClusters: @@ -946,6 +966,10 @@ func (e Endpoint) String() string { return "GetOrganizationStackTemplates" case GetOrganizationStackTemplate: return "GetOrganizationStackTemplate" + case AddOrganizationStackTemplates: + return "AddOrganizationStackTemplates" + case RemoveOrganizationStackTemplates: + return "RemoveOrganizationStackTemplates" case CreateDashboard: return "CreateDashboard" case GetDashboard: @@ -968,6 +992,12 @@ func (e Endpoint) String() string { return "Admin_GetSystemNotificationTemplates" case Admin_GetSystemNotificationTemplate: return "Admin_GetSystemNotificationTemplate" + case GetOrganizationSystemNotificationTemplates: + return "GetOrganizationSystemNotificationTemplates" + case AddOrganizationSystemNotificationTemplates: + return "AddOrganizationSystemNotificationTemplates" + case RemoveOrganizationSystemNotificationTemplates: + return "RemoveOrganizationSystemNotificationTemplates" case CreateSystemNotificationRule: return "CreateSystemNotificationRule" case GetSystemNotificationRules: @@ -1236,16 +1266,16 @@ func GetEndpoint(name string) Endpoint { return Admin_CreateOrganization case "Admin_DeleteOrganization": return Admin_DeleteOrganization - case "UpdateOrganization": - return UpdateOrganization case "GetOrganizations": return GetOrganizations case "GetOrganization": return GetOrganization - case "UpdatePrimaryCluster": - return UpdatePrimaryCluster case "CheckOrganizationName": return CheckOrganizationName + case "UpdateOrganization": + return UpdateOrganization + case "UpdatePrimaryCluster": + return UpdatePrimaryCluster case "CreateCluster": return CreateCluster case "GetClusters": @@ -1344,6 +1374,10 @@ func GetEndpoint(name string) Endpoint { return GetOrganizationStackTemplates case "GetOrganizationStackTemplate": return GetOrganizationStackTemplate + case "AddOrganizationStackTemplates": + return AddOrganizationStackTemplates + case "RemoveOrganizationStackTemplates": + return RemoveOrganizationStackTemplates case "CreateDashboard": return CreateDashboard case "GetDashboard": @@ -1366,6 +1400,12 @@ func GetEndpoint(name string) Endpoint { return Admin_GetSystemNotificationTemplates case "Admin_GetSystemNotificationTemplate": return Admin_GetSystemNotificationTemplate + case "GetOrganizationSystemNotificationTemplates": + return GetOrganizationSystemNotificationTemplates + case "AddOrganizationSystemNotificationTemplates": + return AddOrganizationSystemNotificationTemplates + case "RemoveOrganizationSystemNotificationTemplates": + return RemoveOrganizationSystemNotificationTemplates case "CreateSystemNotificationRule": return CreateSystemNotificationRule case "GetSystemNotificationRules": diff --git a/internal/delivery/http/organization.go b/internal/delivery/http/organization.go index ea446b40..6fc3e896 100644 --- a/internal/delivery/http/organization.go +++ b/internal/delivery/http/organization.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" - "github.com/google/uuid" "github.com/gorilla/mux" "github.com/openinfradev/tks-api/internal/middleware/auth/request" "github.com/openinfradev/tks-api/internal/model" @@ -325,39 +324,14 @@ func (h *OrganizationHandler) UpdateOrganization(w http.ResponseWriter, r *http. log.Info(r.Context(), err) } - for _, strId := range input.StackTemplateIds { - stackTemplateId, err := uuid.Parse(strId) - if err != nil || stackTemplateId == uuid.Nil { - ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid stackTemplateId"), "C_INVALID_STACK_TEMPLATE_ID", "")) - return - } - dto.StackTemplateIds = append(dto.StackTemplateIds, stackTemplateId) - } - for _, strId := range input.PolicyTemplateIds { - policyTemplateId, err := uuid.Parse(strId) - if err != nil || policyTemplateId == uuid.Nil { - ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid policyTemplateId"), "C_INVALID_POLICY_TEMPLATE_ID", "")) - return - } - dto.PolicyTemplateIds = append(dto.PolicyTemplateIds, policyTemplateId) - } - for _, strId := range input.SystemNotificationTemplateIds { - systemNotificationTemplateId, err := uuid.Parse(strId) - if err != nil || systemNotificationTemplateId == uuid.Nil { - ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid systemNotificationTemplateId"), "C_INVALID_SYSTEM_NOTIFICATION_TEMPLATE_ID", "")) - return - } - dto.SystemNotificationTemplateIds = append(dto.SystemNotificationTemplateIds, systemNotificationTemplateId) - } - - err = h.usecase.UpdateWithTemplates(r.Context(), organizationId, dto) + res, err := h.usecase.Update(r.Context(), organizationId, dto) if err != nil { ErrorJSON(w, r, err) return } var out domain.UpdateOrganizationResponse - out.ID = organizationId + out.ID = res.ID ResponseJSON(w, r, http.StatusOK, out) } diff --git a/internal/delivery/http/stack-template.go b/internal/delivery/http/stack-template.go index 3297f29a..ba1d7935 100644 --- a/internal/delivery/http/stack-template.go +++ b/internal/delivery/http/stack-template.go @@ -451,3 +451,71 @@ func (h *StackTemplateHandler) CheckStackTemplateName(w http.ResponseWriter, r * ResponseJSON(w, r, http.StatusOK, out) } + +// AddOrganizationStackTemplates godoc +// +// @Tags StackTemplates +// @Summary Add organization stackTemplates +// @Description Add organization stackTemplates +// @Accept json +// @Produce json +// @Param body body domain.AddOrganizationStackTemplatesRequest true "Add organization stack templates request" +// @Success 200 {object} nil +// @Router /organizations/{organizationId}/stack-templates [post] +// @Security JWT +func (h *StackTemplateHandler) AddOrganizationStackTemplates(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid organizationId"), "C_INVALID_ORGANIZATION_ID", "")) + return + } + + input := domain.AddOrganizationStackTemplatesRequest{} + err := UnmarshalRequestInput(r, &input) + if err != nil { + ErrorJSON(w, r, err) + return + } + + err = h.usecase.AddOrganizationStackTemplates(r.Context(), organizationId, input.StackTemplateIds) + if err != nil { + ErrorJSON(w, r, err) + return + } + ResponseJSON(w, r, http.StatusOK, nil) +} + +// RemoveOrganizationStackTemplates godoc +// +// @Tags StackTemplates +// @Summary Remove organization stackTemplates +// @Description Remove organization stackTemplates +// @Accept json +// @Produce json +// @Param body body domain.RemoveOrganizationStackTemplatesRequest true "Remove organization stack templates request" +// @Success 200 {object} nil +// @Router /organizations/{organizationId}/stack-templates [put] +// @Security JWT +func (h *StackTemplateHandler) RemoveOrganizationStackTemplates(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid organizationId"), "C_INVALID_ORGANIZATION_ID", "")) + return + } + + input := domain.RemoveOrganizationStackTemplatesRequest{} + err := UnmarshalRequestInput(r, &input) + if err != nil { + ErrorJSON(w, r, err) + return + } + + err = h.usecase.RemoveOrganizationStackTemplates(r.Context(), organizationId, input.StackTemplateIds) + if err != nil { + ErrorJSON(w, r, err) + return + } + ResponseJSON(w, r, http.StatusOK, nil) +} diff --git a/internal/delivery/http/system-notification-template.go b/internal/delivery/http/system-notification-template.go index f1d6dc44..73053e17 100644 --- a/internal/delivery/http/system-notification-template.go +++ b/internal/delivery/http/system-notification-template.go @@ -219,3 +219,124 @@ func (h *SystemNotificationTemplateHandler) UpdateSystemNotificationTemplate(w h ResponseJSON(w, r, http.StatusOK, nil) } + +// GetOrganizationSystemNotificationTemplates godoc +// +// @Tags SystemNotificationTemplates +// @Summary Get Organization SystemNotificationTemplates +// @Description Get Organization SystemNotificationTemplates +// @Accept json +// @Produce json +// @Param limit query string false "pageSize" +// @Param page query string false "pageNumber" +// @Param soertColumn query string false "sortColumn" +// @Param sortOrder query string false "sortOrder" +// @Param filters query []string false "filters" +// @Success 200 {object} domain.GetSystemNotificationTemplatesResponse +// @Router /organizations/{organizationId}/system-notification-templates [get] +// @Security JWT +func (h *SystemNotificationTemplateHandler) GetOrganizationSystemNotificationTemplates(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid organizationId"), "C_INVALID_ORGANIZATION_ID", "")) + return + } + + urlParams := r.URL.Query() + pg := pagination.NewPagination(&urlParams) + systemNotificationTemplates, err := h.usecase.FetchWithOrganization(r.Context(), organizationId, pg) + if err != nil { + ErrorJSON(w, r, err) + return + } + + var out domain.GetSystemNotificationTemplatesResponse + out.SystemNotificationTemplates = make([]domain.SystemNotificationTemplateResponse, len(systemNotificationTemplates)) + for i, systemNotificationTemplate := range systemNotificationTemplates { + if err := serializer.Map(r.Context(), systemNotificationTemplate, &out.SystemNotificationTemplates[i]); err != nil { + log.Info(r.Context(), err) + } + + out.SystemNotificationTemplates[i].Organizations = make([]domain.SimpleOrganizationResponse, len(systemNotificationTemplate.Organizations)) + for j, organization := range systemNotificationTemplate.Organizations { + if err := serializer.Map(r.Context(), organization, &out.SystemNotificationTemplates[i].Organizations[j]); err != nil { + log.Info(r.Context(), err) + } + } + } + + if out.Pagination, err = pg.Response(r.Context()); err != nil { + log.Info(r.Context(), err) + } + + ResponseJSON(w, r, http.StatusOK, out) +} + +// AddOrganizationSystemNotificationTemplates godoc +// +// @Tags SystemNotificationTemplates +// @Summary Add organization systemNotificationTemplates +// @Description Add organization systemNotificationTemplates +// @Accept json +// @Produce json +// @Param body body domain.AddOrganizationSystemNotificationTemplatesRequest true "Add organization systemNotification templates request" +// @Success 200 {object} nil +// @Router /organizations/{organizationId}/system-notification-templates [post] +// @Security JWT +func (h *SystemNotificationTemplateHandler) AddOrganizationSystemNotificationTemplates(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid organizationId"), "C_INVALID_ORGANIZATION_ID", "")) + return + } + + input := domain.AddOrganizationSystemNotificationTemplatesRequest{} + err := UnmarshalRequestInput(r, &input) + if err != nil { + ErrorJSON(w, r, err) + return + } + + err = h.usecase.AddOrganizationSystemNotificationTemplates(r.Context(), organizationId, input.SystemNotificationTemplateIds) + if err != nil { + ErrorJSON(w, r, err) + return + } + ResponseJSON(w, r, http.StatusOK, nil) +} + +// RemoveOrganizationSystemNotificationTemplates godoc +// +// @Tags SystemNotificationTemplates +// @Summary Remove organization systemNotificationTemplates +// @Description Remove organization systemNotificationTemplates +// @Accept json +// @Produce json +// @Param body body domain.RemoveOrganizationSystemNotificationTemplatesRequest true "Remove organization systemNotification templates request" +// @Success 200 {object} nil +// @Router /organizations/{organizationId}/system-notification-templates [put] +// @Security JWT +func (h *SystemNotificationTemplateHandler) RemoveOrganizationSystemNotificationTemplates(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid organizationId"), "C_INVALID_ORGANIZATION_ID", "")) + return + } + + input := domain.RemoveOrganizationSystemNotificationTemplatesRequest{} + err := UnmarshalRequestInput(r, &input) + if err != nil { + ErrorJSON(w, r, err) + return + } + + err = h.usecase.RemoveOrganizationSystemNotificationTemplates(r.Context(), organizationId, input.SystemNotificationTemplateIds) + if err != nil { + ErrorJSON(w, r, err) + return + } + ResponseJSON(w, r, http.StatusOK, nil) +} diff --git a/internal/middleware/auth/role/default.go b/internal/middleware/auth/role/default.go index d56e092e..70f1f256 100644 --- a/internal/middleware/auth/role/default.go +++ b/internal/middleware/auth/role/default.go @@ -58,7 +58,6 @@ var defaultPermissionOfAdmin = defaultPermission{ // Organization internalApi.Admin_CreateOrganization, - internalApi.Admin_UpdateOrganization, internalApi.Admin_DeleteOrganization, internalApi.GetOrganizations, internalApi.GetOrganization, diff --git a/internal/repository/organization.go b/internal/repository/organization.go index 306d9cc6..2ca270b6 100644 --- a/internal/repository/organization.go +++ b/internal/repository/organization.go @@ -20,9 +20,10 @@ type IOrganizationRepository interface { Update(ctx context.Context, organizationId string, in model.Organization) (model.Organization, error) UpdatePrimaryClusterId(ctx context.Context, organizationId string, primaryClusterId string) error UpdateAdminId(ctx context.Context, organizationId string, adminId uuid.UUID) error - UpdateStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) - UpdatePolicyTemplates(ctx context.Context, organizationId string, policyTemplates []model.PolicyTemplate) (err error) - UpdateSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplates []model.SystemNotificationTemplate) (err error) + AddStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) + RemoveStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) + AddSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplates []model.SystemNotificationTemplate) (err error) + RemoveSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplates []model.SystemNotificationTemplate) (err error) Delete(ctx context.Context, organizationId string) (err error) InitWorkflow(ctx context.Context, organizationId string, workflowId string, status domain.OrganizationStatus) error } @@ -167,14 +168,14 @@ func (r *OrganizationRepository) InitWorkflow(ctx context.Context, organizationI return nil } -func (r *OrganizationRepository) UpdateStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) { +func (r *OrganizationRepository) AddStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) { var organization = model.Organization{} res := r.db.WithContext(ctx).Preload("StackTemplates").First(&organization, "id = ?", organizationId) if res.Error != nil { return res.Error } - err = r.db.WithContext(ctx).Model(&organization).Association("StackTemplates").Replace(stackTemplates) + err = r.db.WithContext(ctx).Model(&organization).Association("StackTemplates").Append(stackTemplates) if err != nil { return err } @@ -182,22 +183,47 @@ func (r *OrganizationRepository) UpdateStackTemplates(ctx context.Context, organ return nil } -func (r *OrganizationRepository) UpdatePolicyTemplates(ctx context.Context, organizationId string, policyTemplates []model.PolicyTemplate) (err error) { - // [TODO] +func (r *OrganizationRepository) RemoveStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) { + var organization = model.Organization{} + res := r.db.WithContext(ctx).Preload("StackTemplates").First(&organization, "id = ?", organizationId) + if res.Error != nil { + return res.Error + } + + err = r.db.WithContext(ctx).Model(&organization).Association("StackTemplates").Delete(stackTemplates) + if err != nil { + return err + } return nil } -func (r *OrganizationRepository) UpdateSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplates []model.SystemNotificationTemplate) (err error) { +func (r *OrganizationRepository) AddSystemNotificationTemplates(ctx context.Context, organizationId string, templates []model.SystemNotificationTemplate) (err error) { var organization = model.Organization{} res := r.db.WithContext(ctx).Preload("SystemNotificationTemplates").First(&organization, "id = ?", organizationId) if res.Error != nil { return res.Error } - err = r.db.WithContext(ctx).Model(&organization).Association("SystemNotificationTemplates").Replace(systemNotificationTemplates) + err = r.db.WithContext(ctx).Model(&organization).Association("SystemNotificationTemplates").Append(templates) if err != nil { return err } + + return nil +} + +func (r *OrganizationRepository) RemoveSystemNotificationTemplates(ctx context.Context, organizationId string, templates []model.SystemNotificationTemplate) (err error) { + var organization = model.Organization{} + res := r.db.WithContext(ctx).Preload("SystemNotificationTemplates").First(&organization, "id = ?", organizationId) + if res.Error != nil { + return res.Error + } + + err = r.db.WithContext(ctx).Model(&organization).Association("SystemNotificationTemplates").Delete(templates) + if err != nil { + return err + } + return nil } diff --git a/internal/repository/system-notification-template.go b/internal/repository/system-notification-template.go index 1bf41ded..4c70884f 100644 --- a/internal/repository/system-notification-template.go +++ b/internal/repository/system-notification-template.go @@ -17,6 +17,7 @@ type ISystemNotificationTemplateRepository interface { Get(ctx context.Context, systemNotificationTemplateId uuid.UUID) (model.SystemNotificationTemplate, error) GetByName(ctx context.Context, name string) (model.SystemNotificationTemplate, error) Fetch(ctx context.Context, pg *pagination.Pagination) ([]model.SystemNotificationTemplate, error) + FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (out []model.SystemNotificationTemplate, err error) Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplateId uuid.UUID, err error) Update(ctx context.Context, dto model.SystemNotificationTemplate) (err error) Delete(ctx context.Context, dto model.SystemNotificationTemplate) (err error) @@ -62,6 +63,21 @@ func (r *SystemNotificationTemplateRepository) Fetch(ctx context.Context, pg *pa return } +func (r *SystemNotificationTemplateRepository) FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (out []model.SystemNotificationTemplate, err error) { + if pg == nil { + pg = pagination.NewPagination(nil) + } + + _, res := pg.Fetch( + r.db.WithContext(ctx).Preload(clause.Associations). + Joins("JOIN system_notification_template_organizations ON system_notification_template_organizations.system_notification_template_id = system_notification_templates.id AND system_notification_template_organizations.organization_id = ?", organizationId), + &out) + if res.Error != nil { + return nil, res.Error + } + return +} + func (r *SystemNotificationTemplateRepository) Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplateId uuid.UUID, err error) { dto.ID = uuid.New() res := r.db.WithContext(ctx).Create(&dto) diff --git a/internal/route/route.go b/internal/route/route.go index da1e31ce..6573b4e9 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -194,6 +194,8 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.Admin_DeleteStackTemplate, http.HandlerFunc(stackTemplateHandler.DeleteStackTemplate))).Methods(http.MethodDelete) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stack-templates", customMiddleware.Handle(internalApi.GetOrganizationStackTemplates, http.HandlerFunc(stackTemplateHandler.GetOrganizationStackTemplates))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.GetOrganizationStackTemplate, http.HandlerFunc(stackTemplateHandler.GetOrganizationStackTemplate))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stack-templates", customMiddleware.Handle(internalApi.AddOrganizationStackTemplates, http.HandlerFunc(stackTemplateHandler.AddOrganizationStackTemplates))).Methods(http.MethodPost) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stack-templates", customMiddleware.Handle(internalApi.RemoveOrganizationStackTemplates, http.HandlerFunc(stackTemplateHandler.RemoveOrganizationStackTemplates))).Methods(http.MethodPut) dashboardHandler := delivery.NewDashboardHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts", customMiddleware.Handle(internalApi.GetChartsDashboard, http.HandlerFunc(dashboardHandler.GetCharts))).Methods(http.MethodGet) @@ -218,6 +220,9 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/system-notification-templates", customMiddleware.Handle(internalApi.Admin_GetSystemNotificationTemplates, http.HandlerFunc(systemNotificationTemplateHandler.GetSystemNotificationTemplates))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/system-notification-templates/{systemNotificationTemplateId}", customMiddleware.Handle(internalApi.Admin_GetSystemNotificationTemplate, http.HandlerFunc(systemNotificationTemplateHandler.GetSystemNotificationTemplate))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/system-notification-templates/{systemNotificationTemplateId}", customMiddleware.Handle(internalApi.Admin_UpdateSystemNotificationTemplate, http.HandlerFunc(systemNotificationTemplateHandler.UpdateSystemNotificationTemplate))).Methods(http.MethodPut) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/system-notification-templates", customMiddleware.Handle(internalApi.GetOrganizationSystemNotificationTemplates, http.HandlerFunc(systemNotificationTemplateHandler.GetOrganizationSystemNotificationTemplates))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/system-notification-templates", customMiddleware.Handle(internalApi.AddOrganizationSystemNotificationTemplates, http.HandlerFunc(systemNotificationTemplateHandler.AddOrganizationSystemNotificationTemplates))).Methods(http.MethodPost) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/system-notification-templates", customMiddleware.Handle(internalApi.RemoveOrganizationSystemNotificationTemplates, http.HandlerFunc(systemNotificationTemplateHandler.RemoveOrganizationSystemNotificationTemplates))).Methods(http.MethodPut) systemNotificationRuleHandler := delivery.NewSystemNotificationRuleHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/system-notification-rules", customMiddleware.Handle(internalApi.CreateSystemNotificationRule, http.HandlerFunc(systemNotificationRuleHandler.CreateSystemNotificationRule))).Methods(http.MethodPost) diff --git a/internal/usecase/organization.go b/internal/usecase/organization.go index e8b105f4..ff7c59d0 100644 --- a/internal/usecase/organization.go +++ b/internal/usecase/organization.go @@ -26,7 +26,6 @@ type IOrganizationUsecase interface { Get(ctx context.Context, organizationId string) (model.Organization, error) Update(ctx context.Context, organizationId string, dto model.Organization) (model.Organization, error) UpdatePrimaryClusterId(ctx context.Context, organizationId string, clusterId string) (err error) - UpdateWithTemplates(ctx context.Context, organizationId string, dto model.Organization) (err error) ChangeAdminId(ctx context.Context, organizationId string, adminId uuid.UUID) error Delete(ctx context.Context, organizationId string, accessToken string) error } @@ -194,51 +193,3 @@ func (u *OrganizationUsecase) ChangeAdminId(ctx context.Context, organizationId return nil } - -func (u *OrganizationUsecase) UpdateWithTemplates(ctx context.Context, organizationId string, dto model.Organization) (err error) { - _, err = u.Update(ctx, organizationId, dto) - if err != nil { - return err - } - - stackTemplates := make([]model.StackTemplate, 0) - for _, stackTemplateId := range dto.StackTemplateIds { - stackTemplate, err := u.stackTemplateRepo.Get(ctx, stackTemplateId) - if err != nil { - return fmt.Errorf("Invalid stackTemplateId") - } - stackTemplates = append(stackTemplates, stackTemplate) - } - err = u.repo.UpdateStackTemplates(ctx, organizationId, stackTemplates) - if err != nil { - return httpErrors.NewBadRequestError(err, "O_FAILED_UPDATE_STACK_TEMPLATES", "") - } - - policyTemplates := make([]model.PolicyTemplate, 0) - for _, policyTemplateId := range dto.PolicyTemplateIds { - policyTemplate, err := u.policyTemplateRepo.GetByID(ctx, policyTemplateId) - if err != nil { - return fmt.Errorf("Invalid policyTemplateId") - } - policyTemplates = append(policyTemplates, *policyTemplate) - } - err = u.repo.UpdatePolicyTemplates(ctx, organizationId, policyTemplates) - if err != nil { - return httpErrors.NewBadRequestError(err, "O_FAILED_UPDATE_POLICY_TEMPLATES", "") - } - - systemNotificationTemplates := make([]model.SystemNotificationTemplate, 0) - for _, systemNotificationTemplateId := range dto.SystemNotificationTemplateIds { - systemNotificationTemplate, err := u.systemNotificationTemplateRepo.Get(ctx, systemNotificationTemplateId) - if err != nil { - return fmt.Errorf("Invalid systemNotificationTemplateId") - } - systemNotificationTemplates = append(systemNotificationTemplates, systemNotificationTemplate) - } - err = u.repo.UpdateSystemNotificationTemplates(ctx, organizationId, systemNotificationTemplates) - if err != nil { - return httpErrors.NewBadRequestError(err, "O_FAILED_UPDATE_SYSTEM_NOTIFICATION_TEMPLATES", "") - } - - return nil -} diff --git a/internal/usecase/stack-template.go b/internal/usecase/stack-template.go index 7004d79b..2b56f9e1 100644 --- a/internal/usecase/stack-template.go +++ b/internal/usecase/stack-template.go @@ -25,6 +25,8 @@ type IStackTemplateUsecase interface { Delete(ctx context.Context, dto model.StackTemplate) error UpdateOrganizations(ctx context.Context, dto model.StackTemplate) error GetByName(ctx context.Context, name string) (model.StackTemplate, error) + AddOrganizationStackTemplates(ctx context.Context, organizationId string, stackTemplateIds []string) error + RemoveOrganizationStackTemplates(ctx context.Context, organizationId string, stackTemplateIds []string) error } type StackTemplateUsecase struct { @@ -153,3 +155,49 @@ func (u *StackTemplateUsecase) UpdateOrganizations(ctx context.Context, dto mode return nil } + +func (u *StackTemplateUsecase) AddOrganizationStackTemplates(ctx context.Context, organizationId string, stackTemplateIds []string) error { + _, err := u.organizationRepo.Get(ctx, organizationId) + if err != nil { + return httpErrors.NewBadRequestError(err, "O_NOT_EXISTED_NAME", "") + } + + stackTemplates := make([]model.StackTemplate, 0) + for _, strId := range stackTemplateIds { + stackTemplateId, _ := uuid.Parse(strId) + stackTemplate, err := u.repo.Get(ctx, stackTemplateId) + if err == nil { + stackTemplates = append(stackTemplates, stackTemplate) + } + } + + err = u.organizationRepo.AddStackTemplates(ctx, organizationId, stackTemplates) + if err != nil { + return httpErrors.NewBadRequestError(err, "ST_FAILED_ADD_ORGANIZATION_STACK_TEMPLATE", "") + } + + return nil +} + +func (u *StackTemplateUsecase) RemoveOrganizationStackTemplates(ctx context.Context, organizationId string, stackTemplateIds []string) error { + _, err := u.organizationRepo.Get(ctx, organizationId) + if err != nil { + return httpErrors.NewBadRequestError(err, "O_NOT_EXISTED_NAME", "") + } + + stackTemplates := make([]model.StackTemplate, 0) + for _, strId := range stackTemplateIds { + stackTemplateId, _ := uuid.Parse(strId) + stackTemplate, err := u.repo.Get(ctx, stackTemplateId) + if err == nil { + stackTemplates = append(stackTemplates, stackTemplate) + } + } + + err = u.organizationRepo.RemoveStackTemplates(ctx, organizationId, stackTemplates) + if err != nil { + return httpErrors.NewBadRequestError(err, "ST_FAILED_REMOVE_ORGANIZATION_STACK_TEMPLATE", "") + } + + return nil +} diff --git a/internal/usecase/system-notification-template.go b/internal/usecase/system-notification-template.go index f4f4acf1..6516eb77 100644 --- a/internal/usecase/system-notification-template.go +++ b/internal/usecase/system-notification-template.go @@ -19,9 +19,12 @@ type ISystemNotificationTemplateUsecase interface { Get(ctx context.Context, alertId uuid.UUID) (model.SystemNotificationTemplate, error) GetByName(ctx context.Context, name string) (model.SystemNotificationTemplate, error) Fetch(ctx context.Context, pg *pagination.Pagination) ([]model.SystemNotificationTemplate, error) + FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) ([]model.SystemNotificationTemplate, error) Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplate uuid.UUID, err error) Update(ctx context.Context, dto model.SystemNotificationTemplate) error Delete(ctx context.Context, dto model.SystemNotificationTemplate) error + AddOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error + RemoveOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error } type SystemNotificationTemplateUsecase struct { @@ -114,6 +117,14 @@ func (u *SystemNotificationTemplateUsecase) Fetch(ctx context.Context, pg *pagin return } +func (u *SystemNotificationTemplateUsecase) FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (res []model.SystemNotificationTemplate, err error) { + res, err = u.repo.FetchWithOrganization(ctx, organizationId, pg) + if err != nil { + return nil, err + } + return res, nil +} + func (u *SystemNotificationTemplateUsecase) Delete(ctx context.Context, dto model.SystemNotificationTemplate) (err error) { return nil } @@ -139,3 +150,49 @@ func (u *SystemNotificationTemplateUsecase) UpdateOrganizations(ctx context.Cont return nil } + +func (u *SystemNotificationTemplateUsecase) AddOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error { + _, err := u.organizationRepo.Get(ctx, organizationId) + if err != nil { + return httpErrors.NewBadRequestError(err, "O_NOT_EXISTED_NAME", "") + } + + systemNotificationTemplates := make([]model.SystemNotificationTemplate, 0) + for _, strId := range systemNotificationTemplateIds { + systemNotificationTemplateId, _ := uuid.Parse(strId) + systemNotificationTemplate, err := u.repo.Get(ctx, systemNotificationTemplateId) + if err == nil { + systemNotificationTemplates = append(systemNotificationTemplates, systemNotificationTemplate) + } + } + + err = u.organizationRepo.AddSystemNotificationTemplates(ctx, organizationId, systemNotificationTemplates) + if err != nil { + return httpErrors.NewBadRequestError(err, "ST_FAILED_ADD_ORGANIZATION_SYSTEM_NOTIFICATION_TEMPLATE", "") + } + + return nil +} + +func (u *SystemNotificationTemplateUsecase) RemoveOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error { + _, err := u.organizationRepo.Get(ctx, organizationId) + if err != nil { + return httpErrors.NewBadRequestError(err, "O_NOT_EXISTED_NAME", "") + } + + systemNotificationTemplates := make([]model.SystemNotificationTemplate, 0) + for _, strId := range systemNotificationTemplateIds { + systemNotificationTemplateId, _ := uuid.Parse(strId) + systemNotificationTemplate, err := u.repo.Get(ctx, systemNotificationTemplateId) + if err == nil { + systemNotificationTemplates = append(systemNotificationTemplates, systemNotificationTemplate) + } + } + + err = u.organizationRepo.RemoveSystemNotificationTemplates(ctx, organizationId, systemNotificationTemplates) + if err != nil { + return httpErrors.NewBadRequestError(err, "ST_FAILED_REMOVE_ORGANIZATION_SYSTEM_NOTIFICATION_TEMPLATE", "") + } + + return nil +} diff --git a/pkg/domain/organization.go b/pkg/domain/organization.go index 478dc45b..ef19f12c 100644 --- a/pkg/domain/organization.go +++ b/pkg/domain/organization.go @@ -91,11 +91,8 @@ type ListOrganizationResponse struct { } type UpdateOrganizationRequest struct { - Name string `json:"name" validate:"required,min=1,max=30"` - Description string `json:"description" validate:"omitempty,min=0,max=100"` - StackTemplateIds []string `json:"stackTemplateIds"` - PolicyTemplateIds []string `json:"policyTemplateIds"` - SystemNotificationTemplateIds []string `json:"systemNotificationTemplateIds"` + Name string `json:"name" validate:"required,min=1,max=30"` + Description string `json:"description" validate:"omitempty,min=0,max=100"` } type UpdateOrganizationResponse struct { @@ -106,6 +103,12 @@ type UpdatePrimaryClusterRequest struct { PrimaryClusterId string `json:"primaryClusterId"` } +type UpdateOrganizationTemplatesRequest struct { + StackTemplateIds *[]string `json:"stackTemplateIds,omitempty"` + PolicyTemplateIds *[]string `json:"policyTemplateIds,omitempty"` + SystemNotificationTemplateIds *[]string `json:"systemNotificationTemplateIds,omitempty"` +} + type DeleteOrganizationResponse struct { ID string `json:"id"` } diff --git a/pkg/domain/stack-template.go b/pkg/domain/stack-template.go index ff1f2570..e5bce152 100644 --- a/pkg/domain/stack-template.go +++ b/pkg/domain/stack-template.go @@ -104,3 +104,11 @@ type UpdateStackTemplateOrganizationsRequest struct { type CheckStackTemplateNameResponse struct { Existed bool `json:"existed"` } + +type AddOrganizationStackTemplatesRequest struct { + StackTemplateIds []string `json:"stackTemplateIds" validate:"required"` +} + +type RemoveOrganizationStackTemplatesRequest struct { + StackTemplateIds []string `json:"stackTemplateIds" validate:"required"` +} diff --git a/pkg/domain/system-notification-template.go b/pkg/domain/system-notification-template.go index d4ef68c5..8b242289 100644 --- a/pkg/domain/system-notification-template.go +++ b/pkg/domain/system-notification-template.go @@ -59,3 +59,11 @@ type GetSystemNotificationTemplatesResponse struct { type GetSystemNotificationTemplateResponse struct { SystemNotificationTemplate SystemNotificationTemplateResponse `json:"systemNotificationTemplate"` } + +type AddOrganizationSystemNotificationTemplatesRequest struct { + SystemNotificationTemplateIds []string `json:"systemNotificationTemplateIds" validate:"required"` +} + +type RemoveOrganizationSystemNotificationTemplatesRequest struct { + SystemNotificationTemplateIds []string `json:"systemNotificationTemplateIds" validate:"required"` +} diff --git a/pkg/httpErrors/errorCode.go b/pkg/httpErrors/errorCode.go index a6acff9c..5dff7467 100644 --- a/pkg/httpErrors/errorCode.go +++ b/pkg/httpErrors/errorCode.go @@ -36,6 +36,7 @@ var errorMap = map[ErrorCode]string{ // Organization "O_INVALID_ORGANIZATION_NAME": "조직에 이미 존재하는 이름입니다.", + "O_NOT_EXISTED_NAME": "조직이 존재하지 않습니다.", "O_FAILED_UPDATE_STACK_TEMPLATES": "조직에 스택템플릿을 설정하는데 실패했습니다", "O_FAILED_UPDATE_POLICY_TEMPLATES": "조직에 정책템플릿을 설정하는데 실패했습니다", "O_FAILED_UPDATE_SYSTEM_NOTIFICATION_TEMPLATES": "조직에 알림템플릿을 설정하는데 실패했습니다", @@ -99,11 +100,15 @@ var errorMap = map[ErrorCode]string{ "AG_FAILED_TO_CALL_WORKFLOW": "워크플로우 호출에 실패하였습니다.", // StackTemplate - "ST_CREATE_ALREADY_EXISTED_NAME": "스택템플릿에 이미 존재하는 이름입니다.", - "ST_FAILED_UPDATE_ORGANIZATION": "스택템플릿에 조직을 설정하는데 실패했습니다.", - "ST_NOT_EXISTED_STACK_TEMPLATE": "업데이트할 스택템플릿이 존재하지 않습니다.", - "ST_INVALID_STACK_TEMAPLTE_NAME": "유효하지 않은 스택템플릿 이름입니다. 스택템플릿 이름을 확인하세요.", - "ST_FAILED_FETCH_STACK_TEMPLATE": "스택템플릿을 가져오는데 실패했습니다.", + "ST_CREATE_ALREADY_EXISTED_NAME": "스택템플릿에 이미 존재하는 이름입니다.", + "ST_FAILED_UPDATE_ORGANIZATION": "스택템플릿에 조직을 설정하는데 실패했습니다.", + "ST_NOT_EXISTED_STACK_TEMPLATE": "업데이트할 스택템플릿이 존재하지 않습니다.", + "ST_INVALID_STACK_TEMAPLTE_NAME": "유효하지 않은 스택템플릿 이름입니다. 스택템플릿 이름을 확인하세요.", + "ST_FAILED_FETCH_STACK_TEMPLATE": "스택템플릿을 가져오는데 실패했습니다.", + "ST_FAILED_ADD_ORGANIZATION_STACK_TEMPLATE": "조직에 스택템플릿을 추가하는데 실패하였습니다.", + "ST_FAILED_REMOVE_ORGANIZATION_STACK_TEMPLATE": "조직에서 스택템플릿을 삭제하는데 실패하였습니다.", + "ST_FAILED_ADD_ORGANIZATION_SYSTEM_NOTIFICATION_TEMPLATE": "조직에 시스템알람템플릿을 추가하는데 실패하였습니다.", + "ST_FAILED_REMOVE_ORGANIZATION_SYSTEM_NOTIFICATION_TEMPLATE": "조직에서 시스템알람템플릿을 삭제하는데 실패하였습니다.", // PolicyTemplate "PT_CREATE_ALREADY_EXISTED_NAME": "정첵 템플릿에 이미 존재하는 이름입니다.",