Skip to content

Commit

Permalink
Merge pull request #1311 from Amyy16/feat/contact-us-messages
Browse files Browse the repository at this point in the history
feat: add GET /api/v1/contact endpoint
  • Loading branch information
TheCodeGhinux authored Mar 2, 2025
2 parents 5c1fd59 + a38ed6b commit 8f3b464
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/modules/contact-us/contact-us.controller.ts
Original file line number Diff line number Diff line change
@@ -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) {}
Expand All @@ -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);
}
}
19 changes: 19 additions & 0 deletions src/modules/contact-us/contact-us.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
}
}
15 changes: 14 additions & 1 deletion src/modules/contact-us/docs/contact-us-swagger.docs.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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',
})
);
}
34 changes: 34 additions & 0 deletions src/modules/contact-us/test/contact-us.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('ContactUsService', () => {
mockRepository = {
create: jest.fn(),
save: jest.fn(),
findAndCount: jest.fn(),
};

mockMailerService = {
Expand Down Expand Up @@ -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: '[email protected]', 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,
},
});
});
});
});

0 comments on commit 8f3b464

Please sign in to comment.