sis_core 1.0.3 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/sis/core/application_controller.rb +10 -2
  3. data/app/controllers/sis/core/menus_controller.rb +22 -0
  4. data/app/controllers/sis/core/users_controller.rb +71 -0
  5. data/app/models/sis/core/academic_programme.rb +17 -0
  6. data/app/models/sis/core/academic_year.rb +1 -0
  7. data/app/models/sis/core/applicant.rb +18 -1
  8. data/app/models/sis/core/application.rb +50 -0
  9. data/app/models/sis/core/application_document.rb +12 -0
  10. data/app/models/sis/core/application_module.rb +8 -0
  11. data/app/models/sis/core/application_setup.rb +23 -0
  12. data/app/models/sis/core/document_requirement.rb +13 -0
  13. data/app/models/sis/core/{program_type.rb → document_type.rb} +1 -1
  14. data/app/models/sis/core/exam.rb +17 -0
  15. data/app/models/sis/core/exam_result.rb +25 -0
  16. data/app/models/sis/core/exam_setup.rb +15 -0
  17. data/app/models/sis/core/menu.rb +11 -0
  18. data/app/models/sis/core/programme.rb +7 -0
  19. data/app/models/sis/core/programme_level.rb +6 -0
  20. data/app/models/sis/core/programme_type.rb +6 -0
  21. data/app/models/sis/core/semester.rb +4 -0
  22. data/app/models/sis/core/user.rb +13 -0
  23. data/app/models/sis/core/user_role.rb +10 -0
  24. data/app/services/sis/core/user_service.rb +25 -0
  25. data/app/uploaders/sis/core/application_document_uploader.rb +51 -0
  26. data/app/uploaders/sis/core/photo_uploader.rb +51 -0
  27. data/config/routes.rb +6 -0
  28. data/db/migrate/{20200307072142_create_sis_core_academic_years.rb → 20200105072142_create_sis_core_academic_years.rb} +2 -1
  29. data/db/migrate/{20200307072246_create_sis_core_semesters.rb → 20200106072246_create_sis_core_semesters.rb} +0 -0
  30. data/db/migrate/20200106130428_create_sis_core_programmes.rb +10 -0
  31. data/db/migrate/20200106132035_create_sis_core_academic_programmes.rb +21 -0
  32. data/db/migrate/20200106132521_create_sis_core_application_setups.rb +19 -0
  33. data/db/migrate/20200106133034_create_sis_core_document_requirements.rb +19 -0
  34. data/db/migrate/20200106133937_create_sis_core_applicants.rb +14 -2
  35. data/db/migrate/20200107094449_create_sis_core_applications.rb +24 -0
  36. data/db/migrate/20200107101638_create_sis_core_application_documents.rb +16 -0
  37. data/db/migrate/20200107302251_create_sis_core_exam_setups.rb +15 -0
  38. data/db/migrate/20200107333411_create_sis_core_exams.rb +17 -0
  39. data/db/migrate/20200107434658_create_sis_core_exam_results.rb +18 -0
  40. data/db/migrate/20200307033100_create_sis_core_courses.rb +2 -2
  41. data/db/migrate/20200307072006_create_sis_core_students.rb +2 -2
  42. data/db/migrate/20200507114003_create_sis_core_users.rb +17 -0
  43. data/db/migrate/20200511165121_create_sis_core_application_modules.rb +15 -0
  44. data/db/migrate/20200511165241_create_sis_core_user_roles.rb +15 -0
  45. data/db/migrate/20200511165450_create_sis_core_menus.rb +18 -0
  46. data/lib/authorize_request.rb +18 -0
  47. data/lib/json_web_token.rb +12 -0
  48. data/lib/sis/core/engine.rb +2 -2
  49. data/lib/sis/core/version.rb +1 -1
  50. data/lib/sis_core.rb +5 -0
  51. data/spec/factories/sis/core/academic_programmes.rb +8 -0
  52. data/spec/factories/sis/core/academic_years.rb +3 -2
  53. data/spec/factories/sis/core/applicants.rb +9 -2
  54. data/spec/factories/sis/core/application_documents.rb +7 -0
  55. data/spec/factories/sis/core/application_modules.rb +6 -0
  56. data/spec/factories/sis/core/application_setups.rb +9 -0
  57. data/spec/factories/sis/core/applications.rb +11 -0
  58. data/spec/factories/sis/core/document_requirements.rb +9 -0
  59. data/spec/factories/sis/core/document_types.rb +5 -0
  60. data/spec/factories/sis/core/exam_results.rb +8 -0
  61. data/spec/factories/sis/core/exam_setups.rb +8 -0
  62. data/spec/factories/sis/core/exams.rb +10 -0
  63. data/spec/factories/sis/core/instructor_courses.rb +2 -2
  64. data/spec/factories/sis/core/instructors.rb +2 -2
  65. data/spec/factories/sis/core/menus.rb +9 -0
  66. data/spec/factories/sis/core/programme_levels.rb +5 -0
  67. data/spec/factories/sis/core/programme_types.rb +5 -0
  68. data/spec/factories/sis/core/programmes.rb +6 -0
  69. data/spec/factories/sis/core/semesters.rb +1 -1
  70. data/spec/factories/sis/core/students.rb +5 -5
  71. data/spec/factories/sis/core/user_roles.rb +6 -0
  72. data/spec/factories/sis/core/users.rb +9 -0
  73. metadata +150 -29
  74. data/app/models/sis/core/attached_document.rb +0 -8
  75. data/app/models/sis/core/enrollment_application.rb +0 -9
  76. data/app/models/sis/core/program.rb +0 -9
  77. data/app/models/sis/core/staff.rb +0 -21
  78. data/app/models/user.rb +0 -12
  79. data/config/initializers/devise_token_auth.rb +0 -55
  80. data/db/migrate/20200106001016_create_sis_core_field_of_studies.rb +0 -9
  81. data/db/migrate/20200106130428_create_sis_core_programs.rb +0 -10
  82. data/db/migrate/20200108092202_devise_token_auth_create_users.rb +0 -53
  83. data/db/migrate/20200108094449_create_sis_core_enrollment_applications.rb +0 -10
  84. data/db/migrate/20200108101638_create_sis_core_attached_documents.rb +0 -11
  85. data/db/migrate/20200316120805_create_sis_core_staffs.rb +0 -31
  86. data/db/migrate/20200319074334_modify_user.rb +0 -16
  87. data/spec/factories/sis/core/attached_documents.rb +0 -7
  88. data/spec/factories/sis/core/enrollment_applications.rb +0 -7
  89. data/spec/factories/sis/core/field_of_studies.rb +0 -6
  90. data/spec/factories/sis/core/program_types.rb +0 -5
  91. data/spec/factories/sis/core/programs.rb +0 -7
  92. data/spec/factories/sis/core/staffs.rb +0 -32
  93. data/spec/factories/users.rb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d6d06580eeab8666073ed41a67aec635989681c89ac979d8303ec85e0adf15f
4
- data.tar.gz: 2f34eb2200e4a15dc40775f8c1fac1006ec1b588be232f6d10b50483dec87d01
3
+ metadata.gz: c36e2c8bba7e35681f66af9d69533eb938c804eebb4317f2994f71ce015f4a22
4
+ data.tar.gz: 201448c119d21219b68094ade5aa46f920d4006a34c21c72ad3d62b4f5dd3980
5
5
  SHA512:
6
- metadata.gz: a4916c93eaf0adab13edd713779386a00ceacca0618e19a5fe56841cebda7d8dedafdd270956471d740ea884c5841f24dbe812f336fd6f30b6656ed4045baaed
7
- data.tar.gz: 3d7d5c4deb881a3f94d708176af6d3847bea0c675f02cd93590ddbde8ebb6354416c31ff05625fc89044c096edb8a6fbeeab74f04144410f9aad4b234aaada7e
6
+ metadata.gz: b0892082b1f456a1c0e084b5d624481a5d339f66c9d3c2f272960ce5ba095efd08e77e15e9c1f9f978bb0436db5439b52c323363be45443dcbaa550a6369b053
7
+ data.tar.gz: c9d0b2302de92f151af251ab32bf7f0701247572a5e2466624d40e9e76a40c4d973914bf4d0097a5af769483113da873f5dfdf1ad2a2e2e80d52d3046020b091
@@ -1,7 +1,15 @@
1
1
  module Sis
2
2
  module Core
3
- class ApplicationController < ActionController::Base
4
- protect_from_forgery with: :exception
3
+ class ApplicationController < ActionController::API
4
+ before_action :authenticate_request
5
+ attr_reader :current_user
6
+
7
+ private
8
+
9
+ def authenticate_request
10
+ @current_user = AuthorizeRequest.user(request.headers)
11
+ render json: { error: 'Not Authorized' }, status: 401 unless @current_user
12
+ end
5
13
  end
6
14
  end
7
15
  end
@@ -0,0 +1,22 @@
1
+ module Sis
2
+ module Core
3
+ class MenusController < ApplicationController
4
+ skip_before_action :authenticate_request, only: [:menu]
5
+ def menu
6
+ app_module_id = ApplicationModule.find_by_code(params[:app_module]).id
7
+ user_roles = User.find(params[:user_id]).user_roles.where(application_module_id: app_module_id)
8
+ menus = []
9
+ user_roles.each do |role|
10
+ user_menus = role.menus.where(parent_id: nil, application_module_id: app_module_id)
11
+ user_menus.each do |user_menu|
12
+ children = user_menu.children.where(parent_id: user_menu.id, application_module_id: app_module_id)
13
+ children = children.map { |c| { label: c.text, icon: c.icon_cls, routerLink: [c.location] } }
14
+ menus.push({ label: user_menu.text, icon: user_menu.icon_cls, location: user_menu.location,
15
+ items: children })
16
+ end
17
+ end
18
+ render json: { success: true, data: menus }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,71 @@
1
+ module Sis
2
+ module Core
3
+ class UsersController < ApplicationController
4
+ skip_before_action :authenticate_request, only: %i[login sign_in generate_recovery_token reset_password]
5
+ before_action :set_user_service, only: %i[generate_recovery_token reset_password]
6
+ def sign_in
7
+ applicant = Applicant&.find_by_email(params[:email])
8
+ if Applicant&.find_by_email(params[:email]) && applicant.authenticate(params[:password])
9
+ token = JsonWebToken.encode({ id: applicant.id, first_name: applicant.first_name,
10
+ middle_name: applicant.middle_name, last_name: applicant.last_name,
11
+ email: applicant.email })
12
+ render json: { success: true, jwt: token }
13
+ else
14
+ render json: { success: false, errors: ['Invalid username or password !'] }
15
+ end
16
+ end
17
+
18
+ def create
19
+ user = User.new(user_params)
20
+ if user.save
21
+ render json: { success: true, data: user }
22
+ else
23
+ render json: { success: false, errors: user.errors }
24
+ end
25
+ end
26
+
27
+ def login
28
+ user = User.find_by_email(params[:email])
29
+ if User&.find_by_email(params[:email]) && user.authenticate(params[:password])
30
+ token = JsonWebToken.encode({ id: user.id, first_name: user.first_name, last_name: user.last_name })
31
+ render json: { success: true, jwt: token }
32
+ else
33
+ render json: { success: false, errors: ['Invalid username or password !'] }
34
+ end
35
+ end
36
+
37
+ def generate_recovery_token
38
+ user = User.where(email: params[:email], secret_question_answer: params[:secret_question_answer])
39
+ if user.count.positive?
40
+ user = user[0]
41
+ @service.generate_password_token(user)
42
+ render json: { success: true, token: user.reset_password_token, email: user.email }
43
+ else
44
+ render json: { success: false, errors: ['User not found !'] }
45
+ end
46
+ end
47
+
48
+ def reset_password
49
+ token = params[:token].to_s
50
+ user = User.find_by_reset_password_token(token)
51
+ if user && @service.password_token_valid?(user.id)
52
+ @service.reset_password(user.id, params[:password])
53
+ render json: { success: true }
54
+ else
55
+ render json: { success: false, errors: ['Link not valid or expired. Try generating a new link !'] }
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def user_params
62
+ params.require('user').permit(:id, :first_name, :last_name, :email, :password, :reset_password_token,
63
+ :reset_password_sent_at, :secret_question, :secret_question_answer)
64
+ end
65
+
66
+ def set_user_service
67
+ @service = UserService.new
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,17 @@
1
+ module Sis
2
+ module Core
3
+ class AcademicProgramme < ApplicationRecord
4
+ belongs_to :programme
5
+ belongs_to :programme_type
6
+ belongs_to :programme_level
7
+
8
+ delegate(:name, to: :programme, prefix: true, allow_nil: false)
9
+ delegate(:name, to: :programme_type, prefix: true, allow_nil: false)
10
+ delegate(:name, to: :programme_level, prefix: true, allow_nil: false)
11
+
12
+ def name
13
+ programme.name.to_s + ' ' + programme_level.name.to_s + ' ' + programme_type.name.to_s
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,6 +1,7 @@
1
1
  module Sis
2
2
  module Core
3
3
  class AcademicYear < ApplicationRecord
4
+ validates :year, presence: true, uniqueness: { case_sensitive: false }
4
5
  end
5
6
  end
6
7
  end
@@ -1,8 +1,25 @@
1
1
  module Sis
2
2
  module Core
3
3
  class Applicant < ApplicationRecord
4
- validates :first_name, :middle_name, :last_name, :email, :phone_number, presence: true
4
+ mount_base64_uploader :photo, PhotoUploader
5
+ validates :first_name, :middle_name, :last_name, :email, presence: true
5
6
  validates :email, uniqueness: true
7
+
8
+ has_many :applications
9
+
10
+ has_secure_password
11
+
12
+ def full_name
13
+ first_name + ' ' + middle_name + ' ' + last_name
14
+ end
15
+
16
+ scope :by_application_setup, (lambda do |app_setup_id|
17
+ joins(applications: :application_setup).where('sis_core_applications.application_setup_id': app_setup_id)
18
+ end)
19
+
20
+ scope :by_status, (lambda do |status|
21
+ joins(:applications).where('sis_core_applications.status': status)
22
+ end)
6
23
  end
7
24
  end
8
25
  end
@@ -0,0 +1,50 @@
1
+ module Sis
2
+ module Core
3
+ class Application < ApplicationRecord
4
+ include AASM
5
+
6
+ # Gender types
7
+ MALE = 'Male'.freeze
8
+ FEMALE = 'Female'.freeze
9
+
10
+ aasm column: :status do
11
+ state :draft, initial: true
12
+ state :submitted, :under_review, :accepted, :rejected, :selected_for_exam, :selected_for_admission
13
+
14
+ event :submit do
15
+ transitions from: :draft, to: :submitted
16
+ end
17
+
18
+ event :review do
19
+ transitions from: %i[submitted rejected accepted], to: :under_review
20
+ end
21
+
22
+ event :accept do
23
+ transitions from: :under_review, to: :accepted
24
+ end
25
+
26
+ event :reject do
27
+ transitions from: :under_review, to: :rejected
28
+ end
29
+
30
+ event :select_for_exam do
31
+ transitions from: :accepted, to: :selected_for_exam
32
+ end
33
+
34
+ event :select_for_admission do
35
+ transitions from: :selected_for_exam, to: :selected_for_admission
36
+ end
37
+ end
38
+
39
+ validates :status, presence: true
40
+ validates :payment_information, presence: true, if: :payment_made
41
+
42
+ belongs_to :applicant
43
+ belongs_to :academic_programme
44
+ belongs_to :application_setup
45
+
46
+ delegate(:name, to: :academic_programme, prefix: true, allow_nil: false)
47
+ delegate(:full_name, to: :applicant, prefix: true, allow_nil: false)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,12 @@
1
+ module Sis
2
+ module Core
3
+ class ApplicationDocument < ApplicationRecord
4
+ mount_base64_uploader :document, ApplicationDocumentUploader
5
+
6
+ belongs_to :application
7
+ belongs_to :document_requirement
8
+
9
+ delegate(:name, to: :document_requirement, prefix: true, allow_nil: false)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Sis
2
+ module Core
3
+ class ApplicationModule < ApplicationRecord
4
+ has_and_belongs_to_many :users
5
+ validates :code, :name, presence: true
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module Sis
2
+ module Core
3
+ class ApplicationSetup < ApplicationRecord
4
+ OPEN = 'Open'.freeze
5
+ CLOSED = 'Closed'.freeze
6
+
7
+ belongs_to :academic_programme
8
+ belongs_to :target_semester, class_name: 'Sis::Core::Semester'
9
+ has_many :document_requirements
10
+
11
+ validates :start_date, :end_date, :status, presence: true
12
+ validates :status, inclusion: [OPEN, CLOSED]
13
+
14
+ delegate(:name, to: :target_semester, prefix: true, allow_nil: false)
15
+ delegate(:name, to: :academic_programme, prefix: true, allow_nil: false)
16
+
17
+ def name
18
+ academic_programme.programme.name.to_s + ' ' + academic_programme.programme_level.name.to_s + ' ' +
19
+ academic_programme.programme_type.name.to_s
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Sis
2
+ module Core
3
+ class DocumentRequirement < ApplicationRecord
4
+ belongs_to :document_type
5
+ belongs_to :application_setup
6
+
7
+ validates :original, :copy, presence: true
8
+
9
+ delegate(:name, to: :document_type, prefix: true, allow_nil: false)
10
+ delegate(:name, to: :application_setup, prefix: true, allow_nil: false)
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  module Sis
2
2
  module Core
3
- class ProgramType < Lookup
3
+ class DocumentType < Lookup
4
4
  end
5
5
  end
6
6
  end
@@ -0,0 +1,17 @@
1
+ module Sis
2
+ module Core
3
+ class Exam < ApplicationRecord
4
+ belongs_to :exam_setup
5
+
6
+ validates :title, :start, :finish, :pass_point, :points, presence: true
7
+
8
+ before_validation :set_pass_point
9
+
10
+ def set_pass_point
11
+ self.pass_point = points / 2 unless pass_point || !points
12
+ end
13
+
14
+ delegate(:name, to: :exam_setup, prefix: true, allow_nil: false)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ module Sis
2
+ module Core
3
+ class ExamResult < ApplicationRecord
4
+ PASS = 'Pass'.freeze
5
+ FAIL = 'Fail'.freeze
6
+
7
+ belongs_to :exam
8
+ belongs_to :application
9
+
10
+ validates :points, presence: true
11
+ validates_uniqueness_of :application_id, scope: %i[exam_id]
12
+
13
+
14
+ before_validation :set_status
15
+
16
+ def set_status
17
+ self.status = if exam && exam.pass_point >= points
18
+ PASS
19
+ else
20
+ FAIL
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ module Sis
2
+ module Core
3
+ class ExamSetup < ApplicationRecord
4
+ belongs_to :application_setup
5
+
6
+ validates :date, :hours, :venue, presence: true
7
+
8
+ def name
9
+ application_setup.academic_programme.name
10
+ end
11
+
12
+ delegate(:name, to: :application_setup, prefix: true, allow_nil: false)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Sis
2
+ module Core
3
+ class Menu < ApplicationRecord
4
+ has_and_belongs_to_many :user_roles
5
+ belongs_to :application_module
6
+ belongs_to :parent, class_name: 'Sis::Core::Menu', optional: true
7
+ has_many :children, class_name: 'Sis::Core::Menu', foreign_key: 'parent_id'
8
+ validates :text, presence: true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Sis
2
+ module Core
3
+ class Programme < ApplicationRecord
4
+ validates :code, :name, presence: true, uniqueness: true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Sis
2
+ module Core
3
+ class ProgrammeLevel < Lookup
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Sis
2
+ module Core
3
+ class ProgrammeType < Lookup
4
+ end
5
+ end
6
+ end
@@ -2,6 +2,10 @@ module Sis
2
2
  module Core
3
3
  class Semester < ApplicationRecord
4
4
  belongs_to :academic_year
5
+
6
+ validates :name, presence: true
7
+
8
+ delegate(:label, to: :academic_year, prefix: true, allow_nil: false)
5
9
  end
6
10
  end
7
11
  end
@@ -0,0 +1,13 @@
1
+ module Sis
2
+ module Core
3
+ class User < ApplicationRecord
4
+ validates :first_name, :last_name, :email, presence: true
5
+ validates :password, presence: true, on: :create
6
+ validates :email, uniqueness: true
7
+ has_and_belongs_to_many :application_modules
8
+ has_and_belongs_to_many :user_roles
9
+
10
+ has_secure_password
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Sis
2
+ module Core
3
+ class UserRole < ApplicationRecord
4
+ validates :name, presence: true
5
+ has_and_belongs_to_many :users
6
+ has_and_belongs_to_many :menus
7
+ belongs_to :application_module
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module Sis
2
+ module Core
3
+ class UserService
4
+ def generate_password_token(user)
5
+ user.update({ reset_password_token: generate_token, reset_password_sent_at: Time.now.utc })
6
+ end
7
+
8
+ def password_token_valid?(user_id)
9
+ user = User.find(user_id)
10
+ (user.reset_password_sent_at + 4.hours) > Time.now.utc
11
+ end
12
+
13
+ def reset_password(user_id, password)
14
+ user = User.find(user_id)
15
+ user.reset_password_token = nil
16
+ user.password = password
17
+ user.save!
18
+ end
19
+
20
+ def generate_token
21
+ SecureRandom.hex(10)
22
+ end
23
+ end
24
+ end
25
+ end