CSC/ECE 517 Spring 2023 - E2330. Reimplement Invitation Controller
Project Overview
Expertiza Background
Expertiza is an online platform designed to facilitate peer review and submission of various types of academic work, including articles, codes, and websites. The application allows instructors to add assignments, grade them, and assign students to teams based on their topic preferences. The platform also enables students to provide feedback on each other's work through peer reviews, which can be helpful in improving the quality of their projects. Expertiza is backed by the National Science Foundation.
Problem Statement
The Invitation model and InvitationsController are responsible for managing invitations between users in a team-based assignment system. The system allows a student to invite another student to their team by creating a new Invitation record, which is associated with the inviting and invited students and the assignment they are working on. The invited user can accept, decline, or cancel the invitation, which updates the corresponding Invitation record and may trigger actions such as adding or removing users from teams. The InvitationsController includes methods for creating, accepting, declining, and canceling invitations, as well as checking the status of the user and team before sending or accepting invitations. The system also sends email notifications to invited users when a new invitation is sent. Overall, the Invitation model and InvitationsController are crucial for managing team membership and collaboration in a team-based assignment system. Instructor’s note: There is already a project to merge this controller with the join_team_requests_controller. You should start with the code they have written.
Objectives
1. Write RESTful endpoints of create, show, delete to invitations and approving new invitations, listing pending invitations, creating new invitations, and notifying students about new team formation invite.
2. Develop and implement methods in the InvitationsController that enable the following functionality:Create invitations, Accept invitations, Decline invitations, Cancel invitations, and Check user and team status before sending or accepting invitations.
3. To implement a user-invitation system that allows invited users to accept, decline, or cancel invitations, which will update the corresponding Invitation record and trigger actions such as adding or removing users from teams.
4. Implement an email notification system in the user-invitation system, which sends email notifications to invited users when a new invitation is sent.
5. Return proper status codes and proper validation for RESTful endpoints.
6. Write models and controllers such that they use modern approaches to writing Ruby code, including utilizing language features and adhering to best practices for object-oriented design.
7. Write proper Rspec tests for all APIs, models, controllers.
Development Strategy
We have tried to follow a Test Driven Development approach to implement both the Invitation model and the Invitation Controller. Firstly a failing test case was written and then the code was added such that the test which was failing will pass now.
Also, we have tried to follow the rails philosophy of "Fat models" We have tried to move as much business logic from the controller to the model as possible. This will also help keep the controller code clean and encourage code reusability.
Project Design and Implementation
Use Case Diagram
Class Diagram
Functionality
In this project, we aim to add the following functions:
1. Enable users to create a new team invitation.
2. Enable users to Accept or Reject invitations.
3. Enable users to Retract invitations.
4. Enable users to send email notifications to invited users.
5. Ensure that data is validated properly in all the above APIs.
6. Test cases for all the above.
Screen Shots
Send an invite
Accept an invite
Reject invite
Retract invite
We retracted invitation of angel and malka and added ofelia
Project Design
Model
W: waiting
A: accepted
R: rejected/declined
filename: invitation.rb
GitHub file Link to invitation.rb
class Invitation < ApplicationRecord after_initialize :set_defaults ACCEPT_STATUS = 'A'.freeze REJECT_STATUS = 'R'.freeze WAITING_STATUS = 'W'.freeze belongs_to :to_user, class_name: 'User', foreign_key: 'to_id', inverse_of: false belongs_to :from_user, class_name: 'User', foreign_key: 'from_id', inverse_of: false belongs_to :assignment, class_name: 'Assignment', foreign_key: 'assignment_id' validates :reply_status, presence: true, length: { maximum: 1 } validates_inclusion_of :reply_status, in: [ACCEPT_STATUS, REJECT_STATUS, WAITING_STATUS], allow_nil: false validates :assignment_id, uniqueness: { scope: %i[from_id to_id reply_status], message: 'You cannot have duplicate invitations' } validate :to_from_cant_be_same # validate if the to_id and from_id are same def to_from_cant_be_same return unless from_id == to_id errors.add(:from_id, 'to and from users should be different') end # Return a new invitation # params = :assignment_id, :to_id, :from_id, :reply_status def self.invitation_factory(params) Invitation.new(params) end # check if the user is invited def self.invited?(from_id, to_id, assignment_id) @invitations_count = Invitation.where(to_id:) .where(from_id:) .where(assignment_id:) .where(reply_status: WAITING_STATUS) .count @invitations_count.positive? end # send invite email def send_invite_email InvitationSentMailer.with(invitation: self) .send_invitation_email .deliver_later end # Remove all invites sent by a user for an assignment. def remove_users_sent_invites_for_assignment(user_id, assignment_id); end # After a users accepts an invite, the teams_users table needs to be updated. # NOTE: Depends on TeamUser model, which is not implemented yet. def update_users_topic_after_invite_accept(inviter_user_id, invited_user_id, assignment_id); end # This method handles all that needs to be done upon a user accepting an invitation. # First the users previous team is deleted if they were the only member of that # team and topics that the old team signed up for will be deleted. # Then invites the user that accepted the invite sent will be removed. # Last the users team entry will be added to the TeamsUser table and their assigned topic is updated def accept_invitation(logged_in_user) update(reply_status: ACCEPT_STATUS) end # This method handles all that needs to be done upon an user decline an invitation. def decline_invitation(logged_in_user) update(reply_status: REJECT_STATUS) end # This method handles all that need to be done upon an invitation retraction. def retract_invitation(logged_in_user) destroy end # This will override the default as_json method in the ApplicationRecord class and specify def as_json(options = {}) super(options.merge({ only: %i[id reply_status created_at updated_at], include: { assignment: { only: %i[id name] }, from_user: { only: %i[id name fullname email] }, to_user: { only: %i[id name fullname email] } } })).tap do |hash| end end def set_defaults self.reply_status ||= WAITING_STATUS end end
Helper Methods in Invitation model.
to_from_cant_be_same:
validate if the to_id and from_id are same i.e a users cannot send invitations to themselves.
self.invitation_factory(params):
This helper method will help in creating new Invitation with the given parameters. Custom parameters can be passed to create specific Invitation.
self.invited?(from_id, to_id, assignment_id):
check if the user is invited given the from_id, to_id, and assignment_id. This is a class method to make it more generic and can be used by other classes.
send_invite_email:
This method uses the InvitaionSentMailer to send invitation emails to the invited user.
remove_users_sent_invites_for_assignment(user_id, assignment_id) [ OUT OF SCOPE OF THIS PROJECT ]:
Removes all invites sent by a user for an assignment. This method can be useful
update_users_topic_after_invite_accept(inviter_user_id, invited_user_id, assignment_id) [ OUT OF SCOPE OF THIS PROJECT ]:
After a users accepts an invite, the teams_users table needs to be updated. NOTE: Depends on TeamUser model, which is not implemented yet.
accept_invitation:
This method handles all that needs to be done upon a user accepting an invitation.
decline_invitation:
This method handles all that needs to be done upon an user decline an invitation.
retract_invitation:
This method handles all that need to be done upon an invitation retraction.
as_json:
This will override the default as_json method in the ApplicationRecord class and specify
set_defaults:
This will be used to set default parameters when creating new Invitation.
Changes in models/user.rb
GitHub Link to user.rb file
has_many :invitations
Changes in models/assignment.rb
GitHub Link to assignment.rb file
# add the following relations in the assignment.rb file. class Assignment < ApplicationRecord has_many :invitations end
Schema
filename: schema.rb
GitHub Link to scheme.rb file
create_table "invitations", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.integer "assignment_id" t.integer "from_id" t.integer "to_id" t.string "reply_status", limit: 1 t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["assignment_id"], name: "fk_invitation_assignments" t.index ["from_id"], name: "fk_invitationfrom_users" t.index ["to_id"], name: "fk_invitationto_users" end
Controllers
We create a new controller at app/controllers/invitation_controller.rb with following methods: [Implementation of the invitation controller[1]]
REST Route methods
create:
This will create a new invitation record.
index:
This will give a list of all the invitations
show:
This will be used to show a particular invitation with given invitation_id.
update:
This will be used to update the status of the current invitation. Either accepted or rejected
destroy:
This will be used to delete the invitation that is already sent to another participant using particular invite_id.
invitations_for_user_assignment:
This will be used to get all the invitations for a particular assignment for a particular student.
Helper methods
check_team_before_accept [Not in scope of this project]:
This method will check if the team meets the joining requirements when an invitation is accepted.
check_team_before_invitation [Not in scope of this project]:
This method will check if the team meets the joining requirement before sending an invite.
check_participant_before_invitation [Not in scope of this project]:
This method will check if the invited user is a participant in the assignment.
check_user_before_invitation [Not in scope of this project]:
This method will check if the invited user exists.
End point description
create
Description: This route will create a new invitation record with default reply_status: w. w stands for waiting. REST VERB: POST Path: /invitations/ Response: Success: Status code: 200 ``` { { "id": 11, "reply_status": "W", "created_at": "2023-04-20T21:33:29.580Z", "updated_at": "2023-04-20T21:33:29.580Z", "assignment": { "id": 2, "name": "oodd program3" }, "from_user": { "id": 2, "name": "jhon", "fullname": "jhon carmen", "email": "example1@gmail.com" }, "to_user": { "id": 1, "name": "katy", "fullname": "katy shah", "email": "example2@gmail.com" } } ``` Failure: Status code: 422 ``` { error: @invitation.errors } ```
index
Description: Get a list of all the invitations in the database. REST VERB: GET Path: /invitations/ Request: NA Success Status Code: 200 ``` [ { "id": 1, "reply_status": "A", "created_at": "2023-04-13T17:22:09.805Z", "updated_at": "2023-04-19T00:06:33.718Z", "assignment": { "id": 1, "name": "program4" }, "from_user": { "id": 2, "name": "katy", "fullname": "katy kimson", "email": "kt@gmail.com" }, "to_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" } }, { "id": 2, "reply_status": "W", "created_at": "2023-04-18T15:52:44.543Z", "updated_at": "2023-04-18T15:52:44.543Z", "assignment": { "id": 1, "name": "program4" }, "from_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" }, "to_user": { "id": 3, "name": "rebecca", "fullname": "rebecca fiat", "email": "reb@gmail.com" } } ] ```
show
Description: Get a particular invite with the invite_id. REST VERB: GET Path: /invitations/<invite_id> Request: NA Response: success status code: 200 ``` { "id": 1, "reply_status": "A", "created_at": "2023-04-13T17:22:09.805Z", "updated_at": "2023-04-19T00:06:33.718Z", "assignment": { "id": 1, "name": "program4" }, "from_user": { "id": 2, "name": "katy", "fullname": "katy kimson", "email": "kt@gmail.com" }, "to_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" } } ``` Error Status code: 404 ``` { "error": "Couldn't find Invitation with 'id'=1021321" } ```
update
Description: This route will modify the status of the invitation to be accepted or declined REST VERB: PATCH Path: /invitations/<invite_id>/ Request: ``` { "reply_status": ["A"/"R"], } ``` Response: Success Status Code: 200 ``` { "id": 1, "reply_status": "A", "created_at": "2023-04-13T17:22:09.805Z", "updated_at": "2023-04-19T00:06:33.718Z", "assignment": { "id": 1, "name": "program4" }, "from_user": { "id": 2, "name": "katy", "fullname": "katy kimson", "email": "kt@gmail.com" }, "to_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" } } ``` Error Status Code: 404/422 ``` { "error": @invitation.errors } ```
destroy
Description: Delete the invitation REST VERB: DELETE Path: /invitations/<invite_id> Request: NA Response: Success Status code: 204 :no_content Response: NA Error Status Code: 404 Response ``` { "error": @invitation.errors } ```
invitations_for_user_assignment
Description: Get the invitations for the user with id: user_id for the given assignment with id: assignment_id. REST VERB: GET Path: /invitations/<user_id>/<assingment_id> Request: NA Response: Success Status Code: 200 :ok ``` [ { "id": 1, "reply_status": "A", "created_at": "2023-04-13T17:22:09.805Z", "updated_at": "2023-04-19T00:06:33.718Z", "assignment": { "id": 1, "name": "program4" }, "from_user": { "id": 2, "name": "katy", "fullname": "katy kimson", "email": "kt@gmail.com" }, "to_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" } }, { "id": 2, "reply_status": "W", "created_at": "2023-04-18T15:52:44.543Z", "updated_at": "2023-04-18T15:52:44.543Z", "assignment": { "id": 1, "name": "program4" }, "from_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" }, "to_user": { "id": 1, "name": "john", "fullname": "john fella", "email": "john@gmail.com" } } ] ``` Error Status Code: 404 :not_found ``` { "error": @invitations.errors } ```
End point summary
Sr No | Method | Endpoint | Request Body | Description |
---|---|---|---|---|
1 | create | POST /invitations/ |
assignment_id: INT, from_id: INT, to_id: INT, reply_status: CHAR | Create a new invitation record with default reply_status: w, w stands for waiting. |
2 | index | GET /invitations/ |
Get a list of all the invitations. | |
3 | show | GET /invitations/<assignment_id> |
Get a particular invite with the invite_id. | |
4 | update | PATCH /invitations/<invite_id>/ | "reply_status"=["A"/"R"] | Modify the status of the invitation to be accepted or declined. |
5 | destroy | DELETE /invitations/<invite_id> |
Delete the invitation. | |
6 | invitations_for_user_assignment | GET /invitations/<assignment_id> |
Get the invitations for the user with id: user_id for the given assignment with id: assignment_id |
Testing Plan
We utilized RSpec as the testing framework for our system. We have used factoryBot along with Faker to create model objects for testing. The development process has followed Test Driven Development. The tests for the model and the controller are written in a comprehensive way.
Controller Tests
To run the controller tests:
1. git clone
2. cd reimplementation-back-end/
3. bundle install
4. bundle exec rspec spec/requests/api/v1/invitation_controller_spec.rb
File: invitation_controller_spec.rb
Sr No | Test Description |
---|---|
1 | Test to list all Invitations |
2 | Test to create an invitation with valid parameters |
3 | Test to create an invitation with invalid 'to' user parameters' |
4 | Test to create an invitation with invalid 'from' user parameters |
5 | Test to create an invitation with invalid assignment parameters |
6 | Test to create an invitation with invalid reply_status parameter |
7 | Test to create an invitation with same to user and from user parameters' |
8 | Test to show invitation with valid invitation id |
9 | Test to show invitation with invalid invitation id |
10 | Test to accept invite successfully |
11 | Test to reject invite successfully |
12 | Test to update invitation with invalid reply_status |
13 | Test to update status with invalid invitation_id |
14 | Test to delete invitation with valid invite id |
15 | Test to delete invitation with invalid invite id |
16 | Test to show all invitations for the user for an assignment |
17 | Test to show invitation with invalid user and assignment |
18 | Test to delete invitation with invalid invite idTest to Show invitation with user and invalid assignment |
19 | Test to show invitation with invalid user and invalid assignment |
Model Tests
To run the model tests:
1. git clone
2. cd reimplementation-back-end/
3. bundle install
4. bundle exec rspec spec/models/invitation_spec.rb
File: invitation_spec.rb
Sr No | Test Description |
---|---|
1 | Test to create an invitation with valid parameters. |
2 | Test to create an invitation with the same "to" and "from" user. |
3 | Test to create an invitation with the invalid "to" user parameter |
4 | Test to create an invitation with the invalid "from" user parameter |
5 | Test to create an invitation with a invalid assignment argument. |
6 | Test to create an invitation with an invalid reply_status argument. |
7 | Test to check is_invited? is a true case. |
8 | Test to check is_invited? is a false case. |
9 | Test to check if the default reply_status for a new invitation is WAITING('W'). |
10 | Test to check if invitation_factory returns new invitation. |
11 | Test to check if invitation email is sent properly. |
12 | Test to check if accept_invitation changes reply_status to 'A'. |
13 | Test to check if decline_invitation changes reply_status to 'R'. |
14 | Test to check if retract_invitation destroys the invitation. |
15 | Test to check if as_json formats invitation as required. |
Test Coverage Report for Invitation model.
Swagger UI Screenshot
Swagger is used to provide an easy to use API documentation for the users of the API. It also provides an easy to use interface to interact with the API in the browser itself. You can use the swagger page to test out our API by clicking on the "Try it out" button next to each route.
path: /api-docs/
References
Team Members
- Saigirishwar Rohit Geddam
- Keerthana Telaprolu
- Dhrumil Shah
Mentor
Ankur Mundra