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 { IOrganisationStaff } from '../core/IOrganisationStaff'; import { IRole } from '../core/IRole'; import { environment } from '../../environments/environment'; @Injectable({ providedIn: 'root' }) export class RoleService { private setRoleUrl = globals.serverBase + '/api/Role/SetNewRole'; private getRoleUrl = globals.serverBase + '/api/Role/GetRoles'; private editRoleUrl = globals.serverBase + '/api/Role/EditRole'; private deleteRoleUrl = globals.serverBase + '/api/Role/DeleteRole'; private getStaffNotAssignedUrl = globals.serverBase + '/api/Role/GetStaffNotAssigned'; private setRoleStaffUrl = globals.serverBase + '/api/Role/SetRoleStaff'; private deleteRoleStaffUrl = globals.serverBase + '/api/Role/DeleteRoleStaff'; constructor(private _http: Http) { } getNotAssignedStaff(organisationId:number,actionType:string,staffId:string): Observable { let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); myparams.append("staffId", staffId); myparams.append("actionType", actionType); let options = new RequestOptions({ params: myparams }); if(environment.useDummyData == false){ return this._http.get(this.getStaffNotAssignedUrl,options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } else{ var staffs:IOrganisationStaff[]=[]; var staff1:IOrganisationStaff={Id:1,Name:'Des whyte',PersonId:'aa',RoleId:0,CompanyId:0} var staff2:IOrganisationStaff={Id:2,Name:'Craig Bull',PersonId:'cc',RoleId:0,CompanyId:0} staffs.push(staff1); staffs.push(staff2); return this._http.get('') .pipe(map((response: Response) => staffs), catchError(this.handleError)) } // return this._http.get(this.getOrgTypeUrl,options) // .pipe(map((response: Response) => response.json()), // catchError(this.handleError)) } saveNewRole(organisationId:number,roleName:string,roleDescription:string=''):Observable{ if(environment.useDummyData==false){ let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); myparams.append("roleName", roleName); myparams.append("roleDescription", roleDescription); let options = new RequestOptions({ params: myparams }); return this._http.get(this.setRoleUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } else{ var role:IRole[]=[{Id:1,RoleName:'Partner',RoleDescription:'ddfdfdf',StaffCount:0,OrganisationId:1,StaffList:[]}] return this._http.get('') .pipe(map((response: Response) => role), catchError(this.handleError)) } } getRoleList(organisationId:number):Observable{ if(environment.useDummyData == false){ let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); let options = new RequestOptions({ params: myparams }); return this._http.get(this.getRoleUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } else{ var roles:IRole[]=[]; var role1:IRole={Id:1,RoleName:'Manager',RoleDescription:'ddfdfdf',StaffCount:1,OrganisationId:1,StaffList:[{CompanyId:1,Id:1,Name:'Van Le',PersonId:'1',RoleId:1},{CompanyId:1,Id:2,Name:'Vinn leong',PersonId:'2',RoleId:1}]} var role2:IRole={Id:2,RoleName:'Accountant',RoleDescription:'ddfdfdf',StaffCount:1,OrganisationId:1,StaffList:[{CompanyId:1,Id:1,Name:'Jennifer Jane',PersonId:'ddd',RoleId:1}]} roles.push(role1); roles.push(role2); return this._http.get('') .pipe(map((response: Response) => roles), catchError(this.handleError)) } } addRoleStaff(organisationId:number,staffId:string,roleId:number){ let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); myparams.append("staffId", staffId); myparams.append("roleId", roleId.toString()); let options = new RequestOptions({ params: myparams }); if(environment.useDummyData == false){ return this._http.get(this.setRoleStaffUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } return this._http.get('') .pipe( catchError(this.handleError)) } editStaff(staff:IOrganisationStaff){ return this._http.get('') .pipe( catchError(this.handleError)) } deleteRoleStaff(staffId:string,roleId:number, organisationId:number){ let myparams = new URLSearchParams(); myparams.append("staffId", staffId); myparams.append("roleId", roleId.toString()); myparams.append("organisationId", organisationId.toString()); let options = new RequestOptions({ params: myparams }); return this._http.get(this.deleteRoleStaffUrl,options) .pipe( catchError(this.handleError)) } editRole(organisationId:number,roleId:number, name:string, description:string){ let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); myparams.append("roleName", name); myparams.append("roleDescription", description); myparams.append("roleId", roleId.toString()); let options = new RequestOptions({ params: myparams }); if(environment.useDummyData == false){ return this._http.get(this.editRoleUrl,options) .pipe( catchError(this.handleError)) } else{ return this._http.get('') .pipe( catchError(this.handleError)) } } deleteRole(organisationId:number,roleId:number){ let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); myparams.append("roleId", roleId.toString()); let options = new RequestOptions({ params: myparams }); if(environment.useDummyData == false){ return this._http.get(this.deleteRoleUrl,options) .pipe( catchError(this.handleError)) } else{ return this._http.get('') .pipe( 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'); } }