Skip to content

Commit

Permalink
chore: remove legacy endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Jan 8, 2024
1 parent 88af186 commit bcc1433
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 175 deletions.
65 changes: 1 addition & 64 deletions bats/core/api/quiz.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,7 @@ setup_file() {
"10000"
}

@test "quiz: completes a quiz question and gets paid once - legacy mutation" {
token_name="alice"
question_id="sat"

# Check initial balance
exec_graphql $token_name 'wallets-for-account'
btc_initial_balance=$(graphql_output '
.data.me.defaultAccount.wallets[]
| select(.walletCurrency == "BTC")
.balance
')

exec_graphql $token_name 'quiz'
completed=$(graphql_output '.data.me.defaultAccount.quiz' | jq '.[] | select(.id == "sat") | .completed')
[[ "${completed}" == "false" ]] || exit 1

# Do quiz
variables=$(
jq -n \
--arg question_id "$question_id" \
'{input: {id: $question_id}}'
)
exec_graphql "$token_name" 'quiz-question' "$variables"
quiz=$(graphql_output '.data.quizCompleted.quiz')
[[ "${quiz}" != "null" ]] || exit 1
quiz_completed=$(graphql_output '.data.quizCompleted.quiz.completed')
[[ "${quiz_completed}" == "true" ]] || exit 1

exec_graphql $token_name 'quiz'
completed=$(graphql_output '.data.me.defaultAccount.quiz' | jq '.[] | select(.id == "sat") | .completed')
[[ "${completed}" == "true" ]] || exit 1

# Check balance after complete
exec_graphql $token_name 'wallets-for-account'
btc_balance_after_quiz=$(graphql_output '
.data.me.defaultAccount.wallets[]
| select(.walletCurrency == "BTC")
.balance
')
[[ "$btc_balance_after_quiz" -gt "$btc_initial_balance" ]] || exit 1

# Check memo
exec_graphql "$token_name" 'transactions' '{"first": 1}'
txn_memo=$(graphql_output '.data.me.defaultAccount.transactions.edges[0].node.memo')
[[ "${txn_memo}" == "${question_id}" ]] || exit 1

# Retry quiz
exec_graphql "$token_name" 'quiz-question' "$variables"
errors=$(graphql_output '.data.quizCompleted.errors')
[[ "${errors}" != "null" ]] || exit 1
error_msg=$(graphql_output '.data.quizCompleted.errors[0].message')
[[ "${error_msg}" =~ "already claimed" ]] || exit 1

# Check balance after retry
exec_graphql $token_name 'wallets-for-account'
btc_balance_after_retry=$(graphql_output '
.data.me.defaultAccount.wallets[]
| select(.walletCurrency == "BTC")
.balance
')
[[ "$btc_balance_after_retry" == "$btc_balance_after_quiz" ]] || exit 1
}

@test "quiz: completes a quiz question and gets paid once - time based quiz mutation" {
@test "quiz: completes a quiz question and gets paid once" {
token_name="alice"
question_id="whatIsBitcoin"

Expand Down
95 changes: 0 additions & 95 deletions core/api/src/app/quiz/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,101 +41,6 @@ type ClaimQuizResult = {
notBefore: Date | undefined
}[]

export const claimQuizLegacy = async ({
quizQuestionId: quizQuestionIdString,
accountId: accountIdRaw,
ip,
}: {
quizQuestionId: string
accountId: string
ip: IpAddress | undefined
}): Promise<ClaimQuizResult | ApplicationError> => {
const check = await checkAddQuizAttemptPerIpLimits(ip)
if (check instanceof Error) return check

const accountId = checkedToAccountId(accountIdRaw)
if (accountId instanceof Error) return accountId

const quizzesConfig = getQuizzesConfig()

// TODO: quizQuestionId checkedFor
const quizId = quizQuestionIdString as QuizQuestionId

const amount = QuizzesValue[quizId]
if (!amount) return new InvalidQuizQuestionIdError()

const funderWalletId = await getFunderWalletId()
const funderWallet = await WalletsRepository().findById(funderWalletId)
if (funderWallet instanceof Error) return funderWallet
const funderAccount = await AccountsRepository().findById(funderWallet.accountId)
if (funderAccount instanceof Error) return funderAccount

const recipientAccount = await AccountsRepository().findById(accountId)
if (recipientAccount instanceof Error) return recipientAccount

const user = await UsersRepository().findById(recipientAccount.kratosUserId)
if (user instanceof Error) return user

const validatedPhoneMetadata = PhoneMetadataAuthorizer(
quizzesConfig.phoneMetadataValidationSettings,
).authorize(user.phoneMetadata)

if (validatedPhoneMetadata instanceof Error) {
return new InvalidPhoneForQuizError(validatedPhoneMetadata.name)
}

const accountIP = await AccountsIpsRepository().findLastByAccountId(recipientAccount.id)
if (accountIP instanceof Error) return accountIP

const validatedIPMetadata = IPMetadataAuthorizer(
quizzesConfig.ipMetadataValidationSettings,
).authorize(accountIP.metadata)
if (validatedIPMetadata instanceof Error) {
if (validatedIPMetadata instanceof MissingIPMetadataError)
return new InvalidIpMetadataError(validatedIPMetadata)

return validatedIPMetadata
}

const quizzesBefore = await listQuizzesByAccountId(accountId)
if (quizzesBefore instanceof Error) return quizzesBefore

const recipientWallets = await WalletsRepository().listByAccountId(accountId)
if (recipientWallets instanceof Error) return recipientWallets

const recipientBtcWallet = recipientWallets.find(
(wallet) => wallet.currency === WalletCurrency.Btc,
)
if (recipientBtcWallet === undefined) return new NoBtcWalletExistsForAccountError()
const recipientWalletId = recipientBtcWallet.id

const funderBalance = await getBalanceForWallet({ walletId: funderWalletId })
if (funderBalance instanceof Error) return funderBalance

const sendCheck = FunderBalanceChecker().check({
balance: funderBalance as Satoshis,
amountToSend: amount,
})
if (sendCheck instanceof Error) return sendCheck

const shouldGiveSats = await QuizRepository().add({ quizId, accountId })
if (shouldGiveSats instanceof Error) return shouldGiveSats

const payment = await intraledgerPaymentSendWalletIdForBtcWallet({
senderWalletId: funderWalletId,
recipientWalletId,
amount,
memo: quizId,
senderAccount: funderAccount,
})
if (payment instanceof Error) return payment

const quizzesAfter = await listQuizzesByAccountId(accountId)
if (quizzesAfter instanceof Error) return quizzesAfter

return quizzesAfter
}

const checkAddQuizAttemptPerIpLimits = async (
ip: IpAddress | undefined,
): Promise<true | RateLimiterExceededError> => {
Expand Down
23 changes: 7 additions & 16 deletions core/api/src/graphql/public/root/mutation/quiz-completed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Quiz } from "@/app"
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map"
import { GT } from "@/graphql/index"

import QuizCompleted from "@/graphql/public/types/payload/quiz-completed"
Expand All @@ -11,6 +9,7 @@ const QuizCompletedInput = GT.Input({
}),
})

// TODO: remove endpoint after 06/2024
const QuizCompletedMutation = GT.Field<
null,
GraphQLPublicContextAuth,
Expand All @@ -24,21 +23,13 @@ const QuizCompletedMutation = GT.Field<
args: {
input: { type: GT.NonNull(QuizCompletedInput) },
},
resolve: async (_, args, { domainAccount, ip }) => {
const { id } = args.input

const quizzes = await Quiz.claimQuizLegacy({
quizQuestionId: id,
accountId: domainAccount.id,
ip,
})
if (quizzes instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(quizzes)] }
}

resolve: async () => {
return {
errors: [],
quiz: quizzes.find((q) => q.id === id),
errors: [{
message: "update the application to the latest version to use this feature",
path: "update the application to the latest version to use this feature",
code: "update the application to the latest version to use this feature",
}],
}
},
})
Expand Down

0 comments on commit bcc1433

Please sign in to comment.