From a7ef781719b9b4f1eda19f7c544b40c10fc0ad6f Mon Sep 17 00:00:00 2001 From: sr Date: Sun, 10 Sep 2023 11:57:57 +0530 Subject: [PATCH] fix: feed duplication --- apps/api/src/app/feeds/e2e/create-feed.e2e.ts | 11 +++++++++++ .../feeds/usecases/create-feed/create-feed.usecase.ts | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/apps/api/src/app/feeds/e2e/create-feed.e2e.ts b/apps/api/src/app/feeds/e2e/create-feed.e2e.ts index 0779d4f7edc..2b4477cb868 100644 --- a/apps/api/src/app/feeds/e2e/create-feed.e2e.ts +++ b/apps/api/src/app/feeds/e2e/create-feed.e2e.ts @@ -135,4 +135,15 @@ describe('Create A Feed - /feeds (POST)', async () => { }); expect(feedsCount).to.equal(2); }); + + it('should throw error if a feed already exist', async function () { + await session.testAgent.post(`/v1/feeds`).send({ + name: 'identifier_123', + }); + const { body } = await session.testAgent.post(`/v1/feeds`).send({ + name: 'identifier_123', + }); + expect(body.statusCode).to.equal(409); + expect(body.message).to.equal('Feed with identifier: identifier_123 already exists'); + }); }); diff --git a/apps/api/src/app/feeds/usecases/create-feed/create-feed.usecase.ts b/apps/api/src/app/feeds/usecases/create-feed/create-feed.usecase.ts index d6323ca83f7..7834899bf14 100644 --- a/apps/api/src/app/feeds/usecases/create-feed/create-feed.usecase.ts +++ b/apps/api/src/app/feeds/usecases/create-feed/create-feed.usecase.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { ConflictException, Injectable } from '@nestjs/common'; import { FeedRepository, FeedEntity } from '@novu/dal'; import { CreateFeedCommand } from './create-feed.command'; import { CreateChange, CreateChangeCommand } from '../../../change/usecases'; @@ -9,6 +9,15 @@ export class CreateFeed { constructor(private feedRepository: FeedRepository, private createChange: CreateChange) {} async execute(command: CreateFeedCommand): Promise { + const feedExist = await this.feedRepository.findOne({ + _organizationId: command.organizationId, + identifier: command.name, + }); + + if (feedExist) { + throw new ConflictException(`Feed with identifier: ${command.name} already exists`); + } + const item = await this.feedRepository.create({ _environmentId: command.environmentId, _organizationId: command.organizationId,