Skip to content

Commit

Permalink
Feat: Should consider the previous month before show the button to co…
Browse files Browse the repository at this point in the history
…py budget. (#305)

Co-authored-by: Felipe Soares <>
  • Loading branch information
FelipePSoares authored Jan 6, 2025
1 parent 3991962 commit 0a87e77
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
35 changes: 32 additions & 3 deletions easyfinance.client/cypress/e2e/project/detail-project.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { attempt } from "cypress/types/bluebird";
import { Expense } from "src/app/core/models/expense";

describe('EconoFlow - project detail Tests', () => {

beforeEach(() => {
attempts = 0;
});

it('should copy budget from previous month', () => {
cy.fixture('users').then((users) => {
const user = users.testUser;
Expand All @@ -10,18 +15,42 @@ describe('EconoFlow - project detail Tests', () => {

cy.intercept('GET', '**/projects*').as('getProjects')
cy.intercept('GET', '**/categories*').as('getCategories')

cy.fixture('projects').then((projects) => {
cy.visit('/projects/' + projects.defaultProject.id)

findNextMonthWithoutBudget();

cy.get('.btn-primary').contains('Copy Previous Budget').click();

cy.wait<CategoryReq, CategoryRes[]>('@getCategories').then(({ request, response }) => {
const exists = response?.body.some(category => category.expenses.some(expense => expense.budget > 0))
expect(exists).to.be.true
})
})
})
})
})

it('copy budget button should not appears when previous month is empty', () => {
cy.fixture('users').then((users) => {
const user = users.testUser;

cy.login(user.username, user.password)

cy.intercept('GET', '**/projects*').as('getProjects')
cy.intercept('GET', '**/categories*').as('getCategories')

cy.fixture('projects').then((projects) => {
cy.visit('/projects/' + projects.defaultProject.id)

findNextMonthWithoutBudget();
cy.get('#next').click()

cy.wait<CategoryReq, CategoryRes[]>('@getCategories').then(({ request, response }) => {
cy.wait<CategoryReq, CategoryRes[]>('@getCategories').then(({ request, response }) => {
cy.get('.btn-primary').should('not.exist');
});
});
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2>{{ getCurrentDate() | date: 'MMMM'}}</h2>

<button class="btn btn-primary"
(click)="copyPreviousBudget()"
*ngIf="month.budget === 0">
*ngIf="showCopyPreviousButton">
Copy Previous Budget
</button>
<div class="clearfix mt-3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { faArrowUp, faArrowDown } from '@fortawesome/free-solid-svg-icons';
import { ProjectService } from '../../../core/services/project.service';
import { CurrencyFormatPipe } from '../../../core/utils/pipes/currency-format.pipe';
import { dateUTC } from '../../../core/utils/date';

@Component({
selector: 'app-detail-project',
Expand Down Expand Up @@ -43,6 +44,7 @@ export class DetailProjectComponent implements OnInit {
month: { budget: number, waste: number, remaining: number, earned: number; } = { budget: 0, waste: 0, remaining: 0, earned: 0 };
year: { budget: number, waste: number, remaining: number, earned: number; } = { budget: 0, waste: 0, remaining: 0, earned: 0 };
buttons: string[] = [this.btnIncome, this.btnCategory];
showCopyPreviousButton = false;

constructor(private router: Router, private route: ActivatedRoute, private projectService: ProjectService, private categoryService: CategoryService, private incomeService: IncomeService) {
}
Expand Down Expand Up @@ -72,6 +74,22 @@ export class DetailProjectComponent implements OnInit {
this.month.budget = res.map(c => c.getTotalBudget()).reduce((acc, value) => acc + value, 0);
this.month.waste = res.map(c => c.getTotalWaste()).reduce((acc, value) => acc + value, 0);
this.month.remaining = res.map(c => c.getTotalRemaining()).reduce((acc, value) => acc + value, 0);

if (this.month.budget === 0) {
var newDate = dateUTC(CurrentDateComponent.currentDate);
newDate.setMonth(CurrentDateComponent.currentDate.getMonth() - 1, 1);

this.categoryService.get(this.projectId, newDate)
.pipe(map(categories => mapper.mapArray(categories, Category, CategoryDto)))
.subscribe({
next: res => {
let previousBudget = res.map(c => c.getTotalBudget()).reduce((acc, value) => acc + value, 0);
this.showCopyPreviousButton = previousBudget !== 0;
}
});
} else {
this.showCopyPreviousButton = false;
}
}
});

Expand Down

0 comments on commit 0a87e77

Please sign in to comment.