mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
create a trigger to update recommender_names in library_item table
This commit is contained in:
parent
f6c906201b
commit
0e7f45a758
4 changed files with 9 additions and 25 deletions
|
|
@ -4,7 +4,6 @@ import {
|
|||
InsertEvent,
|
||||
} from 'typeorm'
|
||||
import { Profile } from '../../entity/profile'
|
||||
import { env } from '../../env'
|
||||
import { createPubSubClient } from '../../pubsub'
|
||||
import { addPopularReadsForNewUser } from '../../services/popular_reads'
|
||||
import { IntercomClient } from '../../utils/intercom'
|
||||
|
|
|
|||
|
|
@ -283,9 +283,9 @@ export const recommendHighlightsResolver = authorized<
|
|||
{
|
||||
id: group.id,
|
||||
note: input.note,
|
||||
recommender: user,
|
||||
recommender: { id: uid },
|
||||
createdAt: new Date(),
|
||||
libraryItem: item,
|
||||
libraryItem: { id: item.id },
|
||||
},
|
||||
auth,
|
||||
input.highlightIds
|
||||
|
|
|
|||
|
|
@ -16,21 +16,6 @@ export const addRecommendation = async (
|
|||
try {
|
||||
// check if the item is already recommended to the group
|
||||
let recommendedItem = await findLibraryItemByUrl(item.originalUrl, userId)
|
||||
// if (existingItem) {
|
||||
// const existingHighlights = existingItem.highlights || []
|
||||
|
||||
// // remove duplicates
|
||||
// const newHighlights =
|
||||
// highlights?.filter(
|
||||
// (highlight) =>
|
||||
// !existingHighlights.find(
|
||||
// (existingHighlight) => existingHighlight.quote === highlight.quote
|
||||
// )
|
||||
// ) || []
|
||||
|
||||
// return existingItem
|
||||
// }
|
||||
|
||||
if (!recommendedItem) {
|
||||
// create a new item
|
||||
const newItem: DeepPartial<LibraryItem> = {
|
||||
|
|
@ -103,6 +88,6 @@ export const findRecommendationsByLibraryItemId = async (
|
|||
relations: {
|
||||
group: true,
|
||||
recommender: true,
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,33 +4,33 @@
|
|||
|
||||
BEGIN;
|
||||
|
||||
ALTER TABLE omnivore.library_item ADD COLUMN recommender_names text[] DEFAULT '{}'::text[];
|
||||
ALTER TABLE omnivore.library_item ADD COLUMN IF NOT EXISTS recommender_names text[] DEFAULT '{}'::text[];
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_library_item_recommenders()
|
||||
RETURNS trigger AS $$
|
||||
BEGIN
|
||||
-- update library_item recommender names from user and group table name column
|
||||
UPDATE omnivore.library_item
|
||||
SET recommender_names = (
|
||||
SET recommender_names = array_cat(recommender_names, (
|
||||
SELECT array_agg(DISTINCT name)
|
||||
FROM (
|
||||
SELECT name
|
||||
FROM omnivore.user
|
||||
WHERE id = NEW.user_id
|
||||
WHERE id = NEW.recommender_id
|
||||
UNION
|
||||
SELECT name
|
||||
FROM omnivore.group
|
||||
WHERE id = NEW.group_id
|
||||
) AS recommender_names
|
||||
)
|
||||
))
|
||||
WHERE id = NEW.library_item_id;
|
||||
|
||||
return NEW;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER library_item_recommenders_update
|
||||
AFTER INSERT ON omnivore.recommendation
|
||||
BEFORE INSERT ON omnivore.recommendation
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_library_item_recommenders();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue