diff --git a/src/modules/contact-us/contact-us.controller.ts b/src/modules/contact-us/contact-us.controller.ts index 76d4ea924..71571d7f8 100644 --- a/src/modules/contact-us/contact-us.controller.ts +++ b/src/modules/contact-us/contact-us.controller.ts @@ -1,12 +1,11 @@ -import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common'; +import { Controller, Post, Body, HttpCode, HttpStatus, Get, Query } from '@nestjs/common'; import { ContactUsService } from './contact-us.service'; import { CreateContactDto } from '../contact-us/dto/create-contact-us.dto'; import { ApiTags } from '@nestjs/swagger'; -import { createContactDocs } from './docs/contact-us-swagger.docs'; +import { createContactDocs, getAllContactDocs } from './docs/contact-us-swagger.docs'; import { skipAuth } from '@shared/helpers/skipAuth'; @ApiTags('Contact Us') -@skipAuth() @Controller({ path: 'contact', version: '1' }) export class ContactUsController { constructor(private readonly contactUsService: ContactUsService) {} @@ -18,4 +17,11 @@ export class ContactUsController { async createContact(@Body() createContactDto: CreateContactDto) { return this.contactUsService.createContactMessage(createContactDto); } + + @Get() + @HttpCode(HttpStatus.OK) + @getAllContactDocs() + async getAllContact(@Query('page') page: number = 1, @Query('limit') limit: number = 10) { + return this.contactUsService.getAllContactMessages(page, limit); + } } diff --git a/src/modules/contact-us/contact-us.service.ts b/src/modules/contact-us/contact-us.service.ts index 9704606c5..17dec150d 100644 --- a/src/modules/contact-us/contact-us.service.ts +++ b/src/modules/contact-us/contact-us.service.ts @@ -39,4 +39,23 @@ export class ContactUsService { }, }); } + + async getAllContactMessages(page: number, limit: number) { + const [messages, total] = await this.contactRepository.findAndCount({ + order: { created_at: 'DESC' }, + skip: (page - 1) * limit, + take: limit, + }); + const totalPages = Math.ceil(total / limit); + return { + status: 'success', + message: 'Retrieved messages successfully', + data: { + currentPage: page, + totalPages, + totalResults: total, + messages, + }, + }; + } } diff --git a/src/modules/contact-us/docs/contact-us-swagger.docs.ts b/src/modules/contact-us/docs/contact-us-swagger.docs.ts index 4728a5ba6..a357f725c 100644 --- a/src/modules/contact-us/docs/contact-us-swagger.docs.ts +++ b/src/modules/contact-us/docs/contact-us-swagger.docs.ts @@ -1,5 +1,5 @@ import { applyDecorators, HttpStatus } from '@nestjs/common'; -import { ApiBearerAuth, ApiOperation, ApiProperty, ApiResponse } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiOperation, ApiProperty, ApiQuery, ApiResponse } from '@nestjs/swagger'; import { CreateContactResponseDto } from '../dto/create-contact-response.dto'; import { CreateContactErrorDto } from '../dto/create-contact-error.dto'; @@ -19,3 +19,16 @@ export function createContactDocs() { }) ); } + +export function getAllContactDocs() { + return applyDecorators( + ApiBearerAuth(), + ApiOperation({ summary: 'Get all contact messages' }), + ApiQuery({ name: 'page', required: false, type: Number, description: 'Page number' }), + ApiQuery({ name: 'limit', required: false, type: Number, description: 'Number of contact messages per page' }), + ApiResponse({ + status: HttpStatus.OK, + description: 'Successfully retrieved messages', + }) + ); +} diff --git a/src/modules/contact-us/test/contact-us.service.spec.ts b/src/modules/contact-us/test/contact-us.service.spec.ts index 30328bf73..40896ad9d 100644 --- a/src/modules/contact-us/test/contact-us.service.spec.ts +++ b/src/modules/contact-us/test/contact-us.service.spec.ts @@ -18,6 +18,7 @@ describe('ContactUsService', () => { mockRepository = { create: jest.fn(), save: jest.fn(), + findAndCount: jest.fn(), }; mockMailerService = { @@ -62,4 +63,37 @@ describe('ContactUsService', () => { expect(result).toEqual({ message: SYS_MSG.INQUIRY_SENT, status_code: HttpStatus.CREATED }); }); }); + + describe('getAllContactMessages', () => { + it('should return all contact messages paginated', async () => { + const page = 1; + const limit = 10; + const messages = [ + { id: 1, name: 'John Doe', email: 'john@example.com', phone: 123456789, message: 'Test message' }, + ]; + + const total = messages.length; + const totalPages = Math.ceil(total / limit); + + mockRepository.findAndCount.mockResolvedValue([messages, total]); + + const result = await service.getAllContactMessages(page, limit); + + expect(mockRepository.findAndCount).toHaveBeenCalledWith({ + order: { created_at: 'DESC' }, + skip: (page - 1) * limit, + take: limit, + }); + expect(result).toEqual({ + status: 'success', + message: 'Retrieved messages successfully', + data: { + currentPage: page, + totalPages: totalPages, + totalResults: total, + messages, + }, + }); + }); + }); });