2017-05-09 13:39:48 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2017-11-15 14:16:05 +00:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
2017-05-09 13:39:48 +01:00
|
|
|
import { Observable } from 'rxjs/Rx';
|
2017-07-27 17:07:41 +01:00
|
|
|
import { environment } from '../../environments/environment';
|
2017-05-09 13:39:48 +01:00
|
|
|
import 'rxjs/add/operator/map';
|
|
|
|
|
|
|
|
/* this provider handles the interaction between server and client */
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-05-09 13:39:48 +01:00
|
|
|
@Injectable()
|
|
|
|
export class ApiService {
|
2017-07-27 17:07:41 +01:00
|
|
|
private apiUrl = environment.apiUrl;
|
2017-06-05 18:47:34 +01:00
|
|
|
private sessionKey: string = null;
|
2017-05-09 13:39:48 +01:00
|
|
|
constructor(
|
2017-11-15 14:12:28 +00:00
|
|
|
private http: HttpClient,
|
2017-06-05 18:47:34 +01:00
|
|
|
) {
|
2017-09-08 14:49:57 +01:00
|
|
|
if (localStorage.getItem('sessionKey') ) {
|
|
|
|
this.sessionKey = localStorage.getItem('sessionKey');
|
|
|
|
}
|
2017-06-05 18:47:34 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-09-07 15:35:55 +01:00
|
|
|
public post(url: string, data: any = {}) {
|
2017-09-08 15:22:15 +01:00
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-08-29 17:39:27 +01:00
|
|
|
this.apiUrl + url,
|
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-08-29 17:39:27 +01:00
|
|
|
}
|
|
|
|
|
2017-06-15 16:32:13 +01:00
|
|
|
// Login API
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-08-29 18:15:47 +01:00
|
|
|
public getSessionKey() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('get key');
|
|
|
|
return this.sessionKey;
|
2017-06-05 18:47:34 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-08-29 18:15:47 +01:00
|
|
|
public setSessionKey(key) {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('set key');
|
|
|
|
this.sessionKey = key;
|
|
|
|
localStorage.setItem('sessionKey', this.sessionKey);
|
2017-06-05 18:47:34 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-08-29 18:15:47 +01:00
|
|
|
public removeSessionKey() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('remove key');
|
|
|
|
this.sessionKey = null;
|
|
|
|
localStorage.removeItem('sessionKey');
|
2017-06-05 18:47:34 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-05-09 13:39:48 +01:00
|
|
|
public register(data) {
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-05-09 13:39:48 +01:00
|
|
|
this.apiUrl + '/register',
|
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-05-09 13:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public login(data) {
|
2017-09-08 14:59:31 +01:00
|
|
|
return this.http
|
2017-11-15 14:46:04 +00:00
|
|
|
.post<any>(
|
2017-09-08 14:59:31 +01:00
|
|
|
this.apiUrl + '/login',
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.map(
|
|
|
|
result => {
|
2017-11-15 14:46:04 +00:00
|
|
|
const json = result;
|
2017-09-08 14:59:31 +01:00
|
|
|
this.setSessionKey(json.session_key);
|
|
|
|
this.setUserInfo(
|
|
|
|
json.email,
|
|
|
|
json.display_name || json.name
|
2017-07-28 19:30:35 +01:00
|
|
|
);
|
2017-09-08 14:59:31 +01:00
|
|
|
this.setUserType(json.user_type);
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
);
|
2017-05-09 13:39:48 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-09-08 14:49:57 +01:00
|
|
|
public logout() {
|
|
|
|
console.log(this.sessionKey);
|
|
|
|
const key = this.sessionKey;
|
2017-09-08 15:22:15 +01:00
|
|
|
return this.http
|
2017-11-15 14:46:04 +00:00
|
|
|
.post<any>(
|
2017-09-08 15:22:15 +01:00
|
|
|
this.apiUrl + '/logout',
|
|
|
|
{ session_key : key },
|
|
|
|
)
|
|
|
|
.map(
|
|
|
|
response => {
|
|
|
|
localStorage.clear();
|
|
|
|
this.sessionKey = null;
|
2017-11-15 14:46:04 +00:00
|
|
|
return response;
|
2017-09-08 15:22:15 +01:00
|
|
|
}
|
|
|
|
);
|
2017-09-08 14:49:57 +01:00
|
|
|
}
|
2017-05-09 13:39:48 +01:00
|
|
|
|
2017-09-01 14:36:31 +01:00
|
|
|
// Submits feedback
|
|
|
|
|
|
|
|
public feedback(data) {
|
|
|
|
data.app_name = 'Foodloop Web';
|
|
|
|
data.package_name = 'Foodloop Web';
|
2017-09-01 15:02:05 +01:00
|
|
|
data.version_code = 'dev';
|
|
|
|
data.version_number = 'dev';
|
2017-09-01 14:36:31 +01:00
|
|
|
console.log(data);
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-01 14:36:31 +01:00
|
|
|
this.apiUrl + '/feedback',
|
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-01 14:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 16:05:35 +01:00
|
|
|
// gets transaction list for log
|
|
|
|
|
|
|
|
public transList(data) {
|
2017-09-08 14:49:57 +01:00
|
|
|
const key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-08 14:49:57 +01:00
|
|
|
this.apiUrl + '/outgoing-transactions',
|
|
|
|
{
|
2017-09-06 16:05:35 +01:00
|
|
|
session_key : key,
|
|
|
|
page : data
|
|
|
|
}
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-06 16:05:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 18:15:47 +01:00
|
|
|
// Searches organisations used for transaction submission
|
2017-08-31 18:44:17 +01:00
|
|
|
|
2017-05-09 13:39:48 +01:00
|
|
|
public search(data) {
|
2017-09-08 14:49:57 +01:00
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-08 14:49:57 +01:00
|
|
|
this.apiUrl + '/search',
|
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-05-09 13:39:48 +01:00
|
|
|
}
|
2017-08-31 18:44:17 +01:00
|
|
|
|
2017-08-29 18:15:47 +01:00
|
|
|
// Uploads a transaction
|
2017-08-31 18:44:17 +01:00
|
|
|
|
2017-08-29 18:15:47 +01:00
|
|
|
public upload(data) {
|
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-08-29 18:15:47 +01:00
|
|
|
this.apiUrl + '/upload',
|
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-08-29 18:15:47 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-09-19 15:38:35 +01:00
|
|
|
// gets payroll list for log
|
|
|
|
|
|
|
|
public payrollList(data) {
|
|
|
|
const key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-19 15:38:35 +01:00
|
|
|
this.apiUrl + '/v1/organisation/payroll',
|
|
|
|
{
|
|
|
|
session_key : key,
|
|
|
|
page : data
|
|
|
|
}
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-19 15:38:35 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:54:34 +01:00
|
|
|
// handles Org data added
|
|
|
|
|
|
|
|
public orgPayroll(data) {
|
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-19 15:38:35 +01:00
|
|
|
this.apiUrl + '/v1/organisation/payroll/add',
|
2017-09-08 11:54:34 +01:00
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-08 11:54:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public orgSupplier(data) {
|
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-19 15:38:35 +01:00
|
|
|
this.apiUrl + '/v1/organisation/supplier/add',
|
2017-09-08 11:54:34 +01:00
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-08 11:54:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public orgEmployee(data) {
|
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-19 15:38:35 +01:00
|
|
|
this.apiUrl + '/v1/organisation/employee/add',
|
2017-09-08 11:54:34 +01:00
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-08 11:54:34 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 19:30:35 +01:00
|
|
|
// Handles user data interaction
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-07-28 19:30:35 +01:00
|
|
|
// Checks for login status
|
|
|
|
|
|
|
|
public hasLoggedIn() {
|
|
|
|
return this.getSessionKey() ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pulls user info to store locally on login
|
|
|
|
|
|
|
|
public setUserInfo(
|
|
|
|
email: string,
|
|
|
|
display_name: string) {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('set UserInfo');
|
|
|
|
localStorage.setItem('email', email);
|
|
|
|
localStorage.setItem('displayname', display_name);
|
2017-07-28 19:30:35 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-08-31 18:44:17 +01:00
|
|
|
// Sets usertype
|
|
|
|
|
|
|
|
public setUserType(user_type: string) {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('set UserType');
|
|
|
|
localStorage.setItem('usertype', user_type);
|
2017-08-31 18:44:17 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 19:30:35 +01:00
|
|
|
// Used for getting account details and updating
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-07-28 19:30:35 +01:00
|
|
|
public accountFullLoad() {
|
2017-09-08 14:49:57 +01:00
|
|
|
const key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-07-28 19:30:35 +01:00
|
|
|
this.apiUrl + '/user',
|
|
|
|
{ session_key : key },
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-07-28 19:30:35 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-07-28 19:30:35 +01:00
|
|
|
public accountEditUpdate(data) {
|
2017-09-08 14:49:57 +01:00
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-07-28 19:30:35 +01:00
|
|
|
this.apiUrl + '/user/account',
|
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-07-28 19:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes account details on logout
|
|
|
|
|
|
|
|
public removeUserInfo() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('remove UserInfo');
|
2017-07-28 19:30:35 +01:00
|
|
|
localStorage.removeItem('email');
|
|
|
|
localStorage.removeItem('displayname');
|
|
|
|
}
|
|
|
|
|
|
|
|
public getFullName() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('get Full Name');
|
2017-07-28 19:30:35 +01:00
|
|
|
localStorage.getItem('fullname');
|
|
|
|
}
|
|
|
|
|
|
|
|
public getDisplayName() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('get Display Name');
|
2017-07-28 19:30:35 +01:00
|
|
|
localStorage.getItem('displayname');
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPostcode() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('get Postcode');
|
2017-07-28 19:30:35 +01:00
|
|
|
localStorage.getItem('postcode');
|
|
|
|
}
|
|
|
|
|
|
|
|
public getYearOfBirth() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('get Year of Birth');
|
2017-07-28 19:30:35 +01:00
|
|
|
localStorage.getItem('yearofbirth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public getEmail() {
|
2017-09-08 14:49:57 +01:00
|
|
|
console.log('get email');
|
2017-07-28 19:30:35 +01:00
|
|
|
localStorage.getItem('email');
|
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-06-15 16:32:13 +01:00
|
|
|
// Leaderboard Api
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-11-10 17:15:11 +00:00
|
|
|
public leaderboard_fetch(
|
|
|
|
type: string,
|
|
|
|
page: number) {
|
2017-09-08 14:49:57 +01:00
|
|
|
const key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-11-10 17:15:11 +00:00
|
|
|
this.apiUrl + '/stats/leaderboard/paged',
|
2017-09-08 14:49:57 +01:00
|
|
|
{
|
|
|
|
session_key : key,
|
2017-11-10 17:15:11 +00:00
|
|
|
type : type,
|
|
|
|
page: page,
|
2017-09-08 14:49:57 +01:00
|
|
|
}
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-08 14:49:57 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
|
2017-09-27 13:54:10 +01:00
|
|
|
// Initial Map Data
|
|
|
|
public getMapData(data) {
|
|
|
|
data.session_key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-09-28 12:47:09 +01:00
|
|
|
this.apiUrl + '/v1/supplier/location',
|
2017-09-27 13:54:10 +01:00
|
|
|
data
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-09-27 13:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 13:26:18 +00:00
|
|
|
// Load Association Data
|
|
|
|
public getAssocData(data) {
|
2017-11-23 17:16:46 +00:00
|
|
|
data.session_key = this.sessionKey;
|
|
|
|
return this.http.post<any>(
|
2017-12-08 13:26:18 +00:00
|
|
|
this.apiUrl + '/v1/supplier/location/trail',
|
2017-11-23 17:16:46 +00:00
|
|
|
data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:44:17 +01:00
|
|
|
// Basic Customer User stats API
|
2017-12-18 12:57:40 +00:00
|
|
|
public customerStats() {
|
2017-09-08 14:49:57 +01:00
|
|
|
const key = this.sessionKey;
|
2017-11-15 14:46:04 +00:00
|
|
|
return this.http.post<any>(
|
2017-12-18 12:57:40 +00:00
|
|
|
this.apiUrl + '/stats/customer',
|
2017-09-08 14:49:57 +01:00
|
|
|
{
|
2017-08-31 18:44:17 +01:00
|
|
|
session_key : key,
|
2017-09-08 14:49:57 +01:00
|
|
|
}
|
2017-11-15 14:16:05 +00:00
|
|
|
);
|
2017-06-19 17:20:21 +01:00
|
|
|
}
|
2017-08-16 16:12:40 +01:00
|
|
|
}
|