-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Api): Added Feedback Form Controller
- Loading branch information
1 parent
8762354
commit f5bf431
Showing
10 changed files
with
225 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
apps/api/src/feedback/controller/feedback.controller.spec.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,25 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { FeedbackController } from './feedback.controller' | ||
import { FeedbackService } from '../service/feedback.service' | ||
import { MAIL_SERVICE } from '../../mail/services/interface.service' | ||
import { MockMailService } from '../../mail/services/mock.service' | ||
|
||
describe('FeedbackController', () => { | ||
let controller: FeedbackController | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [FeedbackController], | ||
providers: [ | ||
FeedbackService, | ||
{ provide: MAIL_SERVICE, useValue: MockMailService } | ||
] | ||
}).compile() | ||
|
||
controller = module.get<FeedbackController>(FeedbackController) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
}) |
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,16 @@ | ||
import { Controller, Post, Body } from '@nestjs/common' | ||
import { Public } from '../../decorators/public.decorator' | ||
import { FeedbackService } from '../service/feedback.service' | ||
import { ApiTags } from '@nestjs/swagger' | ||
|
||
@ApiTags('Feedback Controller') | ||
@Controller('feedback') | ||
export class FeedbackController { | ||
constructor(private readonly feedbackService: FeedbackService) {} | ||
|
||
@Public() | ||
@Post() | ||
async registerFeedback(@Body('feedback') feedback: string): Promise<void> { | ||
await this.feedbackService.registerFeedback(feedback) | ||
} | ||
} |
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,100 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { AppModule } from '../app/app.module' | ||
import { FeedbackService } from '../feedback/service/feedback.service' | ||
import { MockMailService } from '../mail/services/mock.service' | ||
import { | ||
FastifyAdapter, | ||
NestFastifyApplication | ||
} from '@nestjs/platform-fastify' | ||
import { MAIL_SERVICE } from '../mail/services/interface.service' | ||
import { FeedbackModule } from './feedback.module' | ||
import { MailModule } from '../mail/mail.module' | ||
import { PrismaService } from '../prisma/prisma.service' | ||
import { User } from '@prisma/client' | ||
import cleanUp from '../common/cleanup' | ||
|
||
describe('Feedback Controller (E2E)', () => { | ||
let app: NestFastifyApplication | ||
let feedbackService: FeedbackService | ||
let mockMailService: MockMailService | ||
let prisma: PrismaService | ||
let user: User | ||
|
||
beforeAll(async () => { | ||
const moduleRef: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule, FeedbackModule, MailModule] | ||
}) | ||
.overrideProvider(MAIL_SERVICE) | ||
.useClass(MockMailService) | ||
.compile() | ||
|
||
app = moduleRef.createNestApplication<NestFastifyApplication>( | ||
new FastifyAdapter() | ||
) | ||
feedbackService = moduleRef.get(FeedbackService) | ||
mockMailService = moduleRef.get(MAIL_SERVICE) | ||
|
||
prisma = moduleRef.get(PrismaService) | ||
|
||
await app.init() | ||
await app.getHttpAdapter().getInstance().ready() | ||
|
||
await cleanUp(prisma) | ||
|
||
user = await prisma.user.create({ | ||
data: { | ||
email: '[email protected]', | ||
name: 'John', | ||
isActive: true, | ||
isAdmin: false, | ||
isOnboardingFinished: false | ||
} | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
await cleanUp(prisma) | ||
await prisma.$disconnect() | ||
await app.close() | ||
}) | ||
|
||
it('should be defined', async () => { | ||
expect(app).toBeDefined() | ||
expect(feedbackService).toBeDefined() | ||
expect(mockMailService).toBeDefined() | ||
expect(prisma).toBeDefined() | ||
}) | ||
|
||
it('should register feedback successfully', async () => { | ||
const feedbackMessage = 'Test feedback message' | ||
|
||
const { statusCode } = await app.inject({ | ||
method: 'POST', | ||
url: '/feedback', | ||
payload: { feedback: feedbackMessage }, | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(statusCode).toBe(201) | ||
}) | ||
|
||
it('should handle empty feedback', async () => { | ||
const { statusCode, payload } = await app.inject({ | ||
method: 'POST', | ||
url: '/feedback', | ||
payload: { feedback: '' }, | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(statusCode).toBe(400) | ||
expect(JSON.parse(payload)).toEqual({ | ||
error: 'Bad Request', | ||
message: 'Feedback cannot be null', | ||
statusCode: 400 | ||
}) | ||
}) | ||
}) |
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 { Module } from '@nestjs/common' | ||
import { FeedbackService } from './service/feedback.service' | ||
import { FeedbackController } from './controller/feedback.controller' | ||
|
||
@Module({ | ||
providers: [FeedbackService], | ||
controllers: [FeedbackController] | ||
}) | ||
export class FeedbackModule {} |
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,23 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { FeedbackService } from './feedback.service' | ||
import { MAIL_SERVICE } from '../../mail/services/interface.service' | ||
import { MockMailService } from '../../mail/services/mock.service' | ||
|
||
describe('FeedbackService', () => { | ||
let service: FeedbackService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
FeedbackService, | ||
{ provide: MAIL_SERVICE, useClass: MockMailService } | ||
] | ||
}).compile() | ||
|
||
service = module.get<FeedbackService>(FeedbackService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
}) |
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,21 @@ | ||
import { BadRequestException, Inject, Injectable } from '@nestjs/common' | ||
import { | ||
IMailService, | ||
MAIL_SERVICE | ||
} from '../../mail/services/interface.service' | ||
|
||
@Injectable() | ||
export class FeedbackService { | ||
constructor( | ||
@Inject(MAIL_SERVICE) private readonly mailService: IMailService | ||
) {} | ||
|
||
async registerFeedback(feedback: string): Promise<void> { | ||
if (!feedback) { | ||
throw new BadRequestException('Feedback cannot be null') | ||
} | ||
const adminEmail = '[email protected]' | ||
|
||
await this.mailService.feedbackEmail(adminEmail, feedback) | ||
} | ||
} |
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