Skip to content

Commit

Permalink
chore: refactoring interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Dec 21, 2023
1 parent daea2e9 commit c085ec5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
10 changes: 5 additions & 5 deletions core/api/src/app/quiz/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export const completeQuiz = async ({
const quizzesConfig = getQuizzesConfig()

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

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

const funderWalletId = await getFunderWalletId()
Expand Down Expand Up @@ -100,7 +100,7 @@ export const completeQuiz = async ({
if (recipientBtcWallet === undefined) return new NoBtcWalletExistsForAccountError()
const recipientWalletId = recipientBtcWallet.id

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

const funderBalance = await getBalanceForWallet({ walletId: funderWalletId })
Expand All @@ -116,12 +116,12 @@ export const completeQuiz = async ({
senderWalletId: funderWalletId,
recipientWalletId,
amount,
memo: quizQuestionId,
memo: quizId,
senderAccount: funderAccount,
})
if (payment instanceof Error) return payment

return { id: quizQuestionId, earnAmount: amount }
return { id: quizId, earnAmount: amount }
}

const checkAddQuizAttemptPerIpLimits = async (
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/app/quiz/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { QuizzesValue } from "@/domain/earn/config"
import { QuizRepository } from "@/services/mongoose"

export const getQuizzesByAccountId = async (accountId: AccountId) => {
const quizzes = await QuizRepository(accountId).fetchAll()
const quizzes = await QuizRepository().fetchAll(accountId)
if (quizzes instanceof Error) return quizzes

const solvedQuizId = quizzes.map((quiz) => quiz.quizId)
Expand Down
12 changes: 9 additions & 3 deletions core/api/src/services/mongoose/quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ interface ExtendedError extends Error {
code?: number
}

export const QuizRepository = (accountId: AccountId) => {
const add = async (quizId: QuizQuestionId) => {
export const QuizRepository = () => {
const add = async ({
quizId,
accountId,
}: {
quizId: QuizQuestionId
accountId: AccountId
}) => {
try {
await Quiz.create({ accountId, quizId })

Expand All @@ -22,7 +28,7 @@ export const QuizRepository = (accountId: AccountId) => {
}
}

const fetchAll = async () => {
const fetchAll = async (accountId: AccountId) => {
try {
const result = await Quiz.find({ accountId })
return result
Expand Down
14 changes: 7 additions & 7 deletions core/api/test/integration/services/quiz.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import { QuizRepository } from "@/services/mongoose"

describe("QuizRepository", () => {
const accountId = crypto.randomUUID() as AccountId
const quizQuestionId = "fakeQuizQuestionId" as QuizQuestionId
const quizId = "fakeQuizQuestionId" as QuizQuestionId

it("add quiz", async () => {
const result = await QuizRepository(accountId).add(quizQuestionId)
const result = await QuizRepository().add({ quizId, accountId })
expect(result).toBe(true)
})

it("can't add quiz twice", async () => {
const result = await QuizRepository(accountId).add(quizQuestionId)
const result = await QuizRepository().add({ quizId, accountId })
expect(result).toBeInstanceOf(QuizAlreadyPresentError)
})

it("fetch quiz", async () => {
const result = await QuizRepository(accountId).fetchAll()
const result = await QuizRepository().fetchAll(accountId)
expect(result).toHaveLength(1)
})

it("fetch quizzes", async () => {
const quiz2 = "fakeQuizQuestionId2" as QuizQuestionId
await QuizRepository(accountId).add(quiz2)
await QuizRepository().add({ accountId, quizId: quiz2 })

const result = await QuizRepository(accountId).fetchAll()
const result = await QuizRepository().fetchAll(accountId)
expect(result).toMatchObject([
{
accountId,
quizId: quizQuestionId,
quizId: quizId,
},
{
accountId,
Expand Down

0 comments on commit c085ec5

Please sign in to comment.