Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GET /api/v1/contact endpoint #1311

Merged
merged 3 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('contact')
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,
},
});
});
});
});