Skip to content

Commit fa8ebf2

Browse files
committed
fix: #154 Filter projects and project types by customer
1 parent 2a590ed commit fa8ebf2

22 files changed

+83
-59
lines changed

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
44

55
import { CreateCustomerComponent } from './create-customer';
66
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
7-
import { LoadCustomers, ResetCustomerToEdit, UpdateCustomer } from './../../../../store/customer-management.actions';
7+
import { ResetCustomerToEdit, UpdateCustomer } from './../../../../store/customer-management.actions';
88
import { Customer } from 'src/app/modules/shared/models';
99

1010
describe('CreateCustomerComponent', () => {
@@ -17,6 +17,7 @@ describe('CreateCustomerComponent', () => {
1717
isLoading: false,
1818
message: '',
1919
customerIdToEdit: '',
20+
customerId: '',
2021
};
2122

2223
const customerData: Customer = {
@@ -69,9 +70,8 @@ describe('CreateCustomerComponent', () => {
6970

7071
component.onSubmit(customerData);
7172

72-
expect(store.dispatch).toHaveBeenCalledTimes(2);
73+
expect(store.dispatch).toHaveBeenCalledTimes(1);
7374
expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData));
74-
expect(store.dispatch).toHaveBeenCalledWith(new LoadCustomers());
7575
});
7676

7777
it('should call resetCustomerForm', () => {

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { Customer } from 'src/app/modules/shared/models';
88
import {
99
CustomerState,
1010
CreateCustomer,
11-
LoadCustomers,
1211
UpdateCustomer,
1312
ResetCustomerToEdit,
1413
} from 'src/app/modules/customer-management/store';
14+
import { LoadProjectTypes } from '../../../projects-type/store';
15+
import { LoadProjects } from '../../../projects/components/store/project.actions';
1516

1617
@Component({
1718
selector: 'app-create-customer',
@@ -64,7 +65,6 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
6465
this.customerForm.reset();
6566
} else {
6667
this.store.dispatch(new CreateCustomer(customerData));
67-
this.store.dispatch(new LoadCustomers());
6868
}
6969
this.showAlert = true;
7070
setTimeout(() => (this.showAlert = false), 3000);
@@ -85,6 +85,8 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
8585

8686
setDataToUpdate(customerData: Customer) {
8787
if (customerData) {
88+
this.store.dispatch(new LoadProjectTypes(customerData.id));
89+
this.store.dispatch(new LoadProjects(customerData.id));
8890
this.changeValueAreTabsActives.emit(true);
8991
this.customerForm.setValue({
9092
name: customerData.name,

src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('CustomerTableListComponent', () => {
1717
isLoading: false,
1818
message: '',
1919
customerIdToEdit: '',
20+
customerId: ''
2021
};
2122

2223
beforeEach(async(() => {

src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import {
1212
ResetProjectTypeToEdit,
1313
getProjectTypeById,
1414
} from '../../store';
15+
import { getCustomerId } from 'src/app/modules/customer-management/store/customer-management.selectors';
1516
import { ProjectType } from '../../../../../shared/models/project-type.model';
1617

1718
describe('InputProjectTypeComponent', () => {
1819
let component: CreateProjectTypeComponent;
1920
let fixture: ComponentFixture<CreateProjectTypeComponent>;
2021
let store: MockStore<ProjectTypeState>;
2122
let projectTypeIdToEditMock;
23+
let getCustomerIdMock;
2224
let allProjectTypesMock;
2325
let getProjectTypeByIdMock;
2426
let getProjectTypeByIdSelectorMock;
@@ -68,12 +70,13 @@ describe('InputProjectTypeComponent', () => {
6870

6971
it('should reset form onSubmit and dispatch UpdateProjectType action', () => {
7072
const currentState = {
71-
data: [{ id: '1', name: 'xxx', description: 'xxxx' }],
73+
data: [{ id: '1', name: 'xxx', description: 'xxxx', customerId: component.customerId }],
7274
isLoading: false,
7375
message: '',
7476
projectTypeIdToEdit: '1',
7577
};
7678

79+
getCustomerIdMock = store.overrideSelector(getCustomerId, 'xyz');
7780
projectTypeIdToEditMock = store.overrideSelector(projectTypeIdToEdit, currentState.projectTypeIdToEdit);
7881
allProjectTypesMock = store.overrideSelector(allProjectTypes, currentState.data);
7982
getProjectTypeByIdMock = store.overrideSelector(allProjectTypesMock, projectTypeIdToEditMock);
@@ -106,12 +109,16 @@ describe('InputProjectTypeComponent', () => {
106109

107110
spyOn(component.projectTypeForm, 'reset');
108111
spyOn(store, 'dispatch');
109-
112+
component.customerId = '';
110113
component.onSubmit(projectType);
114+
const projectTypeData = {
115+
...projectType,
116+
customer_id: '',
117+
};
111118

112119
expect(component.projectTypeForm.reset).toHaveBeenCalled();
113120
expect(store.dispatch).toHaveBeenCalledTimes(1);
114-
expect(store.dispatch).toHaveBeenCalledWith(new CreateProjectType(projectType));
121+
expect(store.dispatch).toHaveBeenCalledWith(new CreateProjectType(projectTypeData));
115122
});
116123

117124
it('should get name using projectTypeForm', () => {

src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Store, select } from '@ngrx/store';
55
import { ProjectType } from '../../../../../shared/models';
66
import { ProjectTypeState } from '../../store';
77
import { CreateProjectType, ResetProjectTypeToEdit, UpdateProjectType, getProjectTypeById } from '../../store';
8+
import { getCustomerId } from 'src/app/modules/customer-management/store/customer-management.selectors';
89

910
@Component({
1011
selector: 'app-create-project-type',
@@ -14,6 +15,7 @@ import { CreateProjectType, ResetProjectTypeToEdit, UpdateProjectType, getProjec
1415
export class CreateProjectTypeComponent implements OnInit {
1516
projectTypeForm: FormGroup;
1617
projectTypeToEdit: ProjectType;
18+
customerId: string;
1719

1820
constructor(private formBuilder: FormBuilder, private store: Store<ProjectTypeState>) {
1921
this.projectTypeForm = this.formBuilder.group({
@@ -28,12 +30,15 @@ export class CreateProjectTypeComponent implements OnInit {
2830
this.projectTypeToEdit = projectType;
2931
this.setDataToUpdate(this.projectTypeToEdit);
3032
});
33+
const getCustomerId$ = this.store.pipe(select(getCustomerId));
34+
getCustomerId$.subscribe((customerId) => {
35+
this.customerId = customerId;
36+
});
3137
}
3238

3339
get name() {
3440
return this.projectTypeForm.get('name');
3541
}
36-
3742
get description() {
3843
return this.projectTypeForm.get('description');
3944
}
@@ -49,15 +54,14 @@ export class CreateProjectTypeComponent implements OnInit {
4954

5055
onSubmit(projectTypeData) {
5156
this.projectTypeForm.reset();
52-
5357
if (this.projectTypeToEdit) {
5458
const projectType = {
5559
...projectTypeData,
5660
id: this.projectTypeToEdit.id,
5761
};
5862
this.store.dispatch(new UpdateProjectType(projectType));
5963
} else {
60-
this.store.dispatch(new CreateProjectType(projectTypeData));
64+
this.store.dispatch(new CreateProjectType({ ...projectTypeData, customer_id: this.customerId }));
6165
this.projectTypeForm.get('description').setValue('');
6266
}
6367
}

src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ describe('ProjectTypeTableListComponent', () => {
4545
expect(component).toBeTruthy();
4646
});
4747

48-
it('onInit, LoadProjecttypes action is dispatched', () => {
49-
spyOn(store, 'dispatch');
50-
51-
component.ngOnInit();
52-
expect(store.dispatch).toHaveBeenCalled();
53-
});
54-
5548
it('onInit, projectTypes field is populated with data from store', () => {
5649
component.ngOnInit();
5750
expect(component.projectTypes).toBe(state.data);

src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
22
import { ITEMS_PER_PAGE } from 'src/environments/environment';
33
import { Store, select } from '@ngrx/store';
44

5-
import { LoadProjectTypes, DeleteProjectType, SetProjectTypeToEdit, allProjectTypes } from '../../store';
5+
import { DeleteProjectType, SetProjectTypeToEdit, allProjectTypes } from '../../store';
66
import { ProjectTypeState } from '../../store';
77
import { ProjectType } from '../../../../../shared/models';
88

@@ -20,7 +20,6 @@ export class ProjectTypeListComponent implements OnInit {
2020
constructor(private store: Store<ProjectTypeState>) {}
2121

2222
ngOnInit(): void {
23-
this.store.dispatch(new LoadProjectTypes());
2423
const projectTypes$ = this.store.pipe(select(allProjectTypes));
2524
projectTypes$.subscribe((response) => {
2625
this.projectTypes = response;

src/app/modules/customer-management/components/projects-type/services/project-type.service.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ describe('Activity Service', () => {
3333

3434
it('projectTypes are read using GET from baseUrl', () => {
3535
const projectTypesFoundSize = projectTypes.length;
36-
service.baseUrl = 'foo';
37-
service.getProjectTypes().subscribe((projecttypesInResponse) => {
36+
service.baseUrl = '/project-types';
37+
service.getProjectTypes({ customerId: 'xyz' }).subscribe((projecttypesInResponse) => {
3838
expect(projecttypesInResponse.length).toBe(projectTypesFoundSize);
3939
});
40-
const getProjectTypesRequest = httpMock.expectOne(service.baseUrl);
40+
const getProjectTypesRequest = httpMock.expectOne(`${service.baseUrl}?customer_id=xyz`);
4141
expect(getProjectTypesRequest.request.method).toBe('GET');
4242
getProjectTypesRequest.flush(projectTypes);
4343
});
@@ -58,7 +58,9 @@ describe('Activity Service', () => {
5858
it('ProjectTypes are delete using DELETE from baseUrl', () => {
5959
const url = `${service.baseUrl}/1`;
6060
service.deleteProjectType(projectTypes[0].id).subscribe((projectTypesInResponse) => {
61-
expect(projectTypesInResponse.filter((activity) => activity.id !== projectTypes[0].id)).toEqual([projectTypes[1]]);
61+
expect(projectTypesInResponse.filter((activity) => activity.id !== projectTypes[0].id)).toEqual([
62+
projectTypes[1],
63+
]);
6264
});
6365
const getProjectTypesRequest = httpMock.expectOne(url);
6466
expect(getProjectTypesRequest.request.method).toBe('DELETE');

src/app/modules/customer-management/components/projects-type/services/project-type.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { HttpClient } from '@angular/common/http';
2+
import { HttpClient, HttpParams } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44

55
import { environment } from '../../../../../../environments/environment';
@@ -13,8 +13,9 @@ export class ProjectTypeService {
1313

1414
constructor(private http: HttpClient) {}
1515

16-
getProjectTypes(): Observable<ProjectType[]> {
17-
return this.http.get<ProjectType[]>(this.baseUrl);
16+
getProjectTypes(customerId: any): Observable<ProjectType[]> {
17+
const params = new HttpParams().set('customer_id', customerId.customerId);
18+
return this.http.get<ProjectType[]>(this.baseUrl, { params });
1819
}
1920

2021
createProjectType(projectTypeData): Observable<any> {

src/app/modules/customer-management/components/projects-type/store/project-type.actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export enum ProjectTypeActionTypes {
2222

2323
export class LoadProjectTypes implements Action {
2424
public readonly type = ProjectTypeActionTypes.LOAD_PROJECT_TYPES;
25+
constructor(public customerId?: string) {}
2526
}
2627

2728
export class LoadProjectTypesSuccess implements Action {

0 commit comments

Comments
 (0)