Skip to content

Commit

Permalink
fix: handle paying field is undefined
Browse files Browse the repository at this point in the history
This fixes a regression where limits were enforced wrongly.
  • Loading branch information
aalemayhu committed Sep 28, 2024
1 parent 4e12767 commit b0503e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
9 changes: 4 additions & 5 deletions src/lib/User/checkFlashcardsLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ interface UserOptions {
}

const getCardCount = (initial: number, decks?: Deck[]) => {
let start = initial ?? 0;
if (decks === undefined) return initial ?? 0;

if (decks === undefined) return start;

return decks.reduce((acc, deck) => acc + deck.cards.length, initial) + start;
return decks.reduce((acc, deck) => acc + deck.cards.length, initial);
};

export const checkFlashcardsLimits = ({
Expand All @@ -21,7 +19,8 @@ export const checkFlashcardsLimits = ({
paying,
}: UserOptions) => {
const CARD_LIMIT = 100;
const isAbove100 = getCardCount(cards ?? 0, decks) > CARD_LIMIT;
const cardCount = getCardCount(cards ?? 0, decks);
const isAbove100 = cardCount > CARD_LIMIT;

if (paying) return;

Expand Down
49 changes: 32 additions & 17 deletions src/lib/User/checkLimits.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { checkFlashcardsLimits } from './checkFlashcardsLimits';

describe('checkLimits', () => {

test('throws an error if more than 100 cards are added for anon', () => {
expect(() => checkFlashcardsLimits({
decks: [],
paying: false,
cards: 101
})).toThrow();
expect(() =>
checkFlashcardsLimits({
decks: [],
paying: false,
cards: 101,
})
).toThrow();
});

test('does not throw an error if 100 cards are added by patreon or subscriber', () => {
expect(() => checkFlashcardsLimits({
decks: [],
paying: true,
cards: 200
})).not.toThrow();
expect(() => checkFlashcardsLimits({
decks: [],
cards: 500,
paying: true,
})).not.toThrow();
})
expect(() =>
checkFlashcardsLimits({
decks: [],
paying: true,
cards: 200,
})
).not.toThrow();
expect(() =>
checkFlashcardsLimits({
decks: [],
cards: 500,
paying: true,
})
).not.toThrow();
});

test('does not throw an error if 51 cards are added by anon', () => {
expect(() =>
checkFlashcardsLimits({
decks: [],
paying: undefined,
cards: 51,
})
).not.toThrow();
});
});

0 comments on commit b0503e1

Please sign in to comment.