Skip to content

Commit e649705

Browse files
committed
Check for unique tags on syncPostTags
1 parent 53488cf commit e649705

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/entity/PostsTags.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ export default class PostsTags {
6666
post!: Post;
6767

6868
static async syncPostTags(postId: string, tags: Tag[]) {
69+
const uniqueTags = tags.reduce<Tag[]>((acc, current) => {
70+
if (!acc.find(tag => tag.id === current.id)) {
71+
acc.push(current);
72+
return acc;
73+
}
74+
return acc;
75+
}, []);
76+
77+
console.log(uniqueTags);
78+
6979
const repo = getRepository(PostsTags);
7080

7181
// get current post tags
@@ -77,15 +87,15 @@ export default class PostsTags {
7787

7888
const normalized = {
7989
prev: normalize(prevPostTags, postTag => postTag.fk_tag_id),
80-
current: normalize(tags)
90+
current: normalize(uniqueTags)
8191
};
8292

8393
// removes tags that are missing
8494
const missing = prevPostTags.filter(postTag => !normalized.current[postTag.fk_tag_id]);
8595
missing.forEach(tag => repo.remove(tag));
8696

8797
// adds tags that are new
88-
const tagsToAdd = tags.filter(tag => !normalized.prev[tag.id]);
98+
const tagsToAdd = uniqueTags.filter(tag => !normalized.prev[tag.id]);
8999
const postTags = tagsToAdd.map(tag => {
90100
const postTag = new PostsTags();
91101
postTag.fk_post_id = postId;

0 commit comments

Comments
 (0)