import { Injectable } from '@angular/core'; import { Http, Response, URLSearchParams, RequestOptions, Headers } from '@angular/http'; import * as globals from '../core/common'; import { Observable } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import { IOrganisation } from '../core/IOrganisation'; import { IOrganisationType } from '../core/IOrganisationType'; import { IIndustry } from '../core/IIndustry'; import { ICompanyConatctDetails } from '../core/ICompanyContactDetails'; import { IAddress } from '../core/IAddress'; import { IState } from '../core/IState'; import { ICountry } from '../core/ICountry'; import { TreeNode } from 'primeng/api'; import { IPreference } from '../core/IPreference'; import { environment } from '../../environments/environment'; import { INewOrganisation } from '../core/INewOrganisation'; import { IFilterKey } from '../core/IFilterKey'; @Injectable({ providedIn: 'root' }) export class OrganisationService { //------------------- Web API URL sections --------------------- private getOrganisationsUrl = globals.serverBase + '/api/Company/GetCompanies'; private getOrgTypeUrl = globals.serverBase + '/api/Company/GetCompanyTypes'; private getIndustriesUrl = globals.serverBase + '/api/Company/GetIndustries'; private setOrgTypeUrl = globals.serverBase + '/api/Company/SetCompanyTypes'; private setIndustriesUrl = globals.serverBase + '/api/Company/SetIndustries'; private setOragnisationContactUrl = globals.serverBase + '/api/Company/GetCompanyContact'; private setOragnisationAddressUrl = globals.serverBase + '/api/Company/GetCompanyAddress'; private getOrganisationPreferenceUrl = globals.serverBase + '/api/Company/GetCompanyPreference'; private getUnassignedStaffListUrl = globals.serverBase + '/api/Company/GetUnassignedStaff'; private setOrganisationChartStaffUrl = globals.serverBase + '/api/Company/SetOrganisationChartStaff'; private getOrganisationChartStaffListUrl = globals.serverBase + '/api/Company/GetOrganisationChartStaff'; private setOrganisationUrl = globals.serverBase + '/api/Company/SetOrganisation'; private setOrganisationProfileUrl = globals.serverBase + '/api/Company/SetOrganisationProfile'; private imageUploadUrl= globals.serverBase +'/api/Company/UploadLogo'; constructor(private _http: Http) { } getOrganisations(id: string,filterKeys:IFilterKey[]): Observable { let myparams = new URLSearchParams(); myparams.append("id", id); myparams.append("filterKeys", JSON.stringify(filterKeys)); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.getOrganisationsUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } //dummy data section var organisations: IOrganisation[] = []; var org1: IOrganisation = { Id: 1, CompanyName: 'Whytes Chartered Accountants', Staff: 25, Type: 'Internal', Country: 'Australia', Email: 'info@whytes.com.au', Website: 'https://whytes.com.au', Phone: '+6123334', TypeId: 1, IndustryId: 1, Industry: 'Accounting', LogoUrl: "https://www.whytes.com.au/images/whytes-logo-icon-large.png", PublicProfileNote: "

public profile details

", PrivateProfileNote: "private profile details", KeyWord: "keyword 1, keyword 2", localisationId: 1, FirstName: 'Des', LastName: 'Whytes', Fax: '+5452558' } var org2: IOrganisation = { Id: 2, CompanyName: 'cloud', Staff: 10, Type: 'Outsource', Country: 'Australia', Email: 'info@cloud.com.au', Website: 'https://cloud.com.au', Phone: '+61212334', TypeId: 2, IndustryId: 2, Industry: 'Accounting', LogoUrl: "https://www.whytes.com.au/images/whytes-logo-icon-large.png", PublicProfileNote: "public profile details", PrivateProfileNote: "private profile details", KeyWord: "keyword 1, keyword 2", localisationId: 2, FirstName: 'Leo', LastName: 'Emmanuel', Fax: '+78512565' } organisations.push(org1); organisations.push(org2); return this._http.get('', options) .pipe(map((response: Response) => organisations), catchError(this.handleError)) } getOrganisationTypes(): Observable { var organisationTypes: IOrganisationType[] = []; // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.getOrgTypeUrl) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var type1: IOrganisationType = { Id: 1, TypeName: 'Internal' } var type2: IOrganisationType = { Id: 2, TypeName: 'External' } organisationTypes.push(type1); organisationTypes.push(type2); return this._http.get('') .pipe(map((response: Response) => organisationTypes), catchError(this.handleError)) } getIndustries(): Observable { var industries: IIndustry[] = []; // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.getIndustriesUrl) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var type1: IIndustry = { Id: 1, Industry: 'Accounting' } var type2: IIndustry = { Id: 2, Industry: 'Marketing' } industries.push(type1); industries.push(type2); return this._http.get('') .pipe(map((response: Response) => industries), catchError(this.handleError)) } saveOrganisationType(title: string): Observable { let myparams = new URLSearchParams(); myparams.append("title", title); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.setOrgTypeUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var organisationTypes: IOrganisationType[] = []; var type1: IOrganisationType = { Id: 1, TypeName: 'Internal' } var type2: IOrganisationType = { Id: 2, TypeName: 'External' } organisationTypes.push(type1); organisationTypes.push(type2); return this._http.get('') .pipe(map((response: Response) => organisationTypes), catchError(this.handleError)) } saveIndustryType(title: string): Observable { let myparams = new URLSearchParams(); myparams.append("title", title); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.setIndustriesUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var industries: IIndustry[] = []; var type1: IIndustry = { Id: 1, Industry: 'Accounting' } var type2: IIndustry = { Id: 2, Industry: 'Marketing' } industries.push(type1); industries.push(type2); return this._http.get('') .pipe(map((response: Response) => industries), catchError(this.handleError)) } getOrgContactDetails(id: string): Observable { let myparams = new URLSearchParams(); myparams.append("id", id); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.setOragnisationContactUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var companyContact: ICompanyConatctDetails; companyContact = { Id: 1, FirstName: "Craig", LastName: "Bull", Email: "info@whytes.com.au", Phone: "+64125369", Fax: "+6985478", WebSiteUrl: "https://whytes.com.au", AddressId: 0 }; return this._http.get('', options) .pipe(map((response: Response) => companyContact), catchError(this.handleError)) } getOrgAddress(id: string): Observable { let myparams = new URLSearchParams(); myparams.append("id", id); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.setOragnisationAddressUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var companyAddress: IAddress; var state: IState = { Id: 1, CountryId: 1, StateName: "Western Australia" }; var country: ICountry = { Id: 1, CountryName: "Australia" }; companyAddress = { Id: 1, StaffId: 1, DoorNumber: 12, StreetName: "Level 1", StreetName2: "22 Delhi Street", City: "West Perth", PostCode: "6005", StateId: 1, State: state, CountryId: 1, Country: country }; return this._http.get('', options) .pipe(map((response: Response) => companyAddress), catchError(this.handleError)) } saveOrganisationDetails(organisation: IOrganisation): Observable { // return this._http.post('', organisation) // .pipe(map((response: Response) => response.json()), // catchError(this.handleError)) // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { var newOrganisation:INewOrganisation = {Organisation:organisation,Localisation:null,Preference:null}; //newOrganisation.Organisation = organisation; return this._http.post(this.setOrganisationUrl, newOrganisation) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } else{ var org1: IOrganisation = { Id: 1, CompanyName: 'XYZ Accounting PH outsource', Staff: 0, Type: 'Affiliate', Country: 'Australia', Email: 'info@whytes.com.au', Website: 'https://whytes.com.au', Phone: '+6123334', TypeId: 1, IndustryId: 1, Industry: 'Accounting', LogoUrl: "https://www.whytes.com.au/images/whytes-logo-icon-large.png", } return this._http.get('') .pipe(map((response: Response) => 17), catchError(this.handleError)) } } saveOrganisation(newOrganisation: INewOrganisation) { // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.post(this.setOrganisationUrl, newOrganisation) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } } saveOrganisationProfile(Organisation: IOrganisation) { // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.post(this.setOrganisationProfileUrl, Organisation) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } } //this will call api and return unassigned staff list to component getUnassignedStaffList(id: string): Observable { let myparams = new URLSearchParams(); myparams.append("organisationId", id); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.getUnassignedStaffListUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var unassignedList = [ { key: "1", label: 'CEO', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'Alexandro robeiru', 'avatar': 'assets/default.png' }, children: [] }, { key: "2", label: 'CFO', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'B', 'avatar': 'assets/default.png' }, children: [] }, { key: "3", label: 'CTO', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'C', 'avatar': 'assets/default.png' }, children: [] }, { key: "4", label: 'Manager', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'D', 'avatar': 'assets/default.png' }, children: [] }, { key: "5", label: 'Manager', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'E', 'avatar': 'assets/default.png' }, children: [] }, { key: "6", label: 'Manager', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'F', 'avatar': 'assets/default.png' }, children: [] }, { key: "7", label: 'Accountant', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'G', 'avatar': 'assets/default.png' }, children: [] }, { key: "8", label: 'Accountant', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'H', 'avatar': 'assets/default.png' }, children: [] }, { key: "9", label: 'Partner', type: 'person', styleClass: 'ui-person', expanded: true, data: { name: 'I', 'avatar': 'assets/default.png' }, children: [] } ]; return this._http.get('') .pipe(map((response: Response) => unassignedList), catchError(this.handleError)) } //this will call api and return current organisation chart data to component getOrganisationChartStaffList(id: string) { let myparams = new URLSearchParams(); myparams.append("organisationId", id); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.getOrganisationChartStaffListUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } var organisationChartDataList = []; return this._http.get('') .pipe(map((response: Response) => organisationChartDataList), catchError(this.handleError)) } saveOrganisationChartStaffList(OrganisationData :TreeNode[]){ // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.post(this.setOrganisationChartStaffUrl, OrganisationData) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } } getOrganisationPreferences(organisationId: number) { let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); let options = new RequestOptions({ params: myparams }); // this 'if' will switch services to use actual data and dummy data if (environment.useDummyData == false) { return this._http.get(this.getOrganisationPreferenceUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } else { var peference: IPreference = { AccessToPersonalise: true, AlertForgetNotify: true, SortInDailyReport: 1, UpcomingDays: 14, ReceiveReportTime: 17 } return this._http.get('') .pipe(map((response: Response) => peference), catchError(this.handleError)) } } uploadFile(fileToUpload:File){ const formData = new FormData(); formData.append('file', fileToUpload, fileToUpload.name); return this._http.post(this.imageUploadUrl, formData) .pipe(map((response: Response) =>response.json()), catchError(this.handleError)) } //The function of handling the error private handleError(error: Response) { console.log("Error in contact service"); console.log(error); return Observable.throw(error.json().error || 'Server error'); } }