Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CatalinVoineag committed Dec 21, 2023
1 parent f3d22de commit 49d8532
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/controllers/claims/organisations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Claims::OrganisationsController < ApplicationController
def index
@schools = current_user.schools
end
end
6 changes: 6 additions & 0 deletions app/controllers/placements/organisations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Placements::OrganisationsController < ApplicationController
def index
@schools = current_user.schools
@providers = current_user.providers
end
end
12 changes: 11 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def callback
if current_user
# DfESignInUsers::Update.call(user: current_user, sign_in_user: sign_in_user)

redirect_to(root_path)
redirect_to after_sign_in_path
else
# session.delete(:requested_path)
DfESignInUser.end_session!(session)
Expand All @@ -19,4 +19,14 @@ def signout
DfESignInUser.end_session!(session)
redirect_to root_path
end

private

def after_sign_in_path
if current_user.memberships.count > 1
public_send("#{current_service}_organisations_path")
else
root_path
end
end
end
24 changes: 24 additions & 0 deletions app/models/membership.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: memberships
#
# id :uuid not null, primary key
# organisation_type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organisation_id :uuid not null
# user_id :uuid not null
#
# Indexes
#
# index_memberships_on_organisation (organisation_type,organisation_id)
# index_memberships_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
class Membership < ApplicationRecord
belongs_to :user
belongs_to :organisation, polymorphic: true
end
6 changes: 5 additions & 1 deletion app/models/school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
# index_schools_on_urn (urn) UNIQUE
#
class School < ApplicationRecord
has_one :gias_school, foreign_key: :urn, primary_key: :urn
belongs_to :gias_school, foreign_key: :urn, primary_key: :urn
has_many :memberships, as: :organisation

validates :urn, presence: true
validates :urn, uniqueness: { case_sensitive: false }

delegate :name, to: :gias_school

scope :placements, -> { where placements: true }
scope :claims, -> { where claims: true }
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
# index_users_on_service_and_email (service,email) UNIQUE
#
class User < ApplicationRecord
has_many :memberships
has_many :schools, through: :memberships, source: :organisation, source_type: "School"
has_many :providers, through: :memberships, source: :organisation, source_type: "Provider"

enum :service,
{ no_service: "no_service", claims: "claims", placements: "placements" },
validate: true
Expand Down
15 changes: 15 additions & 0 deletions app/views/claims/organisations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl"><%= t("organisations") %></h1>

<% if @schools.any? %>
<ul class="govuk-list">
<% @schools.each do |school| %>
<li>
<%= govuk_link_to school.name, request.env["PATH_INFO"], class: "govuk-link--no-visited-state" %>
</li>
<% end %>
</ul>
<% end %>
</div>
</div>
27 changes: 27 additions & 0 deletions app/views/placements/organisations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl"><%= t("organisations") %></h1>

<% if @schools.any? %>
<h2 class="govuk-heading-l"><%= t("schools") %></h2>
<ul class="govuk-list">
<% @schools.each do |school| %>
<li>
<%= govuk_link_to school.name, request.env["PATH_INFO"], class: "govuk-link--no-visited-state" %>
</li>
<% end %>
</ul>
<% end %>

<% if @providers.any? %>
<h2 class="govuk-heading-l"><%= t("providers") %></h2>
<ul class="govuk-list">
<% @providers.each do |provider| %>
<li>
<%= govuk_link_to provider.code, request.env["PATH_INFO"], class: "govuk-link--no-visited-state" %>
</li>
<% end %>
</ul>
<% end %>
</div>
</div>
6 changes: 6 additions & 0 deletions config/initializers/school.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SCHOOLS = [
{ claims: true },
{ placements: true },
{ claims: true, placements: true },
{ claims: true, placements: true }
].freeze
2 changes: 2 additions & 0 deletions config/routes/claims.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
scope module: :claims, as: :claims, constraints: { host: ENV["CLAIMS_HOST"] } do
root to: "pages#index"

resources :organisations, only: [:index]
end
2 changes: 2 additions & 0 deletions config/routes/placements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
host: ENV["PLACEMENTS_HOST"]
} do
root to: "pages#index"

resources :organisations, only: [:index]
end
10 changes: 10 additions & 0 deletions db/migrate/20231220143008_create_memberships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateMemberships < ActiveRecord::Migration[7.1]
def change
create_table :memberships, id: :uuid do |t|
t.references :user, null: false, foreign_key: true, type: :uuid
t.references :organisation, polymorphic: true, null: false, type: :uuid

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_12_16_102842) do
ActiveRecord::Schema[7.1].define(version: 2023_12_20_143008) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -41,6 +41,16 @@
t.index ["urn"], name: "index_gias_schools_on_urn", unique: true
end

create_table "memberships", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "user_id", null: false
t.string "organisationable_type", null: false
t.uuid "organisationable_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organisationable_type", "organisationable_id"], name: "index_memberships_on_organisationable"
t.index ["user_id"], name: "index_memberships_on_user_id"
end

create_table "providers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "provider_code", null: false
t.datetime "created_at", null: false
Expand Down Expand Up @@ -70,4 +80,5 @@
t.index ["service", "email"], name: "index_users_on_service_and_email", unique: true
end

add_foreign_key "memberships", "users"
end
24 changes: 24 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,27 @@
end

Rails.logger.debug "Personas successfully created!"

Rake::Task['gias_update'].invoke unless GiasSchool.any?
gias_scools = GiasSchool.last(SCHOOLS.count)

SCHOOLS.each.with_index do |school_attributes, index|
school = School.find_or_initialize_by(**school_attributes)
school.urn = gias_scools[index].urn

school.save!
end

User.where(first_name: ["Anne", "Patricia"]).each do |user|
school = School.public_send(user.service).first
user.memberships.find_or_create_by!(organisation: school)
end

User.where(first_name: ["Mary", "Colin"]).each do |user|
schools = School.where("#{user.service}": true)

schools.each do |school|
user.memberships.find_or_create_by!(organisation: school)
end
end

25 changes: 25 additions & 0 deletions spec/factories/memberships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == Schema Information
#
# Table name: memberships
#
# id :uuid not null, primary key
# organisation_type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organisation_id :uuid not null
# user_id :uuid not null
#
# Indexes
#
# index_memberships_on_organisation (organisation_type,organisation_id)
# index_memberships_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
FactoryBot.define do
factory :membership do

end
end
25 changes: 25 additions & 0 deletions spec/models/membership_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == Schema Information
#
# Table name: memberships
#
# id :uuid not null, primary key
# organisation_type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organisation_id :uuid not null
# user_id :uuid not null
#
# Indexes
#
# index_memberships_on_organisation (organisation_type,organisation_id)
# index_memberships_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
require 'rails_helper'

RSpec.describe Membership, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 49d8532

Please sign in to comment.