Added has_many assoziations for creator and cashier to jobs, fixed all views according to that changes. Implemented allocation of roles infos when status changes in job model.

This commit is contained in:
2024-10-01 17:45:52 +02:00
parent de3aa07259
commit 5b60eb64ad
13 changed files with 212 additions and 69 deletions

View File

@@ -22,8 +22,7 @@ class Operator::JobsController < ApplicationController
def create def create
@job = Job.new(job_params) @job = Job.new(job_params)
@job.created_by_operator = true @job.created_by_operator = true
# TODO: rename costumer to creater? When created by operator the operator is referenced instead of costumer. @job.creator = current_user
@job.costumer = current_user
respond_to do |format| respond_to do |format|
if @job.save if @job.save
@@ -40,6 +39,8 @@ class Operator::JobsController < ApplicationController
def update def update
@job.assign_attributes(job_params) @job.assign_attributes(job_params)
@status_changed = @job.status_changed? @status_changed = @job.status_changed?
@job.current_user = current_user if @status_changed
respond_to do |format| respond_to do |format|
if @job.save if @job.save
broadcast_update_job broadcast_update_job

View File

@@ -1,6 +1,10 @@
class Job < ApplicationRecord class Job < ApplicationRecord
belongs_to :operator, class_name: "User", optional: true, counter_cache: :jobs_as_operator_count attr_accessor :current_user
belongs_to :costumer, class_name: "User", optional: true, counter_cache: :jobs_as_costumer_count
belongs_to :costumer, class_name: "User", optional: true, counter_cache: :jobs_as_costumer_count, inverse_of: :jobs_as_costumer
belongs_to :operator, class_name: "User", optional: true, counter_cache: :jobs_as_operator_count, inverse_of: :jobs_as_operator
belongs_to :creator, class_name: "User", optional: true, counter_cache: :created_jobs_count, inverse_of: :created_jobs
belongs_to :cashier, class_name: "User", optional: true, counter_cache: :cashed_jobs_count, inverse_of: :cashed_jobs
has_one_attached :pdf, dependent: :purge has_one_attached :pdf, dependent: :purge
@@ -11,11 +15,12 @@ class Job < ApplicationRecord
validate :acceptable_pdf validate :acceptable_pdf
before_save :update_printed_at, if: :will_save_change_to_status?
before_save :update_paid_at, if: :will_save_change_to_status?
before_save :update_status_changed_at, if: :will_save_change_to_status? before_save :update_status_changed_at, if: :will_save_change_to_status?
before_save :update_user_status_infos, if: :will_save_change_to_status?
before_save :set_cost_qm before_save :set_cost_qm
before_save :calc_cost, if: :printed_pages_changes? before_save :calc_cost, if: :printed_pages_changes?
before_validation :set_costumer_infos, unless: :created_by_operator?, on: :create before_validation :set_costumer_infos, unless: :created_by_operator?, on: :create
# TODO: works only when job is created. Should move analyzer to activestorage : # TODO: works only when job is created. Should move analyzer to activestorage :
@@ -36,6 +41,10 @@ class Job < ApplicationRecord
AVAILABLE_PAGE_FORMATS = [ :a0, :a1, :a2, :a3 ] AVAILABLE_PAGE_FORMATS = [ :a0, :a1, :a2, :a3 ]
# scope :created_as_operator, -> { where created_as_operator: true }
# scope :created_as_costumer, -> { where created_as_operator: false }
scope :not_canceled, -> { !canceled }
# NOTE: only named status are returned because of WHERE/IN clause for the enum values # NOTE: only named status are returned because of WHERE/IN clause for the enum values
scope :in_status_order, -> { in_order_of(:status, %w[open printing pickup paid canceled]) } scope :in_status_order, -> { in_order_of(:status, %w[open printing pickup paid canceled]) }
@@ -51,6 +60,8 @@ class Job < ApplicationRecord
scope :status_changed_on_day, lambda { |date| scope :status_changed_on_day, lambda { |date|
where("status_changed_at >= ? AND status_changed_at <= ?", date.beginning_of_day, date.end_of_day) where("status_changed_at >= ? AND status_changed_at <= ?", date.beginning_of_day, date.end_of_day)
} }
scope :created_by_costumer, -> { not(:created_by_operator) }
# Returns all jobs with status: open print pickup and jobs from today with status: paid canceled # Returns all jobs with status: open print pickup and jobs from today with status: paid canceled
# paid: only updated_at today # paid: only updated_at today
# canceled: only updated_at today # canceled: only updated_at today
@@ -111,19 +122,18 @@ class Job < ApplicationRecord
end end
def self.report_to_csv(jobs) def self.report_to_csv(jobs)
columns = [ "id", "costumer_firstname", "costumer_lastname", "paid_at", "cost" ] columns_readable = [ "ID", "Kunde Vorname", "Kunde Nachname", "Kassierer Vorname", "Kassierer Nachname", "bezahlt am", "Betrag" ]
columns_readable = [ "ID", "Name", "Nachname", "bezahlt am", "Betrag" ]
CSV.generate(col_sep: ";") do |csv| CSV.generate(col_sep: ";") do |csv|
csv << columns_readable csv << columns_readable
jobs.each do |job| jobs.each do |job|
# csv << job.attributes.values_at(*columns) # csv << job.attributes.values_at(*columns)
csv << [ job.id, job.costumer_firstname, job.costumer_lastname, job.paid_at.localtime.strftime("%Y-%m-%d"), job.cost.to_s + "" ] csv << [ job.id, job.costumer_firstname, job.costumer_lastname, job.cashier_firstname, job.cashier_lastname, job.paid_at.localtime.strftime("%Y-%m-%d"), job.cost.to_s + "" ]
end end
end end
end end
def self.ransackable_attributes(auth_object = nil) def self.ransackable_attributes(auth_object = nil)
[ "created_at", "id", "costumer_firstname", "costumer_lastname", "pdf.", "number_of_plans_a0", "number_of_plans_a1", "number_of_plans_a2", "number_of_plans_a3", "costum_qm_plan", "cost", "status" ] [ "created_at", "id", "costumer_firstname", "costumer_lastname", "pdf.", "created_by_operator", "number_of_plans_a0", "number_of_plans_a1", "number_of_plans_a2", "number_of_plans_a3", "costum_qm_plan", "cost", "status" ]
end end
def self.ransackable_associations(auth_object = nil) def self.ransackable_associations(auth_object = nil)
@@ -136,14 +146,6 @@ class Job < ApplicationRecord
costum_qm_plan_changed? || number_of_plans_a0_changed? || number_of_plans_a1_changed? || number_of_plans_a2_changed? || number_of_plans_a3_changed? costum_qm_plan_changed? || number_of_plans_a0_changed? || number_of_plans_a1_changed? || number_of_plans_a2_changed? || number_of_plans_a3_changed?
end end
def update_printed_at
self.printed_at = Time.now if pickup? || (paid? && printed_at.nil?)
end
def update_paid_at
self.paid_at = Time.now if paid?
end
def update_status_changed_at def update_status_changed_at
self.status_changed_at = Time.now self.status_changed_at = Time.now
end end
@@ -186,7 +188,67 @@ class Job < ApplicationRecord
end end
def set_costumer_infos def set_costumer_infos
self.costumer = current_user unless self.costumer
self.costumer_firstname = costumer.firstname self.costumer_firstname = costumer.firstname
self.costumer_lastname = costumer.lastname self.costumer_lastname = costumer.lastname
end end
def set_operator_infos
self.operator = current_user unless self.operator
self.operator_firstname = operator.firstname
self.operator_lastname = operator.lastname
end
def clear_operator_infos
self.operator = nil
self.operator_firstname = nil
self.operator_lastname = nil
end
def set_cashier_infos
self.paid_at = Time.now
self.cashier = current_user unless self.cashier
self.cashier_firstname = cashier.firstname
self.cashier_lastname = cashier.lastname
end
def clear_cashier_infos
self.paid_at = nil
self.cashier = nil
self.cashier_firstname = nil
self.cashier_lastname = nil
end
def reset_operator_and_cashier_infos
clear_operator_infos
clear_cashier_infos
end
def update_user_status_infos
if status_changed?
case status.to_sym
when :open
reset_operator_and_cashier_infos
self.printed_at = nil
self.paid_at = nil
when :printing
clear_cashier_infos
set_operator_infos
self.printed_at = nil
when :pickup
clear_cashier_infos
self.printed_at = Time.now unless self.printed_at
when :paid
set_operator_infos unless self.operator
self.printed_at = Time.now unless self.printed_at
set_cashier_infos unless self.cashier
self.paid_at = Time.now unless self.paid_at
end
end
end
def reset_to_status_open?
status_changed? && open?
end
end end

View File

@@ -1,7 +1,10 @@
class User < ApplicationRecord class User < ApplicationRecord
has_secure_password has_secure_password
# has_many :jobs
has_many :jobs_as_costumer, foreign_key: :costumer_id, class_name: "Job" has_many :jobs_as_costumer, foreign_key: :costumer_id, class_name: "Job"
has_many :jobs_as_operator, foreign_key: :operator_id, class_name: "Job" has_many :jobs_as_operator, foreign_key: :operator_id, class_name: "Job"
has_many :created_jobs, foreign_key: :creator_id, class_name: "Job"
has_many :cashed_jobs, foreign_key: :cashier_id, class_name: "Job"
generates_token_for :email_verification, expires_in: 2.days do generates_token_for :email_verification, expires_in: 2.days do
email email

View File

@@ -44,11 +44,20 @@
<%= l job.created_at.localtime.to_date %> <%= l job.created_at.localtime.to_date %>
</td> </td>
<td class="p-2 py-3 text-center"> <td class="p-2 py-3 text-center">
<%= l job.paid_at.localtime.to_date if job.paid_at %> <% if job.created_by_operator %>
</td> <%= link_to admin_user_path(job.creator) do %>
<td class="p-2 py-3"> <span class="badge badge-status">Operator</span>
<% end %>
<% else %>
<%= link_to admin_user_path(job.creator) do %>
<span class="badge badge-status badge-hover">Kunden</span>
<% end %>
<% end %>
</span>
</td>
<td class="p-2 py-3">
<span class="badge badge-status text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light"> <span class="badge badge-status text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light">
<%= job.status %> <%= job.status %>
</span> </span>
</td> </td>
</tr> </tr>

View File

@@ -15,6 +15,8 @@
<%= f.search_field :costumer_firstname_or_costumer_lastname_or_pdf_blob_filename_cont, placeholder: "Suchen", oninput: 'this.form.requestSubmit();' %> <%= f.search_field :costumer_firstname_or_costumer_lastname_or_pdf_blob_filename_cont, placeholder: "Suchen", oninput: 'this.form.requestSubmit();' %>
<%= f.label :status_eq, "Status:" %> <%= f.label :status_eq, "Status:" %>
<%= f.select :status_eq, Job.statuses.keys, {include_blank: "alle"}, onchange: 'this.form.requestSubmit();' %> <%= f.select :status_eq, Job.statuses.keys, {include_blank: "alle"}, onchange: 'this.form.requestSubmit();' %>
<%= f.label :created_by_operator_eq, "Erstellt vom:" %>
<%= f.select :created_by_operator_eq, [["Operator", true],["Kunden", false]], {include_blank: "alle"}, onchange: 'this.form.requestSubmit();' %>
<%= f.submit "Filter anwenden", class: "py-2 px-3 bg-hsrm-red hover:bg-hsrm-red-light shadow-lg text-white inline-block font-medium cursor-pointer" %> <%= f.submit "Filter anwenden", class: "py-2 px-3 bg-hsrm-red hover:bg-hsrm-red-light shadow-lg text-white inline-block font-medium cursor-pointer" %>
</div> </div>
<% end %> <% end %>
@@ -40,7 +42,7 @@
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :costum_qm_plan, "no DIN") %></th> <th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :costum_qm_plan, "no DIN") %></th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :cost, "Kosten") %></th> <th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :cost, "Kosten") %></th>
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_at, "Erstellt am") %></th> <th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_at, "Erstellt am") %></th>
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_at, "Bezahlt am") %></th> <th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_by_operator, "Erstellt von") %></th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :status, "Status") %></th> <th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :status, "Status") %></th>
</tr> </tr>
</thead> </thead>

View File

@@ -6,6 +6,10 @@
</p> </p>
<p><%= user.email %></p> <p><%= user.email %></p>
<p>E-Mail Verifiziert: <p>E-Mail Verifiziert:
<%= icon bool_icon(user.verified), class: "icon #{user.verified ? "text-green-600" : "text-red-600"}" %> <%= icon bool_icon(user.verified), class: "icon #{user.verified ? "text-green-600" : "text-red-600"}" %></p>
<p><%= user.created_at %></p> <p><%= user.created_at %></p>
</div> <p>Druckaufträge als Kunde: <%= @user.jobs_as_costumer.size %></p>
<p>davon abgebrochen: <%= @user.jobs_as_cosutmer.canceled.size %></p>
<p>Druckaufträge als Operator: <%= @user.jobs_as_operator.size %></p>
<p>Druckaufträge kassiert: <%= @user.cashed_jobs.size %></p>
</div>

View File

@@ -18,7 +18,7 @@
<%= highlight user.email, [params.dig(:q, :firstname_or_lastname_or_email_cont).to_s, params.dig(:q, :email_start).to_s] %> <%= highlight user.email, [params.dig(:q, :firstname_or_lastname_or_email_cont).to_s, params.dig(:q, :email_start).to_s] %>
</td> </td>
<td class="p-2 py-3 text-right"> <td class="p-2 py-3 text-right">
<%= user.jobs_as_costumer.where.not(status: :canceled).size %> <%= user.created_jobs.not_canceled.size %>
</td> </td>
<td class="p-2 py-3 text-center"> <td class="p-2 py-3 text-center">
<%= l user.created_at.localtime.to_date %> <%= l user.created_at.localtime.to_date %>

View File

@@ -1,14 +1,8 @@
<div> <div>
<h1 class="text-4xl font-bold">Benutzer Details</h1> <h1 class="text-4xl font-bold text-hsrm-gray py-4">Benutzer Details</h1>
<%= render partial: 'user', locals: { user: @user } %> <%= render partial: 'user', locals: { user: @user } %>
</div> </div>
<p>Some Stats: <h1 class="py-4 text-4xl font-bold text-hsrm-gray">Die letzten 10 Druckaufträge</h1>
<ul>
<li>Druckaufträge insgesammt: <%= @user.jobs_as_costumer.size %></li>
<li>davon abgebrochen: <%= @user.jobs_as_costumer.canceled.size %></li>
<li>letzten 5 Druckaufträge</li>
</ul>
</p>
<div class="min-w-full overflow-auto shadow-lg"> <div class="min-w-full overflow-auto shadow-lg">
<table class="w-full py-8 table-auto"> <table class="w-full py-8 table-auto">
<thead class="font-semibold tracking-wide bg-gray-200 border-b-2 border-gray-300 text text-hsrm-gray"> <thead class="font-semibold tracking-wide bg-gray-200 border-b-2 border-gray-300 text text-hsrm-gray">
@@ -27,7 +21,7 @@
</tr> </tr>
</thead> </thead>
<tbody id='jobs' class="divide-y divivde-gray-300"> <tbody id='jobs' class="divide-y divivde-gray-300">
<%= render partial: "jobs/job_tr", collection: @user.jobs_as_costumer.limit(5), as: :job, locals: { no_actions: true } %> <%= render partial: "jobs/job_tr", collection: @user.created_jobs.order(created_at: :desc).limit(10), as: :job, locals: { no_actions: true } %>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@@ -4,36 +4,58 @@
</div> </div>
<div> <div>
<p class="my-3"> <p class="my-3">
<strong class="mb-1 font-medium">Operator ID:</strong> <strong class="mb-1 font-medium">Costumer ID:</strong>
<% if job.operator %> <% if job.costumer %>
<%= link_to_if allowed_to?(:show, job.operator, namespace: :Admin), "#{job.operatorr_id} - #{job.operator.name} (#{job.operator.email})", admin_user_path(job.operator) %> <%= link_to_if allowed_to?(:show?, job.costumer, namespace: :Admin), "#{job.costumer_id} - #{job.costumer.name} (#{job.costumer.email})", admin_user_path(job.costumer) %>
<% else %> <% else %>
- -
<% end %> <% end %>
</p> </p>
<p class="my-3"> <p class="my-3">
<strong class="mb-1 font-medium">Costumer ID:</strong> <strong class="mb-1 font-medium">Creator ID:</strong>
<% if job.costumer %> <% if job.creator %>
<%= link_to_if allowed_to?(:show, job.costumer, namespace: :Admin), "#{job.costumer_id} - #{job.costumer.name} (#{job.costumer.email})", admin_user_path(job.costumer) %> <%= link_to_if allowed_to?(:show?, job.creator, namespace: :Admin, ), "#{job.creator_id} - #{job.creator.name} (#{job.creator.email})", admin_user_path(job.creator) %>
<% else %> <% else %>
- -
<% end %> <% end %>
</p> </p>
<p class="my-3">
<strong class="mb-1 font-medium">Operator ID:</strong>
<% if job.operator %>
<%= link_to_if allowed_to?(:show?, job.operator, namespace: :Admin), "#{job.operator_id} - #{job.operator.name} (#{job.operator.email})", admin_user_path(job.operator) %>
<% else %>
-
<% end %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Cashier ID:</strong>
<% if job.cashier %>
<%= link_to_if allowed_to?(:show?, job.cashier, namespace: :Admin), "#{job.cashier_id} - #{job.cashier.name} (#{job.cashier.email})", admin_user_path(job.cashier) %>
<% else %>
-
<% end %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Kunde:</strong>
<%= link_to_if job.costumer && allowed_to?(:show? , job.costumer, namespace: :Admin), "#{job.costumer_firstname} #{job.costumer_lastname}", admin_user_path(job.costumer) %>
</p>
<p class="my-3"> <p class="my-3">
<strong class="mb-1 font-medium">Opertator:</strong> <strong class="mb-1 font-medium">Opertator:</strong>
<%= link_to_if job.operator && allowed_to?(:show, job.operator, namespace: :Admin), "#{job.operator_firstname} #{job.operator_lastname}", admin_users_path(job.operator) %> <%= link_to_if job.operator && allowed_to?(:show, job.operator, namespace: :Admin), "#{job.operator_firstname} #{job.operator_lastname}", admin_users_path(job.operator) %>
</p> </p>
<p class="my-3">
<strong class="mb-1 font-medium">Kassierer:</strong>
<%= link_to_if job.cashier && allowed_to?(:show, job.cashier, namespace: :Admin), "#{job.cashier_firstname} #{job.cashier_lastname}", admin_users_path(job.cashier) %>
</p>
<p class="my-3"> <p class="my-3">
<strong class="mb-1 font-medium">Erstellt durch Opertator:</strong> <strong class="mb-1 font-medium">Erstellt durch Opertator:</strong>
<%= icon bool_icon(job.created_by_operator), class: "icon #{job.created_by_operator ? "text-green-600" : "text-red-600"}" %> <%= icon bool_icon(job.created_by_operator), class: "icon #{job.created_by_operator ? "text-green-600" : "text-red-600"}" %>
</p> </p>
<p class="my-3">
<strong class="mb-1 font-medium">Auftraggeber:</strong>
<%= link_to_if job.costumer && !job.created_by_operator && allowed_to?(:show? , job.costumer, namespace: :Admin), "#{job.costumer_firstname} #{job.costumer_lastname}", admin_user_path(job.costumer) %>
</p>
<p class="my-3"> <p class="my-3">
<strong class="mb-1 font-medium">Aktueller Status:</strong> <strong class="mb-1 font-medium">Aktueller Status:</strong>
<%= %> <span class="badge badge-status inline text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light">
<%= job.status %>
</span>
</p> </p>
<p class="my-3"> <p class="my-3">
<strong class="mb-1 font-medium">Paid at:</strong> <strong class="mb-1 font-medium">Paid at:</strong>

View File

@@ -1,17 +1,19 @@
class CreateJobs < ActiveRecord::Migration[7.1] class CreateJobs < ActiveRecord::Migration[7.1]
def change def change
create_table :jobs do |t| create_table :jobs do |t|
t.references :operator, null: true
t.references :costumer, null: true t.references :costumer, null: true
t.string :operator_firstname t.references :creator, null: true
t.string :operator_lastname t.references :cashier, null: true
t.references :operator, null: true
t.string :costumer_firstname t.string :costumer_firstname
t.string :costumer_lastname t.string :costumer_lastname
t.boolean :printed, default: false t.string :operator_firstname
t.boolean :paid, default: false t.string :operator_lastname
t.string :cashier_firstname
t.string :cashier_lastname
t.datetime :printed_at t.datetime :printed_at
t.datetime :status_changed_at
t.datetime :paid_at t.datetime :paid_at
t.datetime :status_changed_at
t.boolean :intern, default: false t.boolean :intern, default: false
t.string :cost_center t.string :cost_center
t.string :status, default: "open", index: true t.string :status, default: "open", index: true

View File

@@ -12,10 +12,14 @@ class CreateUsers < ActiveRecord::Migration[7.2]
t.integer :jobs_as_costumer_count, default: 0 t.integer :jobs_as_costumer_count, default: 0
t.integer :jobs_as_operator_count, default: 0 t.integer :jobs_as_operator_count, default: 0
t.integer :created_jobs_count, default: 0
t.integer :cashed_jobs_count, default: 0
t.timestamps t.timestamps
end end
add_foreign_key :jobs, :users, column: :operator_id
add_foreign_key :jobs, :users, column: :costumer_id add_foreign_key :jobs, :users, column: :costumer_id
add_foreign_key :jobs, :users, column: :operator_id
add_foreign_key :jobs, :users, column: :creator_id
add_foreign_key :jobs, :users, column: :cashier_id
end end
end end

20
db/schema.rb generated
View File

@@ -40,17 +40,19 @@ ActiveRecord::Schema[7.2].define(version: 2024_08_26_144016) do
end end
create_table "jobs", force: :cascade do |t| create_table "jobs", force: :cascade do |t|
t.integer "operator_id"
t.integer "costumer_id" t.integer "costumer_id"
t.string "operator_firstname" t.integer "creator_id"
t.string "operator_lastname" t.integer "cashier_id"
t.integer "operator_id"
t.string "costumer_firstname" t.string "costumer_firstname"
t.string "costumer_lastname" t.string "costumer_lastname"
t.boolean "printed", default: false t.string "operator_firstname"
t.boolean "paid", default: false t.string "operator_lastname"
t.string "cashier_firstname"
t.string "cashier_lastname"
t.datetime "printed_at" t.datetime "printed_at"
t.datetime "status_changed_at"
t.datetime "paid_at" t.datetime "paid_at"
t.datetime "status_changed_at"
t.boolean "intern", default: false t.boolean "intern", default: false
t.string "cost_center" t.string "cost_center"
t.string "status", default: "open" t.string "status", default: "open"
@@ -65,7 +67,9 @@ ActiveRecord::Schema[7.2].define(version: 2024_08_26_144016) do
t.boolean "created_by_operator", default: false t.boolean "created_by_operator", default: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["cashier_id"], name: "index_jobs_on_cashier_id"
t.index ["costumer_id"], name: "index_jobs_on_costumer_id" t.index ["costumer_id"], name: "index_jobs_on_costumer_id"
t.index ["creator_id"], name: "index_jobs_on_creator_id"
t.index ["operator_id"], name: "index_jobs_on_operator_id" t.index ["operator_id"], name: "index_jobs_on_operator_id"
t.index ["status"], name: "index_jobs_on_status" t.index ["status"], name: "index_jobs_on_status"
end end
@@ -88,6 +92,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_08_26_144016) do
t.boolean "verified", default: false, null: false t.boolean "verified", default: false, null: false
t.integer "jobs_as_costumer_count", default: 0 t.integer "jobs_as_costumer_count", default: 0
t.integer "jobs_as_operator_count", default: 0 t.integer "jobs_as_operator_count", default: 0
t.integer "created_jobs_count", default: 0
t.integer "cashed_jobs_count", default: 0
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
@@ -96,7 +102,9 @@ ActiveRecord::Schema[7.2].define(version: 2024_08_26_144016) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "jobs", "users", column: "cashier_id"
add_foreign_key "jobs", "users", column: "costumer_id" add_foreign_key "jobs", "users", column: "costumer_id"
add_foreign_key "jobs", "users", column: "creator_id"
add_foreign_key "jobs", "users", column: "operator_id" add_foreign_key "jobs", "users", column: "operator_id"
add_foreign_key "sessions", "users" add_foreign_key "sessions", "users"
end end

View File

@@ -15,8 +15,9 @@ User.create!(email: "david.boehm@hs-rm.de", firstname: "David", lastname: "Böhm
User.create!(email: "maximilian.lasser@hs-rm.de", firstname: "Max", lastname: "Lasser", role: :admin, password_digest: BCrypt::Password.create("admin"), verified: true) User.create!(email: "maximilian.lasser@hs-rm.de", firstname: "Max", lastname: "Lasser", role: :admin, password_digest: BCrypt::Password.create("admin"), verified: true)
# Operators # Operators
User.create!(email: "tutor.operator@hs-rm.de", firstname: "Tutor", lastname: "Operator", role: :operator, password_digest: BCrypt::Password.create("operator"), verified: true) operators = []
User.create!(email: "tutor2.operator@hs-rm.de", firstname: "Tutor2", lastname: "Operator", role: :operator, password_digest: BCrypt::Password.create("operator"), verified: true) operators << User.create!(email: "tutor.operator@hs-rm.de", firstname: "Tutor", lastname: "Operator", role: :operator, password_digest: BCrypt::Password.create("operator"), verified: true)
operators << User.create!(email: "tutor2.operator@hs-rm.de", firstname: "Tutor2", lastname: "Operator", role: :operator, password_digest: BCrypt::Password.create("operator"), verified: true)
# Students # Students
User.create!(email: "stud.student@student.hs-rm.de", firstname: "Student", lastname: "Student", password_digest: BCrypt::Password.create("stud"), verified: true) User.create!(email: "stud.student@student.hs-rm.de", firstname: "Student", lastname: "Student", password_digest: BCrypt::Password.create("stud"), verified: true)
@@ -54,7 +55,7 @@ end
# created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today, period: :day) # created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today, period: :day)
email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue').gsub('ß', 'ss') email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue').gsub('ß', 'ss')
email.delete(" ") email.delete(" ")
user=User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: false, created_at: created_at).save! User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: false, created_at: created_at).save!
end end
# Jobs paid (and some canceled) in the far past # Jobs paid (and some canceled) in the far past
@@ -76,7 +77,12 @@ end
end end
job = Job.new(status:, privacy_policy: true, created_at: created_at) job = Job.new(status:, privacy_policy: true, created_at: created_at)
job.pdf = File.open(Rails.root.join('db/pdfs/', pdf)) job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
job.costumer = students[rand(0...9)] student = students[rand(0..9)]
job.costumer = student
job.creator = student
operator = operators[rand(0...1)]
job.operator = operator if status != :open
job.cashier = operator if status == :paid
job.save! job.save!
job.update_column :printed_at, printed_at # write with update_column to avoid before_save action job.update_column :printed_at, printed_at # write with update_column to avoid before_save action
job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action
@@ -105,7 +111,12 @@ end
end end
job = Job.new(status:, privacy_policy: true, created_at: created_at) job = Job.new(status:, privacy_policy: true, created_at: created_at)
job.pdf = File.open(Rails.root.join('db/pdfs/', pdf)) job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
job.costumer = students[rand(0...9)] student = students[rand(0...9)]
job.costumer = student
job.creator = student
operator = operators[rand(0...1)]
job.operator = operator if status == :paid
job.cashier = operator if status == :paid
job.save! job.save!
job.update_column :printed_at, printed_at # write with update_column to avoid before_save action job.update_column :printed_at, printed_at # write with update_column to avoid before_save action
job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action
@@ -125,7 +136,28 @@ end
status = :open if i > 0 status = :open if i > 0
job = Job.new(status:, privacy_policy: true) job = Job.new(status:, privacy_policy: true)
job.pdf = File.open(Rails.root.join('db/pdfs/', pdf)) job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
job.costumer = students[rand(0...4)] student = students[rand(0...4)]
job.costumer = student
job.creator = student
operator = operators[rand(0...1)]
job.operator = operator if status != :open
job.cashier = operator if status == :paid
job.save! job.save!
end end
end end
# Jobs created from operator
[ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf',
'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf',
'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf',
'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf|
job = Job.new(privacy_policy: true)
job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
job.costumer = students[rand(0...9)]
job.costumer_firstname = job.costumer.firstname
job.costumer_lastname = job.costumer.lastname
job.creator = operators[rand(0...1)]
job.created_by_operator = true
job.inspect
job.save!
end