Skip to content

Commit

Permalink
Fix api token flash and revoke mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Dec 22, 2023
1 parent 5bbe9ba commit 959a83a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/graphql/mutations/api_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Accent.GraphQL.Mutations.APIToken do
resolve(project_authorize(:create_project_api_token, &APITokenResolver.create/3, :project_id))
end

field :revoke_api_token, :boolean do
field :revoke_api_token, :mutated_api_token do
arg(:id, non_null(:id))

resolve(api_token_authorize(:revoke_project_api_token, &APITokenResolver.revoke/3))
Expand Down
9 changes: 5 additions & 4 deletions lib/graphql/resolvers/api_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ defmodule Accent.GraphQL.Resolvers.APIToken do
alias Accent.Plugs.GraphQLContext
alias Accent.Project

@spec create(Project.t(), any(), GraphQLContext.t()) :: {:ok, AccessToken.t() | nil}
@spec create(Project.t(), any(), GraphQLContext.t()) ::
{:ok, %{api_token: AccessToken.t() | nil, errors: list(String.t()) | nil}}
def create(project, args, info) do
case APITokenManager.create(project, info.context[:conn].assigns[:current_user], args) do
{:ok, %{access_token: api_token}} ->
{:ok, %{api_token: api_token, errors: nil}}

{:error, _reason, _, _} ->
{:ok, %{access_token: nil, errors: ["unprocessable_entity"]}}
{:ok, %{api_token: nil, errors: ["unprocessable_entity"]}}
end
end

@spec revoke(Project.t(), any(), GraphQLContext.t()) :: {:ok, AccessToken.t() | nil}
@spec revoke(Project.t(), any(), GraphQLContext.t()) :: {:ok, AccessToken.t()}
def revoke(access_token, _args, _) do
APITokenManager.revoke(access_token)
{:ok, true}
{:ok, access_token}
end

@spec list_project(Project.t(), any(), GraphQLContext.t()) :: {:ok, AccessToken.t() | nil}
Expand Down
10 changes: 6 additions & 4 deletions webapp/app/locales/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -1091,10 +1091,12 @@
},
"api_token": {
"loading_content": "Fetching project’s API settings…",
"api_token_add_success": "API token has been created with success",
"api_token_add_error": "API token could not be created",
"api_token_revoke_success": "API token has been revoked with success",
"api_token_revoke_error": "API token could not be revoked"
"flash_messages": {
"api_token_add_success": "API token has been created with success",
"api_token_add_error": "API token could not be created",
"api_token_revoke_success": "API token has been revoked with success",
"api_token_revoke_error": "API token could not be revoked"
}
},
"jipt": {
"loading_content": "Fetching project’s Just In Place Translations settings…"
Expand Down
10 changes: 6 additions & 4 deletions webapp/app/locales/fr-ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,12 @@
},
"api_token": {
"loading_content": "Récupération des paramètres d’API du projet…",
"api_token_add_success": "Le jeton d’API a été créé avec succès",
"api_token_add_error": "Le jeton d’API n’a pas pu être créé",
"api_token_revoke_success": "Le jeton d’API a été révoqué avec succès",
"api_token_revoke_error": "Le jeton d’API n’a pas pu être révoqué"
"flash_messages": {
"api_token_add_success": "Le jeton d’API a été créé avec succès",
"api_token_add_error": "Le jeton d’API n’a pas pu être créé",
"api_token_revoke_success": "Le jeton d’API a été révoqué avec succès",
"api_token_revoke_error": "Le jeton d’API n’a pas pu être révoqué"
}
},
"jipt": {
"loading_content": "Récupération des paramètres de traduction JIPT du projet…"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ApolloMutate from 'accent-webapp/services/apollo-mutate';
import apiTokenCreateQuery from 'accent-webapp/queries/create-api-token';
import apiTokenRevokeQuery from 'accent-webapp/queries/revoke-api-token';

const FLASH_MESSAGE_PREFIX = 'pods.project.edit.flash_messages.';
const FLASH_MESSAGE_PREFIX = 'pods.project.edit.api_token.flash_messages.';
const FLASH_MESSAGE_API_TOKEN_ADD_SUCCESS = `${FLASH_MESSAGE_PREFIX}api_token_add_success`;
const FLASH_MESSAGE_API_TOKEN_ADD_ERROR = `${FLASH_MESSAGE_PREFIX}api_token_add_error`;
const FLASH_MESSAGE_API_TOKEN_REVOKE_SUCCESS = `${FLASH_MESSAGE_PREFIX}api_token_revoke_success`;
Expand Down
8 changes: 7 additions & 1 deletion webapp/app/queries/revoke-api-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export interface RevokeApiTokenVariables {

export default gql`
mutation ApiTokenRevoke($id: ID!) {
revokeApiToken(id: $id)
revokeApiToken(id: $id) {
apiToken {
id
}
errors
}
}
`;

0 comments on commit 959a83a

Please sign in to comment.