Skip to content

Commit

Permalink
Merge pull request #3503 from OfficeForProductSafetyAndStandards/feat…
Browse files Browse the repository at this point in the history
…/PSD-4338-add_state_filter_to_notification_query

feat/PSD-4338-add_state_filter_to_notification_query
  • Loading branch information
alan-at-work authored Jan 2, 2025
2 parents 9c66041 + e055368 commit 452c0b2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
12 changes: 9 additions & 3 deletions cosmetics-web/app/graphql/types/notification_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ module NotificationQueries
end

field :notifications, NotificationType.connection_type, null: false, camelize: false, description: <<~DESC do
Retrieve a paginated list of notifications with optional filters for created_at and updated_at timestamps.
Retrieve a paginated list of notifications with optional filters for created_at, updated_at, and state.
A maximum of 100 records can be retrieved per page.
You can filter by either or both of the `created_after` and `updated_after` fields in the format `YYYY-MM-DD HH:MM`.
You can filter by any or all of the following:
- `created_after` (format: `YYYY-MM-DD HH:MM`)
- `updated_after` (format: `YYYY-MM-DD HH:MM`)
- `state` (exact match on state column)
Results can be sorted using the `order_by` argument, and you can specify a starting point with `from_id`.
Example Query:
Expand All @@ -50,6 +53,7 @@ module NotificationQueries
notifications(
created_after: "2024-08-15T13:00:00Z",
updated_after: "2024-08-15T13:00:00Z",
state: "draft",
order_by: { field: "created_at", direction: "desc" },
first: 10,
from_id: 1
Expand Down Expand Up @@ -93,6 +97,7 @@ module NotificationQueries
argument :updated_after, GraphQL::Types::String, required: false, camelize: false, description: "Retrieve notifications updated after this date in the format 'YYYY-MM-DD HH:MM'"
argument :order_by, Types::OrderByInputType, required: false, camelize: false, description: "Sort results by a specified field and direction"
argument :from_id, GraphQL::Types::ID, required: false, camelize: false, description: "Retrieve notifications starting from a specific ID"
argument :state, GraphQL::Types::String, required: false, camelize: false, description: "Filter notifications by their state" # NEW ARGUMENT HERE
end

field :total_notification_count, Integer, null: false, camelize: false, description: <<~DESC do
Expand All @@ -116,7 +121,7 @@ def notification(id:)
raise Errors::SimpleError, "An error occurred: #{e.message}"
end

def notifications(created_after: nil, updated_after: nil, first: nil, last: nil, after: nil, before: nil, order_by: nil, from_id: nil)
def notifications(created_after: nil, updated_after: nil, state: nil, first: nil, last: nil, after: nil, before: nil, order_by: nil, from_id: nil)
max_limit = 100

first = validate_limit(first, max_limit)
Expand All @@ -128,6 +133,7 @@ def notifications(created_after: nil, updated_after: nil, first: nil, last: nil,
scope = scope.where("created_at >= ?", Time.zone.parse(created_after).utc) if created_after.present?
scope = scope.where("updated_at >= ?", Time.zone.parse(updated_after).utc) if updated_after.present?
scope = scope.where("id > ?", from_id) if from_id.present?
scope = scope.where(state:) if state.present? # APPLY STATE FILTER

# Apply sorting
if order_by.present?
Expand Down
4 changes: 2 additions & 2 deletions cosmetics-web/app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ def versions_with_name
private

def all_required_attributes_must_be_set
mandatory_attributes = mandatory_attributes(state)
mandatory_attributes = mandatory_attributes(state) || []

changed.each do |attribute|
(changed || []).each do |attribute|
if mandatory_attributes.include?(attribute) && self[attribute].blank?
if attribute == "product_name"
errors.add attribute, "Enter the product name"
Expand Down
51 changes: 41 additions & 10 deletions cosmetics-web/spec/graphql/types/notification_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ def perform_post_query(query, variables = {})

def query_notification_and_extract_data(id)
response_json = perform_post_query(query, { id: })
response_json["data"]["notification"]
response_json.dig("data", "notification")
end

it "returns a Notification by ID" do
data = query_notification_and_extract_data(notification.id)
expect(data).to have_key("id")
expect(data["id"]).to eq(notification.id.to_s)
end

Expand Down Expand Up @@ -87,13 +86,16 @@ def query_notification_and_extract_data(id)
end

describe "notifications query" do
let!(:recent_notification) { create(:notification, created_at: "2024-08-10", updated_at: "2024-08-15") }
let(:older_notification) { create(:notification, created_at: "2024-08-01", updated_at: "2024-08-05") }

let(:recent_notification) { create(:notification, created_at: "2024-08-10", updated_at: "2024-08-15") }
let(:query) do
<<~GQL
query($created_after: String, $updated_after: String, $first: Int) {
notifications(created_after: $created_after, updated_after: $updated_after, first: $first) {
query($created_after: String, $updated_after: String, $state: String, $first: Int) {
notifications(
created_after: $created_after,
updated_after: $updated_after,
state: $state,
first: $first
) {
edges {
node {
id
Expand Down Expand Up @@ -129,13 +131,25 @@ def query_notification_and_extract_data(id)
}
GQL
end
let(:older_notification) { create(:notification, created_at: "2024-08-01", updated_at: "2024-08-05") }

before do
recent_notification
older_notification
end

def query_notifications(created_after: nil, updated_after: nil, first: nil)
perform_post_query(query, { created_after:, updated_after:, first: })
def query_notifications(created_after: nil, updated_after: nil, first: nil, state: nil)
perform_post_query(query, {
created_after:,
updated_after:,
first:,
state:,
})
end

it "returns the total number of Notifications" do
it "returns the total number of Notifications (filtered by created_after)" do
response_json = query_notifications(created_after: "2024-08-05T00:00:00Z", first: 10)
# Only recent_notification is created after 2024-08-05
expect(response_json.dig("data", "notifications", "edges").size).to eq(1)
end

Expand Down Expand Up @@ -167,6 +181,23 @@ def query_notifications(created_after: nil, updated_after: nil, first: nil)
page_info = response_json.dig("data", "notifications", "pageInfo")
expect(page_info["endCursor"]).not_to be_nil
end

context "when filtering by state" do
let(:components_complete_notification) { create(:notification, state: "components_complete") }
let(:ready_for_components_notification) { create(:notification, state: "ready_for_components") }

before do
components_complete_notification
ready_for_components_notification
end

it "returns only notifications matching the specified state" do
edges = query_notifications(state: "components_complete", first: 10)
.dig("data", "notifications", "edges")
expect(edges.size).to eq(1)
expect(edges.first["node"]).to include("id" => components_complete_notification.id.to_s, "state" => "components_complete")
end
end
end

describe "total_notification_count query" do
Expand Down

0 comments on commit 452c0b2

Please sign in to comment.