Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,90 @@ describe('CreateNewProjectDialogComponent', () => {
// Check if bcParksSections is updated correctly
expect(component.bcParksSections).toEqual([{ parentOrgUnitId: '2', name: 'Section 2' }]);
});

it('should validate latLong and set latitude and longitude correctly', () => {
component.projectForm.patchValue({
projectName: 'New Project',
latLong: '48.484245, -123.332177',
businessArea: 'Area 1',
forestRegion: 1,
forestDistrict: 2,
bcParksRegion: 3,
bcParksSection: 4,
projectLead: 'John Doe',
projectLeadEmail: 'john.doe@example.com',
siteUnitName: 'Unit 1',
closestCommunity: 'Community 1',
});

mockProjectService.createProject.and.returnValue(of({}));

component.onCreate();

expect(mockProjectService.createProject).toHaveBeenCalledWith(
jasmine.objectContaining({
latitude: 48.484245,
longitude: -123.332177,
})
);
});

it('should show an error when latLong is outside BC boundaries', () => {
component.projectForm.patchValue({
projectName: 'New Project',
businessArea: 'Area 1',
forestRegion: 1,
forestDistrict: 2,
bcParksRegion: 3,
bcParksSection: 4,
projectLead: 'John Doe',
projectLeadEmail: 'john.doe@example.com',
siteUnitName: 'Unit 1',
closestCommunity: 'Community 1',
latLong: '70.123456, -123.332177', // Invalid latitude
});

component.onCreate();

expect(mockSnackbarService.open).toHaveBeenCalledWith(
'Latitude and longitude must fall within British Columbia (Lat: 48.3–60, Long: -139 to -114).',
'OK',
{ duration: 5000, panelClass: 'snackbar-error' }
);

expect(mockProjectService.createProject).not.toHaveBeenCalled();
});


it('should handle latLong with boundary values correctly', () => {
component.projectForm.patchValue({
projectName: 'New Project',
businessArea: 'Area 1',
forestRegion: 1,
forestDistrict: 2,
bcParksRegion: 3,
bcParksSection: 4,
projectLead: 'John Doe',
projectLeadEmail: 'john.doe@example.com',
siteUnitName: 'Unit 1',
closestCommunity: 'Community 1',
latLong: '48.3, -139', // Boundary value for BC
});

mockProjectService.createProject.and.returnValue(of({}));

component.onCreate();

const expectedLatitude = 48.3;
const expectedLongitude = -139;

expect(mockProjectService.createProject).toHaveBeenCalledWith(
jasmine.objectContaining({
latitude: expectedLatitude,
longitude: expectedLongitude,
})
);
});


});
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,47 @@ export class CreateNewProjectDialogComponent implements OnInit {

onCreate(): void {
if (this.projectForm.valid) {
const latLong = this.projectForm.get('latLong')?.value ?? '';
let latitude = null;
let longitude = null;

if (latLong) {
const parts = latLong.split(',').map((part: string) => part.trim());
if (parts.length === 2) {
const lat = parseFloat(parts[0]);
const long = parseFloat(parts[1]);

// Validate latitude and longitude range for BC
if (
!isNaN(lat) &&
!isNaN(long) &&
lat >= 48.3 &&
lat <= 60 &&
long >= -139 &&
long <= -114
) {
latitude = lat;
longitude = long;
} else {
// Show an error message if latLong is outside BC's boundaries
this.snackbarService.open(
'Latitude and longitude must fall within British Columbia (Lat: 48.3–60, Long: -139 to -114).',
'OK',
{ duration: 5000, panelClass: 'snackbar-error' }
);
return;
}
} else {
// Show an error message if latLong is in an invalid format
this.snackbarService.open(
'Invalid latitude and longitude format.',
'OK',
{ duration: 5000, panelClass: 'snackbar-error' }
);
return; // Exit the method if latLong is invalid
}
}

const newProject: Project = {
projectName: this.projectForm.get('projectName')?.value ?? '',
programAreaGuid: this.projectForm.get('businessArea')?.value ?? '',
Expand Down Expand Up @@ -147,6 +188,8 @@ export class CreateNewProjectDialogComponent implements OnInit {
this.projectForm.get('totalPlannedCostPerHectare')?.value ?? '',
totalActualAmount: this.projectForm.get('totalActualAmount')?.value ?? 0,
isMultiFiscalYearProj: false,
latitude: Number(latitude),
longitude: Number(longitude),
};

this.projectService.createProject(newProject).subscribe({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ export interface Project {
totalPlannedProjectSizeHa: number;
updateDate?: string;
updateUser?: string;
latitude?: number;
longitude?: number;
}
Loading