Last active
January 9, 2019 14:47
-
-
Save AntonGorelov/680d63128b63fcbb8382a12b92e5970e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpParams } from '@angular/common/http'; | |
import { FormGroup } from '@angular/forms'; | |
import { Observable } from 'rxjs'; | |
import { environment } from '@env'; | |
import { ICompany, ID, ITeamMember, IUser } from '@lib/models'; | |
import { RansackHttpUrlEncodingCodec } from './shared/ransack-http-url-encoding.codec'; | |
@Injectable() | |
export class CompaniesService { | |
constructor(private _http: HttpClient) {} | |
/** | |
* Return an object with array of all Companies. | |
* | |
* @param{number} pageNumber - number of current request page. | |
* @param{number} perPage - count of requesting pages. | |
*/ | |
public index$(pageNumber = 1, perPage = 9): Observable<ICompany[]> { | |
const params = new HttpParams() | |
.set('page_number', pageNumber.toString()) | |
.set('per_page', perPage.toString()); | |
return this._http.get<ICompany[]>(`${environment.apiUrl}/companies`, { | |
params, | |
}); | |
} | |
/** | |
* The function starts search by company's name in table. | |
* | |
* @param{HttpParams} params - The parameters of search. | |
* @param{number} page - number of current request page. | |
* @param{number} per - count of requesting pages. | |
*/ | |
public search$(params, page = 1, per = 9): Observable<ICompany[]> { | |
const httpParams = new HttpParams({ | |
encoder: new RansackHttpUrlEncodingCodec(), | |
}) | |
.set('page_number', page.toString()) | |
.set('per_page', per.toString()) | |
.set('q[country_id_eq]', params.country ? params.country : '') | |
.set('q[region_id_eq]', params.region ? params.region : '') | |
.set('q[school_id_eq]', params.school ? params.school : '') | |
.set('q[products_category_id_eq]', params.category ? params.category : '') | |
.set('q[name_cont]', params.name ? params.name : ''); | |
return this._http.get<ICompany[]>( | |
`${environment.apiUrl}/search/companies`, | |
{ | |
params: httpParams, | |
}, | |
); | |
} | |
/** | |
* Return an object with array of featured Companies. | |
*/ | |
public featured$(): Observable<ICompany[]> { | |
return this._http.get<ICompany[]>( | |
`${environment.apiUrl}/companies/featured`, | |
); | |
} | |
public update$({ id, ...company }: Partial<ICompany>): Observable<ICompany> { | |
return this._http.put<ICompany>(`${environment.apiUrl}/companies/${id}`, { | |
company: this._getCompanyParams(company), | |
}); | |
} | |
public create$({ id, ...company }: Partial<ICompany>): Observable<ICompany> { | |
return this._http.post<ICompany>( | |
`${environment.apiUrl}/companies/`, | |
this._getCompanyParams(company), | |
); | |
} | |
/** | |
* Show single company with this id. | |
* | |
* @param{ID} id - unique number of requesting company. | |
*/ | |
public show$(id: ID): Observable<ICompany> { | |
return this._http.get<ICompany>(`${environment.apiUrl}/companies/${id}`); | |
} | |
/** | |
* Delete single company with this id. | |
* | |
* @param{ID} id - unique number of requesting company. | |
*/ | |
public delete$(id: ID): Observable<ICompany> { | |
return this._http.delete<ICompany>(`${environment.apiUrl}/companies/${id}`); | |
} | |
/** | |
* Request for activation of company with this id. | |
* | |
* @param{ID} id - unique number of requesting company. | |
*/ | |
public requestActivation$(id: ID): Observable<ICompany> { | |
const path = `${environment.apiUrl}/companies/${id}/activations`; | |
return this._http.post<ICompany>(path, {}); | |
} | |
// Team Member Edit | |
public getAllRequests$(companyId: ID): Observable<ITeamMember[]> { | |
const path = `${ | |
environment.apiUrl | |
}/companies/${companyId}/team_members/requests`; | |
return this._http.get<ITeamMember[]>(path); | |
} | |
/** | |
* Send request for add to company. | |
* | |
* @param{FormGroup} form - input data from form. | |
* @param{ICompany} company - current company. | |
*/ | |
public sendUserRequest$(form: FormGroup, company: ICompany) { | |
if (form.invalid) { | |
return; | |
} | |
const userRequest = form.value; | |
const path = `${environment.apiUrl}/companies/${ | |
company.id | |
}/team_members/requests`; | |
return this._http.post(path, { membershipRequest: userRequest }); | |
} | |
/** | |
* Add user to company | |
* | |
* @param{(ITeamMember|ICompany)} object.id - unique number of current company. | |
* @param{(ITeamMember|ICompany)} object.teamMember - team members data. | |
*/ | |
public addMember$({ id, ...teamMember }: ITeamMember, company: ICompany) { | |
return this._http.post<IUser[]>( | |
`${environment.apiUrl}/companies/${company.id}/team_members`, | |
{ teamMember }, | |
); | |
} | |
/** | |
* Delete request for add to company. | |
* | |
* @param{ITeamMember} user - current team member. | |
* @param{ICompany} company - current company. | |
*/ | |
public deleteUserRequest$(user: ITeamMember, company: ICompany) { | |
return this._http.post( | |
`${environment.apiUrl}/companies/${company.id}/team_members/requests/${ | |
user.id | |
}/reject`, | |
user, | |
); | |
} | |
/** | |
* Approve request for add to company. | |
* | |
* @param{ITeamMember} user - current team member. | |
* @param{ICompany} company - current company. | |
*/ | |
public approveUserRequest$(user: ITeamMember, company: ICompany) { | |
return this._http.post( | |
`${environment.apiUrl}/companies/${company.id}/team_members/requests/${ | |
user.id | |
}/approve`, | |
user, | |
); | |
} | |
public deactivate$(companyId: ID): Observable<ICompany> { | |
return this._http.put<ICompany>( | |
`${environment.apiUrl}/companies/${companyId}/deactivate`, | |
{}, | |
); | |
} | |
public activate$(companyId: ID): Observable<ICompany> { | |
return this._http.put<ICompany>( | |
`${environment.apiUrl}/companies/${companyId}/activate`, | |
{}, | |
); | |
} | |
private _getCompanyParams({ | |
teamMembers, | |
products, | |
categories, | |
school, | |
region, | |
organization, | |
coverImage, | |
...rest | |
}: Partial<ICompany>) { | |
const newCompany: any = { | |
...rest, | |
coverImage: coverImage ? coverImage : '', | |
organizationId: organization.id, | |
}; | |
if (school && school.id) { | |
newCompany.schoolId = school.id; | |
} | |
if (region && region.id) { | |
newCompany.regionId = region.id; | |
} | |
delete newCompany.stripeConnected; | |
return newCompany; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment