@@ -5,54 +5,74 @@ class SubscriptionIssuesController < ApplicationController
5
5
accept_api_auth :create
6
6
7
7
def create
8
- # Create a new issue based on the provided parameters and the subscription template
9
- @issue = Issue . new ( )
10
- @issue . project = @subscription_template . project
11
- @issue . tracker = @subscription_template . tracker
12
- @issue . subject = params [ :subject ]
13
- @issue . description = params [ :description ]
14
- @issue . is_private = @subscription_template . is_private
15
- @issue . status = @subscription_template . issue_status
16
- @issue . author = User . current
17
- @issue . category = @subscription_template . issue_category
18
- @issue . priority = @subscription_template . issue_priority
19
- @issue . fixed_version = @subscription_template . version
8
+ # Check if there's an existing issue with the same fiware_entity and subscription_template_id
9
+ # that was created within the threshold_create time frame
10
+ existing_issue = Issue . where ( fiware_entity : params [ "entity" ] , subscription_template_id : @subscription_template . id )
11
+ . where ( "created_on >= ?" , Time . now - @subscription_template . threshold_create . seconds )
12
+ . order ( created_on : :desc )
13
+ . first
20
14
21
- logger . info "################################################################################"
22
- logger . info "################################################################################"
23
- logger . info "Request received to create a new issue based on a subscription template"
24
- logger . info "Request parameters: #{ params } "
25
- logger . info "################################################################################"
15
+ if existing_issue
16
+ # Create a new journal note for the existing issue
17
+ note = existing_issue . init_journal ( User . current , params [ "notes" ] )
26
18
27
- # If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
28
- # try to convert it to an RGeo geometry object
29
- if Redmine ::Plugin . installed? ( :redmine_gtt ) && @subscription_template . project . module_enabled? ( 'gtt' ) && params [ :geometry ]
30
- begin
31
- @issue . geom = RedmineGtt ::Conversions . to_geom ( params [ :geometry ] . to_json )
32
- rescue => e
33
- logger . warn "Failed to convert geometry data: #{ e . message } "
19
+ # If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
20
+ # try to convert it to an RGeo geometry object and update the issue's geometry
21
+ if Redmine ::Plugin . installed? ( :redmine_gtt ) && @subscription_template . project . module_enabled? ( 'gtt' ) && params [ :geometry ]
22
+ begin
23
+ new_geom = RedmineGtt ::Conversions . to_geom ( params [ :geometry ] . to_json )
24
+ if new_geom != existing_issue . geom
25
+ old_geom = existing_issue . geom
26
+ existing_issue . geom = new_geom
27
+ # Create a new JournalDetail record to track the change to the geom attribute
28
+ note . details . build ( property : 'attr' , prop_key : 'geom' , old_value : old_geom , value : new_geom )
29
+ end
30
+ rescue => e
31
+ logger . warn "Failed to convert geometry data: #{ e . message } "
32
+ end
34
33
end
35
- end
36
34
37
- logger . info "################################################################################"
38
- logger . info "Create issue key/value:"
39
- @issue . attributes . each do |key , value |
40
- logger . info "#{ key } : #{ value } "
41
- end
42
- logger . info "################################################################################"
43
- logger . info "Subscription template key/value pairs:"
44
- @subscription_template . attributes . each do |key , value |
45
- logger . info "#{ key } : #{ value } "
46
- end
47
- logger . info "################################################################################"
48
- logger . info "################################################################################"
49
-
50
- if @issue . save
51
- # Respond with the newly created issue and a 201 status code
52
- render json : @issue . as_json ( include : [ :status , :tracker , :author , :assigned_to ] ) , status : :created
35
+ # Save the existing issue and respond with the appropriate JSON response
36
+ if existing_issue . save
37
+ render json : existing_issue . as_json ( include : [ :status , :tracker , :author , :assigned_to , :journals ] ) , status : :ok
38
+ else
39
+ render json : { errors : existing_issue . errors . full_messages } , status : :unprocessable_entity
40
+ end
53
41
else
54
- # If saving fails, respond with error messages and a 422 status code
55
- render json : { errors : @issue . errors . full_messages } , status : :unprocessable_entity
42
+ # Create a new issue based on the provided parameters and the subscription template
43
+ @issue = Issue . new ( )
44
+ @issue . project = @subscription_template . project
45
+ @issue . tracker = @subscription_template . tracker
46
+ @issue . subject = params [ :subject ]
47
+ @issue . description = params [ :description ]
48
+ @issue . is_private = @subscription_template . is_private
49
+ @issue . status = @subscription_template . issue_status
50
+ @issue . author = User . current
51
+ @issue . category = @subscription_template . issue_category
52
+ @issue . priority = @subscription_template . issue_priority
53
+ @issue . fixed_version = @subscription_template . version
54
+
55
+ # Set the fiware_entity and subscription_template_id attributes of the new issue
56
+ @issue . fiware_entity = params [ "entity" ]
57
+ @issue . subscription_template_id = @subscription_template . id
58
+
59
+ # If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
60
+ # try to convert it to an RGeo geometry object
61
+ if Redmine ::Plugin . installed? ( :redmine_gtt ) && @subscription_template . project . module_enabled? ( 'gtt' ) && params [ :geometry ]
62
+ begin
63
+ @issue . geom = RedmineGtt ::Conversions . to_geom ( params [ :geometry ] . to_json )
64
+ rescue => e
65
+ logger . warn "Failed to convert geometry data: #{ e . message } "
66
+ end
67
+ end
68
+
69
+ if @issue . save
70
+ # Respond with the newly created issue and a 201 status code
71
+ render json : @issue . as_json ( include : [ :status , :tracker , :author , :assigned_to ] ) , status : :created
72
+ else
73
+ # If saving fails, respond with error messages and a 422 status code
74
+ render json : { errors : @issue . errors . full_messages } , status : :unprocessable_entity
75
+ end
56
76
end
57
77
end
58
78
0 commit comments