Skip to content

Commit

Permalink
fix: feed duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
sr-26 committed Sep 10, 2023
1 parent e741310 commit a7ef781
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions apps/api/src/app/feeds/e2e/create-feed.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,6 +9,15 @@ export class CreateFeed {
constructor(private feedRepository: FeedRepository, private createChange: CreateChange) {}

async execute(command: CreateFeedCommand): Promise<FeedEntity> {
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,
Expand Down

0 comments on commit a7ef781

Please sign in to comment.