소스 검색

:goal_net: Prevent duplicate entires into tag_associations

tags/0.0.3^2
tomit4 3 년 전
부모
커밋
96ff95d665
1개의 변경된 파일20개의 추가작업 그리고 4개의 파일을 삭제
  1. 20
    4
      backend/lib/services/profile/index.js

+ 20
- 4
backend/lib/services/profile/index.js 파일 보기

357
         await this._setTagLookup()
357
         await this._setTagLookup()
358
         let associations = groupingId
358
         let associations = groupingId
359
             ? await TagAssociation.query()
359
             ? await TagAssociation.query()
360
-                  .where('grouping_id', groupingId)
361
-                  .andWhere('profile_id', profileId)
360
+                .where('grouping_id', groupingId)
361
+                .andWhere('profile_id', profileId)
362
             : await TagAssociation.query().andWhere('profile_id', profileId)
362
             : await TagAssociation.query().andWhere('profile_id', profileId)
363
         return associations
363
         return associations
364
             .map(assoc => ({
364
             .map(assoc => ({
371
                     : true
371
                     : true
372
             })
372
             })
373
     }
373
     }
374
+
375
+    /**
376
+     * Use the db to grab tag associations
377
+     * by profile, grouping, tag, and insert
378
+     * it if it already exists
379
+     * @param {object} association
380
+     */
374
     async revealProfileInfo(association) {
381
     async revealProfileInfo(association) {
375
         const { TagAssociation } = this.server.models()
382
         const { TagAssociation } = this.server.models()
376
-        await TagAssociation.query().insert(association)
377
 
383
 
378
-        return await this.getTagsFor(association.profile_id)
384
+        const existingAssociations = await TagAssociation.query()
385
+            .where('profile_id', `${association.profile_id}`)
386
+            .where('grouping_id', `${association.grouping_id}`)
387
+            .where('tag_id', `${association.tag_id}`)
388
+            .where('is_deleted', 0)
389
+        if (!existingAssociations.length) {
390
+            await TagAssociation.query().insert(association)
391
+            return await this.getTagsFor(association.profile_id)
392
+        } else {
393
+            return console.error('tag association already exists')
394
+        }
379
     }
395
     }
380
 }
396
 }

Loading…
취소
저장