Skip to content

Commit

Permalink
findbookbyisbn moved to service class
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnmclean committed Dec 18, 2024
1 parent 83e3480 commit 2452cbb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
113 changes: 59 additions & 54 deletions apps/sovoli.com/src/services/books/findBookByISBN.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { SelectBook } from "@sovoli/db/schema";
import { db, eq, or, schema, sql } from "@sovoli/db";

import { BaseService } from "../baseService";
import { GetBookFromISBNdb } from "../isbndb/getBookFromISBNdb";

export interface FindBookByISBNOptions {
isbn: string;
forceExternal?: boolean;
}

export interface FindBookByISBNResult {
Expand All @@ -14,65 +16,68 @@ export interface FindBookByISBNResult {
book?: SelectBook;
}

export const findBookByISBN = async ({
isbn,
}: FindBookByISBNOptions): Promise<FindBookByISBNResult> => {
const internalBook = await db.query.Book.findFirst({
where: or(eq(schema.Book.isbn10, isbn), eq(schema.Book.isbn13, isbn)),
});
export class FindBookByISBN extends BaseService<
FindBookByISBNOptions,
FindBookByISBNResult
> {
protected async execute({ isbn, forceExternal }: FindBookByISBNOptions) {
const internalBook = await db.query.Book.findFirst({
where: or(eq(schema.Book.isbn10, isbn), eq(schema.Book.isbn13, isbn)),
});

if (internalBook) {
console.log("found book in internal db");
return {
book: internalBook,
};
}
if (internalBook && !!forceExternal) {
console.log("found book in internal db");
return {
book: internalBook,
};
}

console.log("no internal results, searching externally");
const getBookFromISBNdb = new GetBookFromISBNdb();
console.log("no internal results, searching externally");
const getBookFromISBNdb = new GetBookFromISBNdb();

const { book } = await getBookFromISBNdb.call({ isbn });
const { book } = await getBookFromISBNdb.call({ isbn });

if (!book) {
console.log("no external book found");
return {};
}
if (!book) {
console.log("no external book found");
return {};
}

console.log("found external book");
console.log("found external book");

const [insertedBooks] = await db
.insert(schema.Book)
.values(book)
.onConflictDoUpdate({
target: [schema.Book.isbn10, schema.Book.isbn13],
set: {
title: sql.raw(`excluded.${schema.Book.title.name}`), // Update the title if there's a conflict
longTitle: sql.raw(`excluded.${schema.Book.longTitle.name}`), // Update the long title
language: sql.raw(`excluded.${schema.Book.language.name}`), // Update the language field
image: sql.raw(`excluded.${schema.Book.image.name}`), // Update the image URL
dimensions: sql.raw(`excluded.${schema.Book.dimensions.name}`), // Update dimensions if changed
structuredDimensions: sql.raw(
`excluded.${schema.Book.structuredDimensions.name}`,
), // Update structured dimensions
pageCount: sql.raw(`excluded.${schema.Book.pageCount.name}`), // Update the page count
subjects: sql.raw(`excluded.${schema.Book.subjects.name}`), // Update subjects array
authors: sql.raw(`excluded.${schema.Book.authors.name}`), // Update authors array
publishedDate: sql.raw(`excluded.${schema.Book.publishedDate.name}`), // Update published date
publisher: sql.raw(`excluded.${schema.Book.publisher.name}`), // Update the publisher if there's a conflict
binding: sql.raw(`excluded.${schema.Book.binding.name}`), // Update the binding type (e.g., Hardcover, Paperback)
otherISBNs: sql.raw(`excluded.${schema.Book.otherISBNs.name}`), // Update other ISBNs array
description: sql.raw(`excluded.${schema.Book.description.name}`), // Update description (overview or synopsis)
subtitle: sql.raw(`excluded.${schema.Book.subtitle.name}`), // Update subtitle if changed
lastISBNdbUpdated: sql.raw(
`excluded.${schema.Book.lastISBNdbUpdated.name}`,
),
},
})
.returning();
const [insertedBooks] = await db
.insert(schema.Book)
.values(book)
.onConflictDoUpdate({
target: [schema.Book.isbn10, schema.Book.isbn13],
set: {
title: sql.raw(`excluded.${schema.Book.title.name}`), // Update the title if there's a conflict
longTitle: sql.raw(`excluded.${schema.Book.longTitle.name}`), // Update the long title
language: sql.raw(`excluded.${schema.Book.language.name}`), // Update the language field
image: sql.raw(`excluded.${schema.Book.image.name}`), // Update the image URL
dimensions: sql.raw(`excluded.${schema.Book.dimensions.name}`), // Update dimensions if changed
structuredDimensions: sql.raw(
`excluded.${schema.Book.structuredDimensions.name}`,
), // Update structured dimensions
pageCount: sql.raw(`excluded.${schema.Book.pageCount.name}`), // Update the page count
subjects: sql.raw(`excluded.${schema.Book.subjects.name}`), // Update subjects array
authors: sql.raw(`excluded.${schema.Book.authors.name}`), // Update authors array
publishedDate: sql.raw(`excluded.${schema.Book.publishedDate.name}`), // Update published date
publisher: sql.raw(`excluded.${schema.Book.publisher.name}`), // Update the publisher if there's a conflict
binding: sql.raw(`excluded.${schema.Book.binding.name}`), // Update the binding type (e.g., Hardcover, Paperback)
otherISBNs: sql.raw(`excluded.${schema.Book.otherISBNs.name}`), // Update other ISBNs array
description: sql.raw(`excluded.${schema.Book.description.name}`), // Update description (overview or synopsis)
subtitle: sql.raw(`excluded.${schema.Book.subtitle.name}`), // Update subtitle if changed
lastISBNdbUpdated: sql.raw(
`excluded.${schema.Book.lastISBNdbUpdated.name}`,
),
},
})
.returning();

console.log("upserted book");
console.log("upserted book");

return {
book: insertedBooks,
};
};
return {
book: insertedBooks,
};
}
}
2 changes: 1 addition & 1 deletion apps/sovoli.com/src/services/books/searchBooksByQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const searchBooksByQuery = async ({
});

// TODO: later we can combine the internal and external search calls under a single promise.all
if (books.length > 0 && !forceExternal) {
if (books.length > 0 && !!forceExternal) {
console.log(`found ${books.length} books in internal db`);
return {
books: books,
Expand Down
5 changes: 3 additions & 2 deletions apps/sovoli.com/src/services/knowledge/knowledgeUpserted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SelectBook, SelectKnowledgeSchema } from "@sovoli/db/schema";
import { and, db, eq, schema } from "@sovoli/db";
import { KnowledgeQueryType, KnowledgeType } from "@sovoli/db/schema";

import { findBookByISBN } from "../books/findBookByISBN";
import { FindBookByISBN } from "../books/findBookByISBN";
import { searchBooksByQuery } from "../books/searchBooksByQuery";
import { PublishKnowledge } from "./publishKnowledge";

Expand Down Expand Up @@ -94,7 +94,8 @@ const handleBookKnowledgeTypeUpserted = async (
switch (knowledge.queryType) {
case KnowledgeQueryType.isbn: {
console.log("searching for book by isbn");
const result = await findBookByISBN({
const findBookByISBN = new FindBookByISBN();
const result = await findBookByISBN.call({
isbn: knowledge.query,
});
book = result.book;
Expand Down

0 comments on commit 2452cbb

Please sign in to comment.