Skip to content
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

add more changes #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions app/controllers/managers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class TranslationAssignmentsController < ApplicationController
before_action :fetch_invitations
before_action :fetch_assignment, except: [:index]

def show
if !@gig_invitation || @gig_invitation.status == 'rejected'
flash[:error] = 'You are not authorised to view this assignment'
redirect_to root_path and return
end
end

def index
@type = params[:type] || 'new'
@page_heading = 'My Assignments'

if @type == 'new'
invitation_status = 'pending'
status = 'active'
elsif @type == 'current'
invitation_status = 'accepted'
status = 'active'
else
invitation_status = 'accepted'
status = 'archived'
end

gig_ids = @gig_invitations.select {|gi| gi.status == invitation_status }.map(&:gig_id)
@assignments = TranslationAssignment.where(gig_idx: gig_ids, status: status).order(created_at: :desc)
end

def accept
CIRRO_V2_CLIENT.GigInvitation.accept(@gig_invitation.id)

redirect_to translation_assignment_path(@assignment), notice: 'Assignment accepted successfully'
end

def reject
CIRRO_V2_CLIENT.GigInvitation.reject(@gig_invitation.id)

redirect_to root_path(@assignment), notice: 'Assignment rejected successfully'
end

private

def fetch_invitations
@gig_invitations = CIRRO_V2_CLIENT.GigInvitation.list(user_id: current_user.uid).data
end

def fetch_assignment
@assignment = TranslationAssignment.find(params[:id])
@gig_invitation = @gig_invitations.find {|gi| gi.gig_id.to_s == @assignment.gig_idx.to_s }
end
end
33 changes: 33 additions & 0 deletions app/models/attempt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class TranslationFile < ApplicationRecord
mount_uploader :file, TranslationFileUploader
belongs_to :translation_assignment
has_one :translation_result

validates :status, presence: true

scope :for_user, ->(user) {
includes(:translation_result).references(:translation_result).where("translation_files.status = 'available' OR translation_results.user_id = ?", user.id)
}
scope :available, -> { where(status: :available) }
scope :in_progress, -> { where(status: :in_progress) }
scope :pending_review, -> { where(status: :waiting_for_review) }


state_machine :status, initial: :available do
event :pick do
transition available: :in_progress
end

event :submit_for_review do
transition in_progress: :waiting_for_review
end

event :review do
transition waiting_for_review: :reviewed
end

event :expire do
transition available: :expired
end
end
end
96 changes: 96 additions & 0 deletions app/models/heavy_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
class TranslationAssignment < ApplicationRecord
has_many :translation_files
has_many :translation_results, through: :translation_files

accepts_nested_attributes_for :translation_files

attr_accessor :total_seats

after_commit :create_gig_with_tasks_and_invitation_filter, on: :create

validates :title, presence: true
validates :domain, presence: true
validates :from_language, presence: true
validates :to_language, presence: true
validates :status, presence: true

state_machine :status, initial: :active do
after_transition active: :archived do |assignment, _|
assignment.translation_files.available.map(&:expire)
assignment.archive_gig
end

state :archived do
validate { |assignment| assignment.archivable? }
end

event :archive do
transition active: :archived
end
end

def archivable?
translation_files.in_progress.empty? && translation_files.pending_review.empty?
end

def title_with_id
"##{id} #{title}"
end

# In a real application interaction with cirro should be happening inside a background job
def archive_gig
# Normally we would store the gig task's id
# so that we don't need to fetch the gig here
gig = CIRRO_V2_CLIENT.Gig.find(gig_idx)
task = gig.tasks.data.first
results = translation_results.accepted

gig_results = results.group_by(&:user).map do |user, res|
CIRRO_V2_CLIENT.GigResult.create(gig_task_id: task.id,
user_id: user.uid,
title: "translation ##{id}",
description: "Language: #{from_language} > #{to_language}",
quantity: res.count,
delivery_date: res.first.submitted_at,
cost_center_key: 'EPMCIR') # epam project code to which the cost is booked
end

gig_time_activities = results.group_by(&:user).map do |user, res|
CIRRO_V2_CLIENT.GigTimeActivity.create(gig_id: gig.id,
user_id: user.uid,
date: Time.current,
description: "translation ##{id}: #{from_language} > #{to_language}",
duration_in_ms: res.map { |result| result.submitted_at - result.started_at }.sum * 1000) # need to split it into different days
end

CIRRO_V2_CLIENT.Gig.archive(gig_idx)
end

private

def create_gig_with_tasks_and_invitation_filter
price_per_translation_result = 500

created_gig = CIRRO_V2_CLIENT.Gig.create(
title: title,
description: description,
seats_min: 10,
invitation_mode: 'auto',
archive_at: 1.month.from_now,
start_at: Time.current,
end_at: 1.week.from_now,
url: Rails.application.routes.url_helpers.translation_assignment_url(id, host: Settings.host),
filter_query: {
languages: {
'$in': [from_language, to_language]
},
domains: [domain]
},
tasks: [
{ title: self.class.name, base_price: price_per_translation_result }
]
)

update_attribute(:gig_idx, created_gig.id)
end
end