Skip to content

Commit

Permalink
Merge pull request #175 from RJ-SMTR/feature/#167-fin-previous-days
Browse files Browse the repository at this point in the history
Feature/#167 Endpoints financeiros, dias anteriores
  • Loading branch information
williamfl2007 committed Feb 5, 2024
2 parents 4386d6d + b17ab8e commit d85065b
Show file tree
Hide file tree
Showing 61 changed files with 1,241 additions and 594 deletions.
12 changes: 6 additions & 6 deletions src/auth-licensee/auth-licensee.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import { UsersService } from 'src/users/users.service';
import { BaseValidator } from 'src/utils/validators/base-validator';
import { AuthLicenseeService } from './auth-licensee.service';

/**
* All tests below were based on the requirements on GitHub.
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('AuthLicenseeService', () => {
let authLicenseeService: AuthLicenseeService;
let jwtService: JwtService;
Expand Down Expand Up @@ -98,7 +94,9 @@ describe('AuthLicenseeService', () => {
});

describe('getInviteProfile', () => {
it('should throw exception when mail status is not SENT', async () => {
it('should throw exception when mail status is not SENT', /**
* Requirement: 2023/11/16 {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 #94, item 18 - GitHub}
*/ async () => {
// Arrange
const user = new User({
id: 1,
Expand All @@ -124,7 +122,9 @@ describe('AuthLicenseeService', () => {
});

describe('concludeRegistration', () => {
it('should set mail status to SENT when succeeded', async () => {
it('should set mail status to SENT when succeeded', /**
* Requirement: 2023/11/16 {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 #94, item 19 - GitHub}
*/ async () => {
// Arrange
const dateNow = new Date('2023-01-01T10:00:00');
const user = new User({
Expand Down
58 changes: 14 additions & 44 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Provider } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Test, TestingModule } from '@nestjs/testing';
import { ForgotService } from 'src/forgot/forgot.service';
import { InviteStatus } from 'src/mail-history-statuses/entities/mail-history-status.entity';
import { InviteStatusEnum } from 'src/mail-history-statuses/mail-history-status.enum';
import { MailHistory } from 'src/mail-history/entities/mail-history.entity';
import { MailHistoryService } from 'src/mail-history/mail-history.service';
Expand All @@ -10,16 +11,11 @@ import { MailSentInfo } from 'src/mail/interfaces/mail-sent-info.interface';
import { MailService } from 'src/mail/mail.service';
import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
import { AuthService } from './auth.service';
import { DeepPartial } from 'typeorm';
import { InviteStatus } from 'src/mail-history-statuses/entities/mail-history-status.entity';
import { AuthService } from './auth.service';

process.env.TZ = 'UTC';

/**
* All tests below were based on the requirements on GitHub.
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('AuthService', () => {
let authService: AuthService;
let usersService: UsersService;
Expand Down Expand Up @@ -92,7 +88,9 @@ describe('AuthService', () => {
});

describe('resendRegisterMail', () => {
it('should throw exception when no mail quota available', async () => {
it('should throw exception when no mail quota available', /**
* Requirement: 2023/11/16 {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 #94, item 13 - GitHub}
*/ async () => {
// Arrange
const user = new User({
id: 1,
Expand All @@ -111,41 +109,9 @@ describe('AuthService', () => {
await expect(response).rejects.toThrowError();
});

it('should throw exception when mail status is not QUEUED', async () => {
// Arrange
const user = new User({
id: 1,
email: 'user1@example.com',
hash: 'hash_1',
});
const mailHistory = new MailHistory({
id: 1,
user: user,
hash: 'hash_1',
});
mailHistory.setInviteStatus(InviteStatusEnum.sent);
jest.spyOn(usersService, 'getOne').mockResolvedValue(user);
jest.spyOn(mailHistoryService, 'findOne').mockResolvedValue(mailHistory);
jest.spyOn(mailHistoryService, 'getRemainingQuota').mockResolvedValue(1);

// Act
const response = authService.resendRegisterMail({ id: 1 });
let error: any;
await response.catch((httpException) => {
error = httpException;
});

// Assert
await expect(response).rejects.toThrowError();
await expect(error?.response?.error).toContain(
"User's mailStatus is not 'queued'",
);
});

/**
*@see {@link https://github.com/RJ-SMTR/api-cct/issues/164 Requirements #164 - GitHub}
*/
it('should return success regardless of quota status', async () => {
it('should return success regardless of quota status', /**
* Requirement: 2023/12/28 {@link https://github.com/RJ-SMTR/api-cct/issues/164#issuecomment-1871504885 #164 - GitHub}
*/ async () => {
// Arrange
const users = [
new User({ id: 1, email: 'user1@mail.com', hash: 'hash_1' }),
Expand Down Expand Up @@ -202,7 +168,9 @@ describe('AuthService', () => {
});

describe('forgotPassword', () => {
it('should throw exception when no mail quota available', async () => {
it('should throw exception when no mail quota available', /**
* Requirement: 2023/11/16 {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 #94, item 16 - GitHub}
*/ async () => {
// Arrange
jest.spyOn(mailHistoryService, 'getRemainingQuota').mockResolvedValue(0);

Expand All @@ -213,7 +181,9 @@ describe('AuthService', () => {
await expect(responsePromise).rejects.toThrowError();
});

it('should not consume the quota when available', async () => {
it('should not consume the quota when available', /**
* Requirement: 2023/11/16 {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 #94, item 17 - GitHub}
*/ async () => {
// Arrange
const user = new User({ id: 1, email: 'user1@mail.com', hash: 'hash_1' });
const mailSentInfo = {
Expand Down
110 changes: 0 additions & 110 deletions src/bank-statements/bank-statements.controller.spec.ts

This file was deleted.

115 changes: 91 additions & 24 deletions src/bank-statements/bank-statements.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ import {
Get,
HttpCode,
HttpStatus,
Param,
Query,
Request,
SerializeOptions,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiQuery, ApiTags } from '@nestjs/swagger';
import { DateApiParams } from 'src/utils/api-param/date.api-param';
import { DescriptionApiParam } from 'src/utils/api-param/description-api-param';
import { ApiBearerAuth, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { User } from 'src/users/entities/user.entity';
import { CommonApiParams } from 'src/utils/api-param/common-api-params';
import { DateApiParams } from 'src/utils/api-param/date-api-param';
import { PaginationApiParams } from 'src/utils/api-param/pagination.api-param';
import { TimeIntervalEnum } from 'src/utils/enums/time-interval.enum';
import { IRequest } from 'src/utils/interfaces/request.interface';
import { getPagination } from 'src/utils/get-pagination';
import { ParseNumberPipe } from 'src/utils/pipes/parse-number.pipe';
import { DateQueryParams } from 'src/utils/query-param/date.query-param';
import { PaginationQueryParams } from 'src/utils/query-param/pagination.query-param';
import { Pagination } from 'src/utils/types/pagination.type';
import { BankStatementsService } from './bank-statements.service';
import { IBankStatementsGet } from './interfaces/bank-statements-get.interface';
import { IBankStatementsResponse } from './interfaces/bank-statements-response.interface';
import { BSMePrevDaysTimeIntervalEnum } from './enums/bs-me-prev-days-time-interval.enum';
import { BSMeTimeIntervalEnum } from './enums/bs-me-time-interval.enum';
import { IBSGetMeDayResponse } from './interfaces/bs-get-me-day-response.interface';
import { IBSGetMePreviousDaysResponse } from './interfaces/bs-get-me-previous-days-response.interface';
import { IBSGetMeResponse } from './interfaces/bs-get-me-response.interface';

@ApiTags('BankStatements')
@Controller({
Expand All @@ -34,35 +44,92 @@ export class BankStatementsController {
@UseGuards(AuthGuard('jwt'))
@Get('me')
@ApiQuery(DateApiParams.startDate)
@ApiQuery({
name: 'endDate',
required: false,
description: DescriptionApiParam({ hours: '23:59:59.999' }),
})
@ApiQuery(DateApiParams.endDate)
@ApiQuery(DateApiParams.timeInterval)
@ApiQuery({
name: 'userId',
type: Number,
required: false,
description: DescriptionApiParam({ default: 'Your logged user id (me)' }),
})
@ApiQuery(CommonApiParams.userId)
@HttpCode(HttpStatus.OK)
async getBankStatementsFromUser(
async getMe(
@Request() request,
@Query(...DateQueryParams.startDate) startDate?: string,
@Query(...DateQueryParams.endDate) endDate?: string,
@Query(...DateQueryParams.timeInterval)
timeInterval?: TimeIntervalEnum | undefined,
@Query('userId', new ParseNumberPipe({ min: 0, required: false }))
timeInterval?: BSMeTimeIntervalEnum | undefined,
@Query('userId', new ParseNumberPipe({ min: 1, required: false }))
userId?: number | null,
): Promise<IBankStatementsResponse> {
): Promise<IBSGetMeResponse> {
const isUserIdNumber = userId !== null && !isNaN(Number(userId));
const args: IBankStatementsGet = {
return this.bankStatementsService.getMe({
startDate,
endDate,
timeInterval,
timeInterval: timeInterval
? (timeInterval as unknown as TimeIntervalEnum)
: undefined,
userId: isUserIdNumber ? userId : request.user.id,
};
return this.bankStatementsService.getBankStatementsFromUser(args);
});
}

@SerializeOptions({
groups: ['me'],
})
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@Get('me/day/:date')
@ApiParam({ name: 'date', example: '2023-01-12' })
@ApiQuery(CommonApiParams.userId)
@HttpCode(HttpStatus.OK)
async getMeDayDate(
@Request() request: IRequest,
@Param('date') date: string,
@Query('userId', new ParseNumberPipe({ min: 1, required: false }))
userId?: number | null,
): Promise<IBSGetMeDayResponse> {
const isUserIdParam = userId !== null && !isNaN(Number(userId));
return this.bankStatementsService.getMeDay({
endDate: date,
userId: isUserIdParam ? userId : (request.user as User).id,
});
}

@SerializeOptions({
groups: ['me'],
})
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@Get('me/previous-days')
@ApiQuery(PaginationApiParams.page)
@ApiQuery(PaginationApiParams.limit)
@ApiQuery(DateApiParams.startDate)
@ApiQuery(DateApiParams.endDate)
@ApiQuery(
DateApiParams.getTimeInterval(
BSMePrevDaysTimeIntervalEnum,
BSMePrevDaysTimeIntervalEnum.LAST_WEEK,
),
)
@ApiQuery(CommonApiParams.userId)
@HttpCode(HttpStatus.OK)
async getMePreviousDays(
@Request() request: IRequest,
@Query(...PaginationQueryParams.page) page: number,
@Query(...PaginationQueryParams.limit) limit: number,
@Query(...DateQueryParams.getDate('endDate', true)) endDate: string,
@Query('timeInterval') timeInterval: BSMePrevDaysTimeIntervalEnum,
@Query('userId', new ParseNumberPipe({ min: 1, required: false }))
userId?: number | null,
): Promise<Pagination<IBSGetMePreviousDaysResponse>> {
const isUserIdParam = userId !== null && !isNaN(Number(userId));
const result = await this.bankStatementsService.getMePreviousDays({
endDate: endDate,
timeInterval: timeInterval,
userId: isUserIdParam ? userId : (request.user as User).id,
});
return getPagination(
result,
{
dataLenght: result.data.length,
maxCount: result.data.length,
},
{ limit, page },
);
}
}
Loading

0 comments on commit d85065b

Please sign in to comment.