-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add get environment tags endpoint
- Loading branch information
Showing
48 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
apps/api/src/app/environments-v2/dtos/get-environment-tags.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsDefined, IsString } from 'class-validator'; | ||
|
||
export class GetEnvironmentTagsDto { | ||
@ApiProperty() | ||
@IsDefined() | ||
@IsString() | ||
name: string; | ||
} |
34 changes: 34 additions & 0 deletions
34
apps/api/src/app/environments-v2/environments.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ClassSerializerInterceptor, Controller, Get, Param, UseInterceptors } from '@nestjs/common'; | ||
import { UserSessionData } from '@novu/shared'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { UserSession } from '../shared/framework/user.decorator'; | ||
import { GetEnvironmentTags, GetEnvironmentTagsCommand } from './usecases/get-environment-tags'; | ||
import { ExternalApiAccessible } from '../auth/framework/external-api.decorator'; | ||
import { ApiCommonResponses, ApiResponse } from '../shared/framework/response.decorator'; | ||
import { UserAuthentication } from '../shared/framework/swagger/api.key.security'; | ||
import { GetEnvironmentTagsDto } from './dtos/get-environment-tags.dto'; | ||
|
||
@ApiCommonResponses() | ||
@Controller({ path: `/environments`, version: '2' }) | ||
@UseInterceptors(ClassSerializerInterceptor) | ||
@UserAuthentication() | ||
@ApiTags('Environments') | ||
export class EnvironmentsController { | ||
constructor(private getEnvironmentTagsUsecase: GetEnvironmentTags) {} | ||
|
||
@Get('/:environmentId/tags') | ||
@ApiResponse(GetEnvironmentTagsDto) | ||
@ExternalApiAccessible() | ||
async getEnvironmentTags( | ||
@UserSession() user: UserSessionData, | ||
@Param('environmentId') environmentId: string | ||
): Promise<GetEnvironmentTagsDto[]> { | ||
return await this.getEnvironmentTagsUsecase.execute( | ||
GetEnvironmentTagsCommand.create({ | ||
environmentId, | ||
userId: user._id, | ||
organizationId: user.organizationId, | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EnvironmentsController } from './environments.controller'; | ||
import { GetEnvironmentTags } from './usecases/get-environment-tags'; | ||
import { SharedModule } from '../shared/shared.module'; | ||
|
||
@Module({ | ||
imports: [SharedModule], | ||
controllers: [EnvironmentsController], | ||
providers: [GetEnvironmentTags], | ||
exports: [], | ||
}) | ||
export class EnvironmentsModule {} |
3 changes: 3 additions & 0 deletions
3
...api/src/app/environments-v2/usecases/get-environment-tags/get-environment-tags.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { EnvironmentWithUserCommand } from '../../../shared/commands/project.command'; | ||
|
||
export class GetEnvironmentTagsCommand extends EnvironmentWithUserCommand {} |
53 changes: 53 additions & 0 deletions
53
apps/api/src/app/environments-v2/usecases/get-environment-tags/get-environment-tags.e2e.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { EnvironmentRepository, NotificationTemplateRepository } from '@novu/dal'; | ||
import { UserSession } from '@novu/testing'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Get Environment Tags - /v2/environments/:environmentId/tags (GET)', async () => { | ||
let session: UserSession; | ||
const environmentRepository = new EnvironmentRepository(); | ||
const notificationTemplateRepository = new NotificationTemplateRepository(); | ||
|
||
before(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
}); | ||
|
||
it('should return correct tags for the environment', async () => { | ||
await notificationTemplateRepository.create({ | ||
_environmentId: session.environment._id, | ||
tags: ['tag1', 'tag2'], | ||
}); | ||
await notificationTemplateRepository.create({ | ||
_environmentId: session.environment._id, | ||
tags: ['tag2', 'tag3', null, '', undefined], | ||
}); | ||
|
||
const { body } = await session.testAgent.get(`/v2/environments/${session.environment._id}/tags`); | ||
|
||
expect(body.data).to.be.an('array'); | ||
expect(body.data).to.have.lengthOf(3); | ||
expect(body.data).to.deep.include({ name: 'tag1' }); | ||
expect(body.data).to.deep.include({ name: 'tag2' }); | ||
expect(body.data).to.deep.include({ name: 'tag3' }); | ||
}); | ||
|
||
it('should return an empty array when no tags exist', async () => { | ||
const newEnvironment = await environmentRepository.create({ | ||
name: 'Test Environment', | ||
_organizationId: session.organization._id, | ||
}); | ||
|
||
const { body } = await session.testAgent.get(`/v2/environments/${newEnvironment._id}/tags`); | ||
|
||
expect(body.data).to.be.an('array'); | ||
expect(body.data).to.have.lengthOf(0); | ||
}); | ||
|
||
it('should throw NotFoundException for non-existent environment', async () => { | ||
const nonExistentId = '60a5f2f2f2f2f2f2f2f2f2f2'; | ||
const { body } = await session.testAgent.get(`/v2/environments/${nonExistentId}/tags`); | ||
|
||
expect(body.statusCode).to.equal(404); | ||
expect(body.message).to.equal(`Environment ${nonExistentId} not found`); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
...api/src/app/environments-v2/usecases/get-environment-tags/get-environment-tags.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { EnvironmentEntity, EnvironmentRepository, NotificationTemplateRepository } from '@novu/dal'; | ||
import { GetEnvironmentTagsCommand } from './get-environment-tags.command'; | ||
import { GetEnvironmentTagsDto } from '../../dtos/get-environment-tags.dto'; | ||
|
||
@Injectable() | ||
export class GetEnvironmentTags { | ||
constructor( | ||
private environmentRepository: EnvironmentRepository, | ||
private notificationTemplateRepository: NotificationTemplateRepository | ||
) {} | ||
|
||
async execute(command: GetEnvironmentTagsCommand): Promise<GetEnvironmentTagsDto[]> { | ||
const environment: Omit<EnvironmentEntity, 'apiKeys'> | null = await this.environmentRepository.findOne( | ||
{ | ||
_id: command.environmentId, | ||
_organizationId: command.organizationId, | ||
}, | ||
'-apiKeys' | ||
); | ||
|
||
if (!environment) throw new NotFoundException(`Environment ${command.environmentId} not found`); | ||
|
||
const notificationTemplates = await this.notificationTemplateRepository.find({ | ||
_environmentId: command.environmentId, | ||
tags: { $exists: true, $type: 'array', $ne: [] }, | ||
}); | ||
|
||
const tags = notificationTemplates.flatMap((template) => template.tags); | ||
const uniqueTags = Array.from(new Set(tags)); | ||
|
||
return this.sanitizeTags(uniqueTags); | ||
} | ||
|
||
private sanitizeTags(tags: string[]): GetEnvironmentTagsDto[] { | ||
return tags.filter((tag) => tag != null && tag !== '').map((tag) => ({ name: tag })); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
apps/api/src/app/environments-v2/usecases/get-environment-tags/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './get-environment-tags.command'; | ||
export * from './get-environment-tags.usecase'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters