Skip to content

Commit e11a841

Browse files
refactor(api): unify extension handling via shared service
Linked extension_comments_api (student-requested extensions) to use the shared ExtensionService, previously set up for staff-granted extensions. This refactor ensures both student and staff extension flows use the same logic, improving consistency and reducing duplication.
1 parent b60c1b3 commit e11a841

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

app/api/extension_comments_api.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ class ExtensionCommentsApi < Grape::API
1010
requires :weeks_requested, type: Integer, desc: 'The details of the request'
1111
end
1212
post '/projects/:project_id/task_def_id/:task_definition_id/request_extension' do
13-
project = Project.find(params[:project_id])
14-
task_definition = project.unit.task_definitions.find(params[:task_definition_id])
15-
task = project.task_for_task_definition(task_definition)
16-
17-
# check permissions using specific permission has with addition of request extension if allowed in unit
18-
unless authorise? current_user, task, :request_extension, ->(role, perm_hash, other) { task.specific_permission_hash(role, perm_hash, other) }
19-
error!({ error: 'Not authorised to request an extension for this task' }, 403)
13+
# Use the ExtensionService to handle the extension request
14+
result = ExtensionService.grant_extension(
15+
params[:project_id],
16+
params[:task_definition_id],
17+
current_user,
18+
params[:weeks_requested],
19+
params[:comment]
20+
)
21+
22+
# Handle the service response
23+
if result[:success]
24+
present result[:result].serialize(current_user), Grape::Presenters::Presenter
25+
else
26+
error!({ error: result[:error] }, result[:status])
2027
end
21-
22-
error!({ error: 'Extension weeks can not be 0.' }, 403) if params[:weeks_requested] == 0
23-
24-
max_duration = task.weeks_can_extend
25-
duration = params[:weeks_requested]
26-
duration = max_duration unless params[:weeks_requested] <= max_duration
27-
28-
error!({ error: 'Extensions cannot be granted beyond task deadline.' }, 403) if duration <= 0
29-
30-
result = task.apply_for_extension(current_user, params[:comment], duration)
31-
present result.serialize(current_user), Grape::Presenters::Presenter
3228
end
3329

3430
desc 'Assess an extension for a task'

app/services/extension_service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def self.grant_extension(project_id, task_definition_id, user, weeks_requested,
1919

2020
# ===== Student Request Logic (current endpoint) =====
2121
unless is_staff_grant
22-
# Check task-level authorization for student requests
23-
unless AuthorisationHelpers.authorise?(user, task, :request_extension)
22+
# Check task-level authorization for student requests with specific permission hash
23+
unless AuthorisationHelpers.authorise?(user, task, :request_extension, ->(role, perm_hash, other) { task.specific_permission_hash(role, perm_hash, other) })
2424
return { success: false, error: 'Not authorised to request an extension for this task', status: 403 }
2525
end
2626
end

0 commit comments

Comments
 (0)