Просмотр исходного кода

:goal_net: Prevent duplicate entires into tag_associations

tags/0.0.3^2
tomit4 3 лет назад
Родитель
Сommit
96ff95d665
1 измененных файлов: 20 добавлений и 4 удалений
  1. 20
    4
      backend/lib/services/profile/index.js

+ 20
- 4
backend/lib/services/profile/index.js Просмотреть файл

@@ -357,8 +357,8 @@ module.exports = class ProfileService extends Schmervice.Service {
357 357
         await this._setTagLookup()
358 358
         let associations = groupingId
359 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 362
             : await TagAssociation.query().andWhere('profile_id', profileId)
363 363
         return associations
364 364
             .map(assoc => ({
@@ -371,10 +371,26 @@ module.exports = class ProfileService extends Schmervice.Service {
371 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 381
     async revealProfileInfo(association) {
375 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
 }

Загрузка…
Отмена
Сохранить