-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(HP-2023) Added deleteProfile action
- Loading branch information
Showing
2 changed files
with
120 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { loader } from 'graphql.macro'; | ||
import to from 'await-to-js'; | ||
|
||
import { | ||
ActionExecutor, | ||
ActionProps, | ||
QueueController, | ||
getData, | ||
} from '../../common/actionQueue/actionQueue'; | ||
import graphqlClient from '../../graphql/client'; | ||
import { | ||
DeleteMyProfileMutationInput, | ||
GdprDeleteMyProfileMutation, | ||
GdprDeleteMyProfileMutationVariables, | ||
TranslationLanguage, | ||
} from '../../graphql/generatedTypes'; | ||
import { Mutable } from '../../graphql/typings'; | ||
import { getActionResultAndErrorMessage } from './utils'; | ||
import { | ||
getStoredKeycloakAuthCode, | ||
getStoredTunnistamoAuthCode, | ||
} from './authCodeParser'; | ||
|
||
const DELETE_PROFILE = loader('../graphql/GdprDeleteMyProfileMutation.graphql'); | ||
|
||
const deleteProfileType = 'deleteProfile'; | ||
|
||
export const getDeleteProfileResult = (queueController: QueueController) => | ||
getActionResultAndErrorMessage<boolean>(deleteProfileType, queueController) | ||
.result; | ||
|
||
const deleteProfileExecutor: ActionExecutor = async ( | ||
action, | ||
queueController | ||
) => { | ||
const authorizationCode = getStoredTunnistamoAuthCode(queueController); | ||
const authorizationCodeKeycloak = getStoredKeycloakAuthCode(queueController); | ||
if (!authorizationCode) { | ||
return Promise.reject('No tunnistamo authorization code'); | ||
} | ||
const language = getData(action, 'language') as TranslationLanguage; | ||
const input: Mutable<DeleteMyProfileMutationInput> = { | ||
authorizationCode, | ||
dryRun: true, | ||
}; | ||
if (typeof authorizationCodeKeycloak === 'string') { | ||
input.authorizationCodeKeycloak = authorizationCodeKeycloak; | ||
} | ||
const [error, result] = await to( | ||
graphqlClient.mutate< | ||
GdprDeleteMyProfileMutation, | ||
GdprDeleteMyProfileMutationVariables | ||
>({ | ||
mutation: DELETE_PROFILE, | ||
fetchPolicy: 'no-cache', | ||
variables: { | ||
input, | ||
language, | ||
}, | ||
}) | ||
); | ||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
/* | ||
const { failures, successful } = parseDeleteProfileResult(returnedData); | ||
if (!failures.length) { | ||
trackEvent({ category: 'action', action: 'Delete profile' }); | ||
history.push('/profile-deleted'); | ||
} else { | ||
setResultError({ failures, successful }); | ||
} | ||
*/ | ||
if (!result || !result.data) { | ||
return Promise.reject(new Error('Delete profile failed')); | ||
} | ||
return Promise.resolve(true); | ||
}; | ||
|
||
export function createDeleteProfileAction( | ||
language: TranslationLanguage | ||
): ActionProps { | ||
return { | ||
type: deleteProfileType, | ||
executor: deleteProfileExecutor, | ||
options: { | ||
noStorage: true, | ||
idleWhenActive: true, | ||
data: { | ||
language, | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters