Skip to content

Commit

Permalink
Adding a new GraphQL field tipline_requests to TeamType. (#2100)
Browse files Browse the repository at this point in the history
This new field allows partners to use the API and export tipline requests for a given time interval. For now, limiting to one month for performance reasons.

Reference: CV2-5238.
  • Loading branch information
caiosba authored Oct 22, 2024
1 parent dc55cc1 commit f487d2d
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/graph/types/team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,23 @@ def tipline_messages(uid:)
TiplineMessagesPagination.new(object.tipline_messages.where(uid: uid).order('sent_at DESC'))
end

field :tipline_requests, TiplineRequestType.connection_type, null: true do
argument :from_timestamp, GraphQL::Types::Int, required: true, camelize: false
argument :to_timestamp, GraphQL::Types::Int, required: true, camelize: false
end

def tipline_requests(from_timestamp:, to_timestamp:)
ability = context[:ability] || Ability.new
return TiplineRequest.none unless ability.can?(:read, TiplineRequest.new(team_id: object.id))

raise 'Maximum interval is one month.' if ((to_timestamp - from_timestamp) > 32.days.to_i) # Use 32 just to be safe
from = Time.at(from_timestamp)
to = Time.at(to_timestamp)

# Only `ProjectMedia` requests that are not in the trash
TiplineRequest.joins("INNER JOIN project_medias pm ON pm.id = tipline_requests.associated_id AND tipline_requests.associated_type = 'ProjectMedia'").where(team_id: object.id, created_at: from..to).where('pm.archived' => ::CheckArchivedFlags::FlagCodes::NONE)
end

field :articles, ::ArticleUnion.connection_type, null: true do
argument :article_type, GraphQL::Types::String, required: true, camelize: false

Expand Down
23 changes: 23 additions & 0 deletions lib/relay.idl
Original file line number Diff line number Diff line change
Expand Up @@ -13526,6 +13526,29 @@ type Team implements Node {
"""
last: Int
): TiplineNewsletterConnection
tipline_requests(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String

"""
Returns the elements in the list that come before the specified cursor.
"""
before: String

"""
Returns the first _n_ elements from the list.
"""
first: Int
from_timestamp: Int!

"""
Returns the last _n_ elements from the list.
"""
last: Int
to_timestamp: Int!
): TiplineRequestConnection
tipline_resources(
"""
Returns the elements in the list that come after the specified cursor.
Expand Down
93 changes: 93 additions & 0 deletions public/relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -71074,6 +71074,99 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "tipline_requests",
"description": null,
"args": [
{
"name": "after",
"description": "Returns the elements in the list that come after the specified cursor.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "before",
"description": "Returns the elements in the list that come before the specified cursor.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "first",
"description": "Returns the first _n_ elements from the list.",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "last",
"description": "Returns the last _n_ elements from the list.",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "from_timestamp",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "to_timestamp",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"type": {
"kind": "OBJECT",
"name": "TiplineRequestConnection",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "tipline_resources",
"description": null,
Expand Down
54 changes: 54 additions & 0 deletions test/controllers/graphql_controller_11_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,58 @@ def teardown
assert !JSON.parse(@response.body)['data']['exportList']['success']
end
end

test "should not get requests if interval is more than one month" do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'editor'
2.times { create_tipline_request team_id: t.id }
create_tipline_request
authenticate_with_user(u)

query = <<~GRAPHQL
query {
team(slug: "#{t.slug}") {
tipline_requests(from_timestamp: 1724266372, to_timestamp: 1729536732) {
edges {
node {
id
}
}
}
}
}
GRAPHQL
post :create, params: { query: query, team: t.slug }
assert_response 400
assert_equal 'Maximum interval is one month.', JSON.parse(@response.body)['errors'][0]['message']
end

test "should get requests" do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'editor'
2.times { create_tipline_request team_id: t.id }
create_tipline_request
authenticate_with_user(u)

from = Time.now.beginning_of_month.to_i
to = Time.now.end_of_month.to_i
query = <<~GRAPHQL
query {
team(slug: "#{t.slug}") {
tipline_requests(from_timestamp: #{from}, to_timestamp: #{to}) {
edges {
node {
id
}
}
}
}
}
GRAPHQL
post :create, params: { query: query, team: t.slug }
assert_response :success
assert_equal 2, JSON.parse(@response.body).dig('data', 'team', 'tipline_requests', 'edges').size
end
end

0 comments on commit f487d2d

Please sign in to comment.