Skip to content

Commit

Permalink
feat(helper): 🏷️ type predicates for AuthorProfile and Chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
djdembeck committed Aug 28, 2022
1 parent 487315a commit 5386967
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
25 changes: 24 additions & 1 deletion src/config/typing/checkers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { AuthorDocument } from '#config/models/Author'
import { BookDocument } from '#config/models/Book'
import { Book } from '#config/typing/books'
import { ChapterDocument } from '#config/models/Chapter'
import { ApiChapter, Book } from '#config/typing/books'
import { AuthorProfile } from '#config/typing/people'

export function isAuthorProfile(author: unknown): author is AuthorProfile {
if (!author) return false
return typeof author === 'object' && 'name' in author
}

export function isAuthorDocument(author: unknown): author is AuthorDocument {
if (!author) return false
return typeof author === 'object' && '_id' in author && 'name' in author
}

export function isBook(book: unknown): book is Book {
if (!book) return false
Expand All @@ -10,3 +23,13 @@ export function isBookDocument(book: unknown): book is BookDocument {
if (!book) return false
return typeof book === 'object' && '_id' in book && 'title' in book
}

export function isChapter(chapter: unknown): chapter is ApiChapter {
if (!chapter) return false
return typeof chapter === 'object' && 'runtimeLengthMs' in chapter
}

export function isChapterDocument(chapter: unknown): chapter is ChapterDocument {
if (!chapter) return false
return typeof chapter === 'object' && '_id' in chapter && 'runtimeLengthMs' in chapter
}
14 changes: 8 additions & 6 deletions src/helpers/database/papr/audible/PaprAudibleAuthorHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AuthorModel from '#config/models/Author'
import { isAuthorProfile } from '#config/typing/checkers'
import { AuthorProfile } from '#config/typing/people'
import { RequestGeneric } from '#config/typing/requests'
import SharedHelper from '#helpers/shared'
Expand Down Expand Up @@ -56,12 +57,12 @@ export default class PaprAudibleAuthorHelper {
}

async findOneWithProjection() {
const findOneAuthor = (await AuthorModel.findOne(
const findOneAuthor = await AuthorModel.findOne(
{
asin: this.asin
},
{ projection: projectionWithoutDbFields }
)) as unknown as AuthorProfile
)
return {
data: findOneAuthor,
modified: false
Expand All @@ -77,19 +78,20 @@ export default class PaprAudibleAuthorHelper {
const findInDb = await this.findOneWithProjection()

// Update
if (this.options.update === '1' && findInDb.data) {
if (this.options.update === '1' && isAuthorProfile(findInDb.data)) {
const data = findInDb.data
// If the objects are the exact same return right away
const equality = sharedHelper.checkDataEquality(findInDb.data, this.authorData)
const equality = sharedHelper.checkDataEquality(data, this.authorData)
if (equality) {
return {
data: findInDb.data,
data: data,
modified: false
}
}
// Check state of existing author
// Only update if either genres exist and can be checked
// -or if genres exist on new item but not old
if (findInDb.data.genres || (!findInDb.data.genres && this.authorData.genres)) {
if (data.genres || (!data.genres && this.authorData.genres)) {
// Only update if it's not nuked data
if (this.authorData.genres?.length) {
console.log(`Updating author asin ${this.asin}`)
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/database/papr/audible/PaprAudibleBookHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BookModel from '#config/models/Book'
import { Book } from '#config/typing/books'
import { isBookDocument } from '#config/typing/checkers'
import { isBook } from '#config/typing/checkers'
import { RequestGenericWithSeed } from '#config/typing/requests'
import SharedHelper from '#helpers/shared'

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

// Update
if (this.options.update === '1' && isBookDocument(findInDb.data)) {
if (this.options.update === '1' && isBook(findInDb.data)) {
const data = findInDb.data
// If the objects are the exact same return right away
const equality = sharedHelper.checkDataEquality(data, this.bookData)
Expand Down
12 changes: 7 additions & 5 deletions src/helpers/database/papr/audible/PaprAudibleChapterHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ChapterModel from '#config/models/Chapter'
import { ApiChapter } from '#config/typing/books'
import { isChapter } from '#config/typing/checkers'
import { RequestGeneric } from '#config/typing/requests'
import SharedHelper from '#helpers/shared'

Expand Down Expand Up @@ -56,12 +57,12 @@ export default class PaprAudibleChapterHelper {
}

async findOneWithProjection() {
const findOneChapter = (await ChapterModel.findOne(
const findOneChapter = await ChapterModel.findOne(
{
asin: this.asin
},
{ projection: projectionWithoutDbFields }
)) as unknown as ApiChapter
)
return {
data: findOneChapter,
modified: false
Expand All @@ -77,12 +78,13 @@ export default class PaprAudibleChapterHelper {
const findInDb = await this.findOneWithProjection()

// Update
if (this.options.update === '1' && findInDb.data) {
if (this.options.update === '1' && isChapter(findInDb.data)) {
const data = findInDb.data
// If the objects are the exact same return right away
const equality = sharedHelper.checkDataEquality(findInDb.data, this.chapterData)
const equality = sharedHelper.checkDataEquality(data, this.chapterData)
if (equality) {
return {
data: findInDb.data,
data: data,
modified: false
}
}
Expand Down

0 comments on commit 5386967

Please sign in to comment.