Skip to content

Commit

Permalink
fix(service): Fix addDiscount function
Browse files Browse the repository at this point in the history
When reading a single book via id, book is not iterable and the function crashes.
  • Loading branch information
HeneryHawk committed Jul 10, 2022
1 parent 5dd7389 commit 66c43cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions src/entities/BookHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,25 @@ export class BookHandler {
public async addDiscount(@Entities() book: CatalogService.IBook[], @Req() req: any): Promise<void> {
console.log("Read Books");

for (const each of book) {
if (each.stock > 111) each.title += ` -- 11% discount!`;
const addDiscount = (book) => {
if (book.stock > 111) book.title += ` -- 11% discount!`;
return book
}

const isIterable = (obj) => {
// checks for null and undefined
if (obj == null) {
return false;
}
return typeof obj[Symbol.iterator] === 'function';
}

if (isIterable(book)) {
for (const each of book) {
addDiscount(each);
}
} else {
addDiscount(book);
}
}
}

0 comments on commit 66c43cf

Please sign in to comment.