Skip to content

Commit

Permalink
feat(helper): 🏷️ add type predicates for Book and BookDocument
Browse files Browse the repository at this point in the history
no longer statically assign these types
  • Loading branch information
djdembeck committed Aug 28, 2022
1 parent 62135e2 commit 487315a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/config/typing/checkers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BookDocument } from '#config/models/Book'
import { Book } from '#config/typing/books'

export function isBook(book: unknown): book is Book {
if (!book) return false
return typeof book === 'object' && 'title' in book
}

export function isBookDocument(book: unknown): book is BookDocument {
if (!book) return false
return typeof book === 'object' && '_id' in book && 'title' in book
}
7 changes: 4 additions & 3 deletions src/helpers/database/papr/audible/PaprAudibleBookHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BookModel, { BookDocument } from '#config/models/Book'
import BookModel from '#config/models/Book'
import { Book } from '#config/typing/books'
import { isBookDocument } from '#config/typing/checkers'
import { RequestGenericWithSeed } from '#config/typing/requests'
import SharedHelper from '#helpers/shared'

Expand Down Expand Up @@ -77,8 +78,8 @@ export default class PaprAudibleBookHelper {
const findInDb = await this.findOneWithProjection()

// Update
if (this.options.update === '1' && findInDb.data) {
const data = findInDb.data as BookDocument
if (this.options.update === '1' && isBookDocument(findInDb.data)) {
const data = findInDb.data
// If the objects are the exact same return right away
const equality = sharedHelper.checkDataEquality(data, this.bookData)
if (equality) {
Expand Down
21 changes: 13 additions & 8 deletions src/helpers/routes/BookShowHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyRedis } from '@fastify/redis'

import { BookDocument } from '#config/models/Book'
import { Book } from '#config/typing/books'
import { isBook, isBookDocument } from '#config/typing/checkers'
import { RequestGenericWithSeed } from '#config/typing/requests'
import SeedHelper from '#helpers/authors/audible/SeedHelper'
import StitchHelper from '#helpers/books/audible/StitchHelper'
Expand Down Expand Up @@ -44,8 +45,8 @@ export default class BookShowHelper {
this.paprHelper.setBookData(await this.getNewBookData())
// Create or update the book
const bookToReturn = await this.paprHelper.createOrUpdate()
if (!bookToReturn.data) throw new Error(`Book ${this.asin} not found`)
const data = bookToReturn.data as BookDocument
if (!isBookDocument(bookToReturn.data)) throw new Error(`BookDocument ${this.asin} not found`)
const data = bookToReturn.data

// Update or create the book in cache
await this.redisHelper.findOrCreate(data)
Expand All @@ -69,12 +70,12 @@ export default class BookShowHelper {
*/
async updateActions(): Promise<Book> {
// 1. Check if it is updated recently
if (this.isUpdatedRecently()) return this.originalBook as Book
if (this.isUpdatedRecently() && isBook(this.originalBook)) return this.originalBook

// 2. Get the new book and create or update it
const bookToReturn = await this.createOrUpdateBook()
if (!bookToReturn.data) throw new Error(`Book ${this.asin} not found`)
const data = bookToReturn.data as BookDocument
if (!isBookDocument(bookToReturn.data)) throw new Error(`BookDocument ${this.asin} not found`)
const data = bookToReturn.data

// 3. Update book in cache
if (bookToReturn.modified) {
Expand Down Expand Up @@ -103,13 +104,17 @@ export default class BookShowHelper {
if (this.options.update === '1') {
return this.updateActions()
}

// 1. Get the book with projections
const bookToReturn = await this.paprHelper.findOneWithProjection()
if (!bookToReturn.data) throw new Error(`Book ${this.asin} not found`)
const data = bookToReturn.data as BookDocument
// Make saure we get a book type back
if (!isBook(bookToReturn.data)) throw new Error(`Book ${this.asin} not found`)
const data = bookToReturn.data

// 2. Check it it is cached
const redisBook = await this.redisHelper.findOrCreate(data)
if (redisBook) return redisBook as Book
if (redisBook && isBook(redisBook)) return redisBook

// 3. Return the book from DB
return data
}
Expand Down

0 comments on commit 487315a

Please sign in to comment.